Yii2 Cron Job
, (*1)
Yii2 extension to help in the creation of automated console scripts. It helps to manage the execution of console scripts, for example avoiding the execution if the previous cron is already running. It generates a history of cron executed, with the time spent and helps to batch processing the script., (*2)
Installation
The preferred way to install this extension is through composer., (*3)
Either run, (*4)
php composer.phar require --prefer-dist fedemotta/yii2-cronjob "*"
or add, (*5)
"fedemotta/yii2-cronjob": "*"
to the require section of your composer.json
file., (*6)
This extension needs a database to manage the cron job execution. Run the following migration to create the table structure:, (*7)
yii migrate --migrationPath=@fedemotta/cronjob/migrations
Usage
Once the extension is installed, you can use it as a helper in your console controller., (*8)
See the following example:, (*9)
<?php
namespace somenamespace\controllers;
use fedemotta\cronjob\models\CronJob;
use somenamespace\SomeModel;
use yii\console\Controller;
/**
* SomeContrController controller
*/
class SomeContrController extends Controller {
/**
* Run SomeModel::some_method for a period of time
* @param string $from
* @param string $to
* @return int exit code
*/
public function actionInit($from, $to){
$dates = CronJob::getDateRange($from, $to);
$command = CronJob::run($this->id, $this->action->id, 0, CronJob::countDateRange($dates));
if ($command === false){
return Controller::EXIT_CODE_ERROR;
}else{
foreach ($dates as $date) {
//this is the function to execute for each day
SomeModel::some_method((string) $date);
}
$command->finish();
return Controller::EXIT_CODE_NORMAL;
}
}
/**
* Run SomeModel::some_method for today only as the default action
* @return int exit code
*/
public function actionIndex(){
return $this->actionInit(date("Y-m-d"), date("Y-m-d"));
}
/**
* Run SomeModel::some_method for yesterday
* @return int exit code
*/
public function actionYesterday(){
return $this->actionInit(date("Y-m-d", strtotime("-1 days")), date("Y-m-d", strtotime("-1 days")));
}
}
Run the SomeModel::some_method for today:, (*10)
./yii some-contr
For yesterday:, (*11)
./yii some-contr/yesterday
For some custom dates like this:, (*12)
./yii some-contr/init 2010-10-10 2012-10-10
And you can add a cron job to run every 10 minutes with some controller action like this:, (*13)
*/10 * * * * /path/to/yii/application/yii some-contr/yesterday >> /var/log/console-app.log 2>&1