Job.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. <?php
  2. namespace App\Module\System\Models;
  3. use UCore\ModelCore;
  4. /**
  5. * 队列任务模型
  6. *
  7. * field start
  8. * @property int $id
  9. * @property string $queue 队列名称
  10. * @property string $payload 任务载荷
  11. * @property int $attempts 尝试次数
  12. * @property int $reserved_at 保留时间
  13. * @property int $available_at 可用时间
  14. * @property int $created_at 创建时间
  15. * field end
  16. *
  17. */
  18. class Job extends ModelCore
  19. {
  20. protected $table = 'jobs';
  21. public $timestamps = false;
  22. protected $fillable = [
  23. 'queue',
  24. 'payload',
  25. 'attempts',
  26. 'reserved_at',
  27. 'available_at',
  28. 'created_at'
  29. ];
  30. protected $casts = [
  31. 'payload' => 'array',
  32. 'attempts' => 'integer',
  33. 'reserved_at' => 'integer',
  34. 'available_at' => 'integer',
  35. 'created_at' => 'integer'
  36. ];
  37. protected $appends = [
  38. 'queue_name',
  39. 'created_at_formatted',
  40. 'available_at_formatted',
  41. 'reserved_at_formatted',
  42. 'status',
  43. 'job_class',
  44. 'job_parameters',
  45. 'job_parameters_short'
  46. ];
  47. //attrlist start
  48. public static $attrlist = [
  49. 'id',
  50. 'queue',
  51. 'payload',
  52. 'attempts',
  53. 'reserved_at',
  54. 'available_at',
  55. 'created_at'
  56. ];
  57. //attrlist end
  58. /**
  59. * 获取队列名称访问器
  60. */
  61. public function getQueueNameAttribute(): string
  62. {
  63. return $this->queue ?? 'default';
  64. }
  65. /**
  66. * 获取格式化的创建时间
  67. */
  68. public function getCreatedAtFormattedAttribute(): string
  69. {
  70. return $this->created_at ? date('Y-m-d H:i:s', $this->created_at) : '';
  71. }
  72. /**
  73. * 获取格式化的可用时间
  74. */
  75. public function getAvailableAtFormattedAttribute(): string
  76. {
  77. return $this->available_at ? date('Y-m-d H:i:s', $this->available_at) : '';
  78. }
  79. /**
  80. * 获取格式化的保留时间
  81. */
  82. public function getReservedAtFormattedAttribute(): string
  83. {
  84. return $this->reserved_at ? date('Y-m-d H:i:s', $this->reserved_at) : '';
  85. }
  86. /**
  87. * 获取任务状态
  88. */
  89. public function getStatusAttribute(): string
  90. {
  91. $now = time();
  92. if ($this->reserved_at && $this->reserved_at > $now) {
  93. return '已保留';
  94. }
  95. if ($this->available_at > $now) {
  96. return '延迟中';
  97. }
  98. return '待处理';
  99. }
  100. /**
  101. * 获取任务类名
  102. */
  103. public function getJobClassAttribute(): string
  104. {
  105. if (!$this->payload) {
  106. return '';
  107. }
  108. $payload = is_array($this->payload) ? $this->payload : json_decode($this->payload, true);
  109. return $payload['displayName'] ?? $payload['job'] ?? '';
  110. }
  111. /**
  112. * 获取任务参数 - 直接反序列化payload原样展示
  113. */
  114. public function getJobParametersAttribute(): string
  115. {
  116. if (!$this->payload) {
  117. return '无参数';
  118. }
  119. $payload = is_array($this->payload) ? $this->payload : json_decode($this->payload, true);
  120. if (!is_array($payload)) {
  121. // 如果JSON解码失败,尝试其他格式
  122. if (is_string($this->payload)) {
  123. return $this->payload;
  124. }
  125. return '无参数';
  126. }
  127. return $this->formatArrayForDisplay($payload);
  128. }
  129. /**
  130. * 格式化数组为显示字符串
  131. */
  132. private function formatArrayForDisplay(array $data): string
  133. {
  134. $result = [];
  135. foreach ($data as $key => $value) {
  136. if (is_array($value)) {
  137. $result[] = $key . ': ' . json_encode($value, JSON_UNESCAPED_UNICODE);
  138. } elseif (is_object($value)) {
  139. $result[] = $key . ': ' . json_encode($value, JSON_UNESCAPED_UNICODE);
  140. } elseif (is_bool($value)) {
  141. $result[] = $key . ': ' . ($value ? 'true' : 'false');
  142. } elseif (is_null($value)) {
  143. $result[] = $key . ': null';
  144. } else {
  145. $result[] = $key . ': ' . $value;
  146. }
  147. }
  148. return implode(', ', $result);
  149. }
  150. /**
  151. * 获取简化的任务参数(用于列表显示)
  152. */
  153. public function getJobParametersShortAttribute(): string
  154. {
  155. $params = $this->job_parameters;
  156. if (mb_strlen($params) > 50) {
  157. return mb_substr($params, 0, 50) . '...';
  158. }
  159. return $params;
  160. }
  161. }