| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184 |
- <?php
- namespace App\Module\System\Models;
- use UCore\ModelCore;
- /**
- * 队列任务模型
- *
- * field start
- * @property int $id
- * @property string $queue 队列名称
- * @property string $payload 任务载荷
- * @property int $attempts 尝试次数
- * @property int $reserved_at 保留时间
- * @property int $available_at 可用时间
- * @property int $created_at 创建时间
- * field end
- *
- */
- class Job extends ModelCore
- {
- protected $table = 'jobs';
-
- public $timestamps = false;
- protected $fillable = [
- 'queue',
- 'payload',
- 'attempts',
- 'reserved_at',
- 'available_at',
- 'created_at'
- ];
- protected $casts = [
- 'payload' => 'array',
- 'attempts' => 'integer',
- 'reserved_at' => 'integer',
- 'available_at' => 'integer',
- 'created_at' => 'integer'
- ];
- protected $appends = [
- 'queue_name',
- 'created_at_formatted',
- 'available_at_formatted',
- 'reserved_at_formatted',
- 'status',
- 'job_class',
- 'job_parameters',
- 'job_parameters_short'
- ];
- //attrlist start
- public static $attrlist = [
- 'id',
- 'queue',
- 'payload',
- 'attempts',
- 'reserved_at',
- 'available_at',
- 'created_at'
- ];
- //attrlist end
- /**
- * 获取队列名称访问器
- */
- public function getQueueNameAttribute(): string
- {
- return $this->queue ?? 'default';
- }
- /**
- * 获取格式化的创建时间
- */
- public function getCreatedAtFormattedAttribute(): string
- {
- return $this->created_at ? date('Y-m-d H:i:s', $this->created_at) : '';
- }
- /**
- * 获取格式化的可用时间
- */
- public function getAvailableAtFormattedAttribute(): string
- {
- return $this->available_at ? date('Y-m-d H:i:s', $this->available_at) : '';
- }
- /**
- * 获取格式化的保留时间
- */
- public function getReservedAtFormattedAttribute(): string
- {
- return $this->reserved_at ? date('Y-m-d H:i:s', $this->reserved_at) : '';
- }
- /**
- * 获取任务状态
- */
- public function getStatusAttribute(): string
- {
- $now = time();
-
- if ($this->reserved_at && $this->reserved_at > $now) {
- return '已保留';
- }
-
- if ($this->available_at > $now) {
- return '延迟中';
- }
-
- return '待处理';
- }
- /**
- * 获取任务类名
- */
- public function getJobClassAttribute(): string
- {
- if (!$this->payload) {
- return '';
- }
- $payload = is_array($this->payload) ? $this->payload : json_decode($this->payload, true);
- return $payload['displayName'] ?? $payload['job'] ?? '';
- }
- /**
- * 获取任务参数
- */
- public function getJobParametersAttribute(): string
- {
- if (!$this->payload) {
- return '';
- }
- $payload = is_array($this->payload) ? $this->payload : json_decode($this->payload, true);
- // 提取有用的参数信息
- $params = [];
- if (isset($payload['maxTries'])) {
- $params['最大尝试次数'] = $payload['maxTries'];
- }
- if (isset($payload['timeout'])) {
- $params['超时时间'] = $payload['timeout'] . 's';
- }
- if (isset($payload['data']['command'])) {
- // 尝试解析序列化的命令参数
- $command = $payload['data']['command'];
- if (strpos($command, 'O:') === 0) {
- // 这是序列化的对象,暂时不解析
- $params['命令类型'] = '序列化对象';
- }
- }
- if (empty($params)) {
- return '无参数';
- }
- $result = [];
- foreach ($params as $key => $value) {
- $result[] = "{$key}: {$value}";
- }
- return implode(', ', $result);
- }
- /**
- * 获取简化的任务参数(用于列表显示)
- */
- public function getJobParametersShortAttribute(): string
- {
- $params = $this->job_parameters;
- if (mb_strlen($params) > 50) {
- return mb_substr($params, 0, 50) . '...';
- }
- return $params;
- }
- }
|