Job.php 4.2 KB

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