From 192c6fe4b2cd2ebeb150840303d38f9966552616 Mon Sep 17 00:00:00 2001 From: Sasha Rybkin Date: Mon, 26 Oct 2020 14:08:52 +0300 Subject: [PATCH] =?UTF-8?q?=D0=9F=D0=BE=D0=B2=D1=82=D0=BE=D1=80=D1=8F?= =?UTF-8?q?=D1=8E=D1=89=D0=B8=D0=B5=D1=81=D1=8F=20=D0=B7=D0=B0=D0=B4=D0=B0?= =?UTF-8?q?=D1=87=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- console/MoleController.php | 43 +++++++++++++++----- messages/ru-RU/mole.php | 1 + models/MoleTask.php | 67 +++++++++++++++++++++++++++----- models/search/MoleTaskSearch.php | 2 +- views/default/_form.php | 30 +++++++------- views/default/index.php | 7 ++++ views/default/view.php | 7 ++++ 7 files changed, 122 insertions(+), 35 deletions(-) diff --git a/console/MoleController.php b/console/MoleController.php index b327387..4a33216 100644 --- a/console/MoleController.php +++ b/console/MoleController.php @@ -22,6 +22,7 @@ use yii\base\InlineAction; */ class MoleController extends Controller { + /** * Запуск всех агентов для проекта yii из mole_task * @return int Exit code @@ -29,7 +30,7 @@ class MoleController extends Controller public function actionIndex() { $filePatch = Yii::getAlias('@app/runtime/lock.lock'); - if(!file_exists($filePatch)) + if (!file_exists($filePatch)) { $fp = fopen($filePatch, "w"); fwrite($fp, ""); @@ -46,25 +47,49 @@ class MoleController extends Controller try { $task->dateStart = date('Y-m-d H:i:s'); - $task->save(); - $class = 'app\commands\\' . $task->controller; + $task->save(); + $params = explode('/', $task->controller); + $controller = $this->format($params[0]); + $action = 'actionIndex'; + if (isset($params[1])) + { + $action = 'action' . $this->format($params[1]); + } + if(stripos($controller, 'Controller') === false) + { + $controller .= 'Controller'; + } + + $class = 'app\\commands\\' . $controller; if (class_exists($class)) { $control = new $class($task->controller, 'product'); - $control->actionIndex(unserialize($task->params)); + if (method_exists($control, $action)) + { + $control->{$action}(unserialize($task->params)); + $task->isReady = 1; + } } - $task->dateEnd = date('Y-m-d H:i:s'); - $task->completed = 1; - $task->save(); - } catch (\yii\elasticsearch\Exception $ex) + $task->setCompleted(); + } catch (\Exception $ex) { $task->dateStart = null; $task->save(); - print_r($ex); + print_r($ex->getMessage()); } } } return ExitCode::OK; } + protected function format($controller) + { + $params = explode('-', $controller); + foreach ($params as $key => $value) + { + $params[$key] = ucfirst($value); + } + return implode('', $params); + } + } diff --git a/messages/ru-RU/mole.php b/messages/ru-RU/mole.php index 2d3c791..52c74e9 100644 --- a/messages/ru-RU/mole.php +++ b/messages/ru-RU/mole.php @@ -15,6 +15,7 @@ return [ 'Is Ready' => 'Готов', 'Completed' => 'Завершен', 'Priority' => 'Приоритет', + 'Period' => 'Периодичность', // 'Childs Total Count' => '', // 'Childs Completed' => '', 'Status' => 'Статус', diff --git a/models/MoleTask.php b/models/MoleTask.php index b0d69e7..f32dc20 100644 --- a/models/MoleTask.php +++ b/models/MoleTask.php @@ -24,9 +24,11 @@ use Yii; * @property int $childsCompleted * @property string $status * @property string $project + * @property int $period */ class MoleTask extends \yii\db\ActiveRecord { + /** * {@inheritdoc} */ @@ -42,7 +44,7 @@ class MoleTask extends \yii\db\ActiveRecord { return [ //[['parentId', 'isReady', 'childsTotalCount', 'childsCompleted', 'status'], 'required'], - [['parentId', 'isReady', 'completed', 'priority', 'childsTotalCount', 'childsCompleted'], 'integer'], + [['parentId', 'isReady', 'completed', 'priority', 'childsTotalCount', 'childsCompleted', 'period'], 'integer'], [['dateAdd', 'dateStart', 'dateEnd'], 'safe'], [['params'], 'string'], [['module', 'controller', 'type', 'name', 'status'], 'string', 'max' => 255], @@ -73,22 +75,67 @@ class MoleTask extends \yii\db\ActiveRecord 'childsCompleted' => Yii::t('mole', 'Childs Completed'), 'status' => Yii::t('mole', 'Status'), 'project' => Yii::t('mole', 'Project'), + 'period' => Yii::t('mole', 'Period'), ]; } - + /** * Выбираем все строки с project */ public function getAllTask() { return self::find() - ->andWhere([ - 'project'=> Yii::$app->getModule('cron')->project, - 'completed' => 0, - ]) - ->andWhere(['IS', 'dateStart', NULL]) - ->andWhere(['IS', 'dateEnd', NULL]) - // ->andWhere(['<=', 'dateAdd', date('Y-m-d H:i:s')]) - ->all(); + ->andWhere([ + 'project' => Yii::$app->getModule('cron')->project, + 'completed' => 0, + ]) + ->andWhere(['IS', 'dateStart', NULL]) + ->andWhere(['IS', 'dateEnd', NULL]) + ->andWhere(['<=', 'dateAdd', date('Y-m-d H:i:s')]) + ->orderBy('priority DESC') + ->all(); } + + public function setCompleted() + { + $this->dateEnd = date('Y-m-d H:i:s'); + $this->completed = 1; + if ($this->save() && $this->period > 0) + { + $model = new MoleTask; + $model->attributes = $this->attributes; + $model->isReady = 0; + $model->completed = 0; + $model->dateStart = null; + $model->dateEnd = null; + $model->dateAdd = date('Y-m-d H:i:s', (strtotime("+$this->period seconds"))); + $model->save(); + } + } + + /** + * Добавление агента (обертка) + * @param string $controller + * @param string $name + * @param array $params + * @param int $priority + * @param int $period + * @param date $dateAdd + * @param string $project + * @return boolean + */ + public static function add($controller, $name, $params = array(), $priority = 0, $period = 0, $dateAdd = false, $project = false) + { + $model = new MoleTask(); + $model->controller = $controller; + $model->name = $name; + $model->params = serialize($params); + $model->priority = $priority; + $model->period = $period; + $model->dateAdd = $dateAdd ?: date('Y-m-d H:i:s'); + $model->project = $project ?: Yii::$app->getModule('cron')->project; + $model->type = $model->project . '.' . $model->controller; // для совместимости + return $model->save(); + } + } diff --git a/models/search/MoleTaskSearch.php b/models/search/MoleTaskSearch.php index 8dea28e..eb8d026 100644 --- a/models/search/MoleTaskSearch.php +++ b/models/search/MoleTaskSearch.php @@ -19,7 +19,7 @@ class MoleTaskSearch extends MoleTask public function rules() { return [ - [['id', 'parentId', 'isReady', 'completed', 'priority', 'childsTotalCount', 'childsCompleted'], 'integer'], + [['id', 'parentId', 'isReady', 'completed', 'priority', 'childsTotalCount', 'childsCompleted', 'period'], 'integer'], [['dateAdd', 'dateStart', 'dateEnd'], 'safe'], [['params'], 'string'], [['module', 'controller', 'type', 'name', 'status'], 'string', 'max' => 255], diff --git a/views/default/_form.php b/views/default/_form.php index ad1f3ed..0d5df99 100644 --- a/views/default/_form.php +++ b/views/default/_form.php @@ -8,29 +8,29 @@ use yii\widgets\ActiveForm; /* @var $form yii\widgets\ActiveForm */ ?> +
+ - ['class' => "form-horizontal"]]); ?> -{input}{error}
'; ?> + field($model, 'project'); ?> -field($model, 'project', ['template' => $template, 'labelOptions' => ['class' => 'col-sm-2 control-label']]); ?> + field($model, 'name'); ?> -field($model, 'name', ['template' => $template, 'labelOptions' => ['class' => 'col-sm-2 control-label']]); ?> + field($model, 'dateAdd'); ?> + field($model, 'dateStart'); ?> + field($model, 'dateEnd'); ?> -field($model, 'dateAdd', ['template' => $template, 'labelOptions' => ['class' => 'col-sm-2 control-label']]); ?> -field($model, 'dateStart', ['template' => $template, 'labelOptions' => ['class' => 'col-sm-2 control-label']]); ?> -field($model, 'dateEnd', ['template' => $template, 'labelOptions' => ['class' => 'col-sm-2 control-label']]); ?> + field($model, 'module'); ?> -field($model, 'module', ['template' => $template, 'labelOptions' => ['class' => 'col-sm-2 control-label']]); ?> + field($model, 'controller'); ?> + field($model, 'params'); ?> + field($model, 'period'); ?> -field($model, 'controller', ['template' => $template, 'labelOptions' => ['class' => 'col-sm-2 control-label']]); ?> -field($model, 'params', ['template' => $template, 'labelOptions' => ['class' => 'col-sm-2 control-label']]); ?> +
-
-
'btn btn-success']) ?> +
+ +
- - - diff --git a/views/default/index.php b/views/default/index.php index 28faf2e..565e8fa 100644 --- a/views/default/index.php +++ b/views/default/index.php @@ -68,6 +68,13 @@ $this->params['breadcrumbs'][] = $this->title; // 'filter' => [0 => Yii::t('mole', 'No'), 1 => Yii::t('mole', 'Yes')] ], 'priority', + [ + 'attribute' => 'period', + 'value' => function($model) + { + return $model->period > 0 ? $model->period : Yii::t('mole', 'No'); + }, + ], //'childsTotalCount', //'childsCompleted', // 'status', diff --git a/views/default/view.php b/views/default/view.php index b7467c3..5ca3004 100644 --- a/views/default/view.php +++ b/views/default/view.php @@ -70,6 +70,13 @@ $this->params['breadcrumbs'][] = $this->title; ], 'priority', 'status', + [ + 'attribute' => 'period', + 'value' => function($model) + { + return $model->period > 0 ? $model->period : Yii::t('mole', 'No'); + }, + ], ], ]) ?>