-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit c9849c7
Showing
7 changed files
with
1,404 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
<?php | ||
|
||
/** | ||
* CronCommand represents console command to run actions for daemon, task wrapper and to display tasks statuses. | ||
* | ||
* Daemon action need to be manually added to the server's cron schedule. | ||
* Example of cron entry: * * * * * php /path/to/yiic cron daemon >> /dev/null 2>&1 | ||
* | ||
* Run action create CronProcess and runs task, serialized in it. | ||
* | ||
* Default index action prints in console current list of tasks with detailed information: cron schedule, task route | ||
* with params, last start and stop time and current status. | ||
* | ||
* @author Vadym Stepanov <[email protected]> | ||
* @date 18.01.2016 | ||
*/ | ||
class CronCommand extends CConsoleCommand | ||
{ | ||
/** @var CronService */ | ||
private $_service; | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function beforeAction($action, $params) | ||
{ | ||
$this->_service = Yii::app()->cron; | ||
|
||
return parent::beforeAction($action, $params); | ||
} | ||
|
||
/** | ||
* Daemon action to run every minute (need to be manually added to the server's cron schedule) | ||
* and check if any of the specified tasks by user can be executed. | ||
* For each task get CronProcess instance with information of it's current state. Check run conditions (time and | ||
* date, uniqueness) and run wrapper command if all conditions are met. | ||
*/ | ||
public function actionDaemon() | ||
{ | ||
$this->_service->loadTasks(); | ||
|
||
/** @var CronTask $task */ | ||
foreach ($this->_service->getActiveTasks() as $task) { | ||
if (!$task->canRun()) { | ||
continue; | ||
} | ||
|
||
if ($task->getProcessInfo()->isRunning() && $task->isUnique()) { | ||
CronService::log( | ||
"Cannot run task '{$task->getName()}': it is still running and does not allow overlapping (is unique)", | ||
CLogger::LEVEL_WARNING | ||
); | ||
} else { | ||
$task->getProcessInfo()->runWrapper(); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Wrapper to handle execution process of the task | ||
* @param string $id unique identifier of the task to be executed | ||
*/ | ||
public function actionRun($id) | ||
{ | ||
CronProcess::createById($id, $this->_service)->run(); | ||
} | ||
|
||
/** | ||
* Action to display detailed status of current available tasks | ||
*/ | ||
public function actionIndex() | ||
{ | ||
$this->_service->loadTasks(); | ||
|
||
/** @var CronTask $task */ | ||
foreach ($this->_service->getActiveTasks() as $task) { | ||
$process = $task->getProcessInfo(); | ||
echo "Task '{$task->getName()}':\n"; | ||
$output = $task->getOutputFile() ? " > {$task->getOutputFile()}" : ''; | ||
$params = array(); | ||
foreach ($task->getParams() as $key => $value) { | ||
$params[] = "--{$key}={$value}"; | ||
} | ||
echo "{$task->getCron()} {$task->getCommand()} {$task->getCommandAction()} "; | ||
echo implode(' ', $params) . "{$output}\n"; | ||
echo "Last start: {$process->lastStart} Last finish: {$process->lastStop} "; | ||
echo 'Status: '; | ||
switch ($process->status) { | ||
case CronProcess::STATUS_NEW: | ||
echo 'NEW (not started yet)'; | ||
break; | ||
case CronProcess::STATUS_RUNNING: | ||
echo "RUNNING (PID: {$process->pid})"; | ||
break; | ||
case CronProcess::STATUS_FINISHED: | ||
echo 'FINISHED'; | ||
break; | ||
case CronProcess::STATUS_FAILED: | ||
echo 'FAILED'; | ||
break; | ||
} | ||
echo "\n\n"; | ||
} | ||
} | ||
} |
Oops, something went wrong.