Job.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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. * 获取任务参数
  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. // 提取有用的参数信息
  121. $params = [];
  122. if (isset($payload['maxTries'])) {
  123. $params['最大尝试次数'] = $payload['maxTries'];
  124. }
  125. if (isset($payload['timeout'])) {
  126. $params['超时时间'] = $payload['timeout'] . 's';
  127. }
  128. if (isset($payload['data']['command'])) {
  129. // 尝试解析序列化的命令参数
  130. $command = $payload['data']['command'];
  131. if (strpos($command, 'O:') === 0) {
  132. // 这是序列化的对象,暂时不解析
  133. $params['命令类型'] = '序列化对象';
  134. }
  135. }
  136. if (empty($params)) {
  137. return '无参数';
  138. }
  139. $result = [];
  140. foreach ($params as $key => $value) {
  141. $result[] = "{$key}: {$value}";
  142. }
  143. return implode(', ', $result);
  144. }
  145. /**
  146. * 获取简化的任务参数(用于列表显示)
  147. */
  148. public function getJobParametersShortAttribute(): string
  149. {
  150. $params = $this->job_parameters;
  151. if (mb_strlen($params) > 50) {
  152. return mb_substr($params, 0, 50) . '...';
  153. }
  154. return $params;
  155. }
  156. }