/** * List all tasks */ public function actionIndex() { // Update task index $this->getScheduler()->getTasks(); $models = SchedulerTask::find()->all(); echo $this->ansiFormat('Scheduled Tasks', Console::UNDERLINE) . PHP_EOL; foreach ($models as $model) { /* @var SchedulerTask $model */ $row = sprintf("%s\t%s\t%s\t%s\t%s", $model->name, $model->schedule, is_null($model->last_run) ? 'NULL' : $model->last_run, $model->next_run, $model->getStatus()); $color = isset($this->_statusColors[$model->status_id]) ? $this->_statusColors[$model->status_id] : null; echo $this->ansiFormat($row, $color) . PHP_EOL; } }
/** * Removes any records of tasks that no longer exist. * * @param Task[] $tasks */ public function cleanTasks($tasks) { $currentTasks = ArrayHelper::map($tasks, function ($task) { return $task->getName(); }, 'description'); foreach (SchedulerTask::find()->indexBy('name')->all() as $name => $task) { /* @var SchedulerTask $task */ if (!array_key_exists($name, $currentTasks)) { SchedulerLog::deleteAll(['scheduler_task_id' => $task->id]); $task->delete(); } } }
/** * @param $task * @return array|null|SchedulerTask|\yii\db\ActiveRecord */ public static function createTaskModel($task) { $model = SchedulerTask::find()->where(['name' => $task->getName()])->one(); if (!$model) { $model = new SchedulerTask(); $model->name = $task->getName(); $model->active = $task->active; $model->next_run = $task->getNextRunDate(); $model->last_run = NULL; $model->status_id = self::STATUS_PENDING; } $model->description = $task->description; $model->schedule = $task->schedule; $model->save(false); return $model; }