/
v.bolshakov
/
AIEcosystem-Testing
Обзор
Документация
Войти
/
v.bolshakov
/
AIEcosystem-Testing
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
dev
common/models/Queue.php
132 строки
4 KB
Developer
Initial commit
03 авг 2026, 17:43
03 авг 2026, 17:43
7433917
Код
Авторство
О чём код?
<?php namespace common\models; use common\jobs\base\BaseJob; use common\models\base\BaseModel; use Yii; use yii\helpers\ArrayHelper; /** * Таблица задач * * @property integer $id * @property string $channel * @property integer $pushed_at * @property integer $ttr * @property integer $delay * @property integer $priority * @property integer $reserved_at * @property integer $attempt * @property integer $done_at * @property string $job * @property integer $object_id * @property integer $user_id * * @author Dmitry E. Semenov <sde.tomsk@gmail.com> * @copyright Self (c) 2020 */ class Queue extends BaseModel { /** * @inheritdoc */ public static function tableName() { return 'queue'; } /** * @inheritdoc */ public $modelName = ['Job', 'Jobs', 'Jobs']; /** * @inheritdoc */ public function rules() { return [ [['channel', 'pushed_at', 'ttr', 'job'], 'required'], [ [ 'id', 'reserved_at', 'ttr', 'pushed_at', 'delay', 'priority', 'attempt', 'done_at', 'object_id', 'user_id' ], 'number' ], [['channel'], 'string', 'max' => 255], [['job'], 'string'], [['priority'], 'default', 'value' => 1024], ]; } /** * @inheritdoc */ public function attributeLabels() { return ArrayHelper::merge(parent::attributeLabels(), [ 'channel' => Yii::t('app', 'Канал'), 'pushed_at' => Yii::t('app', 'Отправлено'), 'ttr' => Yii::t('app', 'Время жизни'), 'delay' => Yii::t('app', 'Отложенная задача'), 'priority' => Yii::t('app', 'Приоритет'), 'reserved_at' => Yii::t('app', 'Ожидает до указанного времени'), 'attempt' => Yii::t('app', 'Повторы'), 'done_at' => Yii::t('app', 'Выполнено'), 'job' => Yii::t('app', 'Задача'), 'object_id' => Yii::t('app', 'Object'), 'user_id' => Yii::t('app', 'User'), ]); } /** * Перезапустить задачу на выполнение * (установить атребуты в начальное состояние) * @return bool */ public function restart() { return $this ->updateAttributes([ 'reserved_at' => null, 'done_at' => null, ]) > 0; } /** * Перезапустить задачу на выполнение * (установить атребуты в начальное состояние) * @return bool */ public function isRestartable() { return $this->reserved_at != null and $this->done_at == null and $this->ttr < ($this->reserved_at - time()); } /** * Выполнить задачу * * @return bool|mixed|void * @throws \Throwable */ public function execute() { /** @var BaseJob $job */ $job = unserialize($this->job); if ($job instanceof BaseJob) { return $job->execute(null); } else { return false; } } }