Job.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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 array $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. protected $fillable = [
  49. 'id',
  50. 'queue',
  51. 'payload',
  52. 'attempts',
  53. 'reserved_at',
  54. 'available_at',
  55. ];
  56. // attrlist end
  57. /**
  58. * 获取队列名称访问器
  59. */
  60. public function getQueueNameAttribute(): string
  61. {
  62. return $this->queue ?? 'default';
  63. }
  64. /**
  65. * 获取格式化的创建时间
  66. */
  67. public function getCreatedAtFormattedAttribute(): string
  68. {
  69. return $this->created_at ? date('Y-m-d H:i:s', $this->created_at) : '';
  70. }
  71. /**
  72. * 获取格式化的可用时间
  73. */
  74. public function getAvailableAtFormattedAttribute(): string
  75. {
  76. return $this->available_at ? date('Y-m-d H:i:s', $this->available_at) : '';
  77. }
  78. /**
  79. * 获取格式化的保留时间
  80. */
  81. public function getReservedAtFormattedAttribute(): string
  82. {
  83. return $this->reserved_at ? date('Y-m-d H:i:s', $this->reserved_at) : '';
  84. }
  85. /**
  86. * 获取任务状态
  87. */
  88. public function getStatusAttribute(): string
  89. {
  90. $now = time();
  91. if ($this->reserved_at && $this->reserved_at > $now) {
  92. return '已保留';
  93. }
  94. if ($this->available_at > $now) {
  95. return '延迟中';
  96. }
  97. return '待处理';
  98. }
  99. /**
  100. * 获取任务类名
  101. */
  102. public function getJobClassAttribute(): string
  103. {
  104. if (!$this->payload) {
  105. return '';
  106. }
  107. $payload = is_array($this->payload) ? $this->payload : json_decode($this->payload, true);
  108. return $payload['displayName'] ?? $payload['job'] ?? '';
  109. }
  110. /**
  111. * 获取任务参数 - 直接反序列化payload原样展示
  112. */
  113. public function getJobParametersAttribute(): string
  114. {
  115. if (!$this->payload) {
  116. return '无参数';
  117. }
  118. $payload = is_array($this->payload) ? $this->payload : json_decode($this->payload, true);
  119. if (!is_array($payload)) {
  120. // 如果JSON解码失败,尝试其他格式
  121. if (is_string($this->payload)) {
  122. return $this->payload;
  123. }
  124. return '无参数';
  125. }
  126. return $this->formatArrayForDisplay($payload);
  127. }
  128. /**
  129. * 格式化数组为显示字符串
  130. */
  131. private function formatArrayForDisplay(array $data): string
  132. {
  133. $result = [];
  134. foreach ($data as $key => $value) {
  135. if (is_array($value)) {
  136. $result[] = $key . ': ' . json_encode($value, JSON_UNESCAPED_UNICODE);
  137. } elseif (is_object($value)) {
  138. $result[] = $key . ': ' . json_encode($value, JSON_UNESCAPED_UNICODE);
  139. } elseif (is_bool($value)) {
  140. $result[] = $key . ': ' . ($value ? 'true' : 'false');
  141. } elseif (is_null($value)) {
  142. $result[] = $key . ': null';
  143. } else {
  144. $result[] = $key . ': ' . $value;
  145. }
  146. }
  147. return implode(', ', $result);
  148. }
  149. /**
  150. * 获取简化的任务参数(用于列表显示)
  151. */
  152. public function getJobParametersShortAttribute(): string
  153. {
  154. $params = $this->job_parameters;
  155. if (mb_strlen($params) > 50) {
  156. return mb_substr($params, 0, 50) . '...';
  157. }
  158. return $params;
  159. }
  160. }