diff --git a/CronController.php b/CronController.php index d392234..be0e9f7 100644 --- a/CronController.php +++ b/CronController.php @@ -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) @@ -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'); @@ -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'; } @@ -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); } } @@ -347,4 +364,4 @@ protected function arrayToDocComment(array $runSettings) return $result; } -} \ No newline at end of file +}