| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189 |
- <?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'] ?? '';
- }
- /**
- * 获取任务参数 - 直接反序列化payload原样展示
- */
- public function getJobParametersAttribute(): string
- {
- if (!$this->payload) {
- return '无参数';
- }
- $payload = is_array($this->payload) ? $this->payload : json_decode($this->payload, true);
- if (!is_array($payload)) {
- // 如果JSON解码失败,尝试其他格式
- if (is_string($this->payload)) {
- return $this->payload;
- }
- return '无参数';
- }
- return $this->formatArrayForDisplay($payload);
- }
- /**
- * 格式化数组为显示字符串
- */
- private function formatArrayForDisplay(array $data): string
- {
- $result = [];
- foreach ($data as $key => $value) {
- if (is_array($value)) {
- $result[] = $key . ': ' . json_encode($value, JSON_UNESCAPED_UNICODE);
- } elseif (is_object($value)) {
- $result[] = $key . ': ' . json_encode($value, JSON_UNESCAPED_UNICODE);
- } elseif (is_bool($value)) {
- $result[] = $key . ': ' . ($value ? 'true' : 'false');
- } elseif (is_null($value)) {
- $result[] = $key . ': null';
- } else {
- $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;
- }
- }
|