Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix cron tags #8

Closed
wants to merge 5 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 22 additions & 5 deletions CronController.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ class CronController extends Controller {
*/
public $logsDir = null;

/**
* @var string path for category for logging
*/
public $logsCategory = 'yii2-cronjobs';

/**
* Update or rewrite log file
* False - rewrite True - update(add to end logs)
Expand Down Expand Up @@ -212,9 +217,15 @@ protected function isWindowsOS(){
* @param array $args List of run-tags to running actions (if empty, only "default" run-tag will be runned).
*/
public function actionRun($args = array()){
$tags = &$args;

//always run default values
$tags[] = 'default';

//if we have specified args in input (cron-tags) insert them in array
if (!empty($args)){
$tags[] = &$args;
}

//Getting timestamp will be used as current
$time = strtotime($this->timestamp);
if ($time === false) throw new CException('Bad timestamp format');
Expand All @@ -236,6 +247,11 @@ public function actionRun($args = array()){
else $stdout = $this->logFileName;

$stdout = $this->formatFileName($stdout, $task);
//if stdout does not exist then create the file
if (!file_exists($stdout)){
touch($stdout);
}

if(!is_writable($stdout)) {
$stdout = '/dev/null';
}
Expand All @@ -244,15 +260,16 @@ public function actionRun($args = array()){
if(!is_writable($stderr)) {
$stdout = '/dev/null';
}

$this->runCommandBackground($command, $stdout, $stderr);
Yii::info('Running task ['.(++$runned).']: '.$task['command'].' '.$task['action']);
Yii::info('Running task ['.(++$runned).']: '.$task['command'].' '.$task['action'],$this->logsCategory);
}
}
if ($runned > 0){
Yii::info('Runned '.$runned.' task(s) at '.date('r', $time));
Yii::info('Runned '.$runned.' task(s) at '.date('r', $time),$this->logsCategory);
}
else{
Yii::info('No task on '.date('r', $time));
Yii::info('No task on '.date('r', $time),$this->logsCategory);
}
}

Expand Down Expand Up @@ -347,4 +364,4 @@ protected function arrayToDocComment(array $runSettings)

return $result;
}
}
}