/** * @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; }
/** * 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; } }
/** * Runs the action * * @return string result content */ public function run($id) { $model = SchedulerTask::findOne($id); $request = Yii::$app->getRequest(); if (!$model) { throw new \yii\web\HttpException(404, 'The requested page does not exist.'); } if ($model->load($request->post())) { $model->save(); } $logModel = new SchedulerLog(); $logModel->scheduler_task_id = $model->id; $logDataProvider = $logModel->search($_GET); return $this->controller->render($this->view ?: $this->id, ['model' => $model, 'logModel' => $logModel, 'logDataProvider' => $logDataProvider]); }
<?php /** * Update Task View * * @var yii\web\View $this * @var webtoolsnz\scheduler\models\SchedulerTask $model */ use yii\helpers\Html; use webtoolsnz\scheduler\models\SchedulerTask; use yii\bootstrap\Tabs; use yii\bootstrap\ActiveForm; use webtoolsnz\widgets\RadioButtonGroup; use yii\grid\GridView; $this->title = $model->__toString(); $this->params['breadcrumbs'][] = ['label' => SchedulerTask::label(2), 'url' => ['index']]; $this->params['breadcrumbs'][] = $model->__toString(); ?> <div class="task-update"> <h1><?php echo $this->title; ?> </h1> <?php $this->beginBlock('main'); ?> <?php $form = ActiveForm::begin(['id' => $model->formName(), 'layout' => 'horizontal', 'enableClientValidation' => false]); ?>
<?php /** * Index View for scheduled tasks * * @var \yii\web\View $this * @var \yii\data\ActiveDataProvider $dataProvider * @var \webtoolsnz\scheduler\models\SchedulerTask $model */ use yii\helpers\Html; use yii\helpers\Url; use yii\grid\GridView; $this->title = \webtoolsnz\scheduler\models\SchedulerTask::label(2); $this->params['breadcrumbs'][] = $this->title; ?> <div class="scheduler-index"> <h1><?php echo $this->title; ?> </h1> <div class="table-responsive"> <?php \yii\widgets\Pjax::begin(); ?> <?php echo GridView::widget(['layout' => '{summary}{pager}{items}{pager}', 'dataProvider' => $dataProvider, 'pager' => ['class' => yii\widgets\LinkPager::className(), 'firstPageLabel' => Yii::t('app', 'First'), 'lastPageLabel' => Yii::t('app', 'Last')], 'columns' => [['attribute' => 'name', 'format' => 'raw', 'value' => function ($t) { return Html::a($t->name, ['update', 'id' => $t->id]); }], 'name', 'description', 'schedule', 'status']]);
/** * Runs the action * * @return string result content */ public function run() { $model = new SchedulerTask(); $dataProvider = $model->search($_GET); return $this->controller->render($this->view ?: $this->id, ['dataProvider' => $dataProvider, 'model' => $model]); }
/** * @return \yii\db\ActiveQuery */ public function getSchedulerTask() { return $this->hasOne(\webtoolsnz\scheduler\models\SchedulerTask::className(), ['id' => 'scheduler_task_id']); }
/** * Given the className of a task, it will return a new instance of that task. * If the task doesn't exist, null will be returned. * * @param $className * @return null|object * @throws \yii\base\InvalidConfigException */ public function loadTask($className) { $className = implode('\\', [$this->taskNameSpace, $className]); try { $task = Yii::createObject($className); $task->setModel(SchedulerTask::createTaskModel($task)); } catch (\ReflectionException $e) { $task = null; } return $task; }