Повторяющиеся задачи

This commit is contained in:
2020-10-26 14:08:52 +03:00
parent 580081a388
commit 192c6fe4b2
7 changed files with 122 additions and 35 deletions

View File

@@ -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();
}
}

View File

@@ -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],