http://lhdst-163-com.iteye.com/blog/2337379
It is very simple to add support for scheduled tasks under the YAF framework.
You can create a new cli directory and put it in index.php
<?php define('BENCHAMRK_START', microtime(TRUE)); ini_set('display_errors','On'); error_reporting(E_ALL & ~E_DEPRECATED); date_default_timezone_set("Asia/Hong_Kong"); define('ROOT_PATH', realpath(dirname(dirname(__FILE__)))); define('APPLICATION_PATH', ROOT_PATH . '/application'); define('PUBLIC_PATH', ROOT_PATH . '/public'); define('VENDOR_PATH', ROOT_PATH . '/vendor'); $app = new Yaf_Application(ROOT_PATH . "/conf/application.ini"); // $app->bootstrap()->run(); $app->getDispatcher()->dispatch(new Yaf_Request_Simple("index", "Admin", "Cli")); ?>
Usually executed under the website
$app->bootstrap()->run();
Under cli, you can customize an action to execute, so you can change it to
$app->getDispatcher()->dispatch(new Yaf_Request_Simple("index", "Admin", "Cli"));
But there is a problem with this. If some initialization operations are performed in bootstrap, call directly
$app->getDispatcher()->dispatch(new Yaf_Request_Simple("index", "Admin", "Cli"));
It will cause some class libraries not to be introduced, which will cause the execution environment of cli to be inconsistent with the website, which may lead to cli execution errors. To avoid this problem, we still execute
$app->bootstrap()->run();
By default, after bootstrap is executed, the indexAction of indexController under the default module will be executed. We handle it here in indexAction
<?php class IndexController extends Controller { /** * default action */ public function indexAction() { if (!empty($_SERVER['argv']) && $_SERVER['argv'][1] == 'cli') { $this->runCli(); exit; } redirect("/xxx/index"); return false; } protected function runCli() { echo datetime() . "------ START CRON JOB -----" . PHP_EOL; D("xxx")->cliTask(); echo datetime() . "------ END CRON JOB -----" . PHP_EOL; return false; } }
When the second parameter is cli, we execute the cli command, otherwise we jump to the default page of the website.
We can write the execution command into the cli.sh file and place it in the same directory as /public/index.php
#!/bin/bash filepath=$(cd "$(dirname "$0")"; pwd) cd $filepath logname="cli_"$(date +%Y%m%d); su www-data -c "php index.php cli 1>>../application/log/cli/$logname.log 2>&1"
Then add a cronjob to the system and define a cycle to execute once.
You can also execute it manually to check the running status, the content will be output to ../application/log/cli/$logname.log
sh cli.sh
To add timed tasks, please check this blog
http://lhdst-163-com.iteye.com/blog/1797038