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 */ ?> +