Повторяющиеся задачи
This commit is contained in:
parent
580081a388
commit
192c6fe4b2
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -15,6 +15,7 @@ return [
|
|||
'Is Ready' => 'Готов',
|
||||
'Completed' => 'Завершен',
|
||||
'Priority' => 'Приоритет',
|
||||
'Period' => 'Периодичность',
|
||||
// 'Childs Total Count' => '',
|
||||
// 'Childs Completed' => '',
|
||||
'Status' => 'Статус',
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -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],
|
||||
|
|
|
@ -8,29 +8,29 @@ use yii\widgets\ActiveForm;
|
|||
/* @var $form yii\widgets\ActiveForm */
|
||||
?>
|
||||
|
||||
<div class="mole-form">
|
||||
<?php $form = ActiveForm::begin(); ?>
|
||||
|
||||
<?php $form = ActiveForm::begin(['options' => ['class' => "form-horizontal"]]); ?>
|
||||
<?php $template = '{label}<div class="col-sm-10">{input}<span class="help-block m-b-none">{error}</span></div>'; ?>
|
||||
<?= $form->field($model, 'project'); ?>
|
||||
|
||||
<?= $form->field($model, 'project', ['template' => $template, 'labelOptions' => ['class' => 'col-sm-2 control-label']]); ?>
|
||||
<?= $form->field($model, 'name'); ?>
|
||||
|
||||
<?= $form->field($model, 'name', ['template' => $template, 'labelOptions' => ['class' => 'col-sm-2 control-label']]); ?>
|
||||
<?= $form->field($model, 'dateAdd'); ?>
|
||||
<?= $form->field($model, 'dateStart'); ?>
|
||||
<?= $form->field($model, 'dateEnd'); ?>
|
||||
|
||||
<?= $form->field($model, 'dateAdd', ['template' => $template, 'labelOptions' => ['class' => 'col-sm-2 control-label']]); ?>
|
||||
<?= $form->field($model, 'dateStart', ['template' => $template, 'labelOptions' => ['class' => 'col-sm-2 control-label']]); ?>
|
||||
<?= $form->field($model, 'dateEnd', ['template' => $template, 'labelOptions' => ['class' => 'col-sm-2 control-label']]); ?>
|
||||
<?= $form->field($model, 'module'); ?>
|
||||
|
||||
<?= $form->field($model, 'module', ['template' => $template, 'labelOptions' => ['class' => 'col-sm-2 control-label']]); ?>
|
||||
<?= $form->field($model, 'controller'); ?>
|
||||
<?= $form->field($model, 'params'); ?>
|
||||
<?= $form->field($model, 'period'); ?>
|
||||
|
||||
<?= $form->field($model, 'controller', ['template' => $template, 'labelOptions' => ['class' => 'col-sm-2 control-label']]); ?>
|
||||
<?= $form->field($model, 'params', ['template' => $template, 'labelOptions' => ['class' => 'col-sm-2 control-label']]); ?>
|
||||
<div class="form-group">
|
||||
|
||||
<div class="form-group">
|
||||
<div class="col-sm-12 col-sm-offset-2">
|
||||
<?= Html::submitButton(Yii::t('mole', 'Save'), ['class' => 'btn btn-success']) ?>
|
||||
|
||||
</div>
|
||||
|
||||
<?php ActiveForm::end(); ?>
|
||||
</div>
|
||||
|
||||
<?php ActiveForm::end(); ?>
|
||||
|
||||
|
||||
|
|
|
@ -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',
|
||||
|
|
|
@ -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');
|
||||
},
|
||||
],
|
||||
],
|
||||
])
|
||||
?>
|
||||
|
|
Loading…
Reference in New Issue