JobRun.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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 string $runclass 运行类
  12. * @property int $attempts
  13. * @property int $reserved_at
  14. * @property int $available_at
  15. * @property int $created_at
  16. * @property string $status 运行状态
  17. * @property string $desc 描述信息
  18. * @property float $runtime 运行时间
  19. * field end
  20. *
  21. */
  22. class JobRun extends ModelCore
  23. {
  24. protected $table = 'job_runs';
  25. public $timestamps = false;
  26. protected $casts = [
  27. 'payload' => 'array',
  28. 'attempts' => 'integer',
  29. 'reserved_at' => 'integer',
  30. 'available_at' => 'integer',
  31. 'created_at' => 'integer',
  32. 'runtime' => 'float'
  33. ];
  34. protected $appends = [
  35. 'queue_name', 'runtime_formatted',
  36. 'job_class',
  37. 'desc_short',
  38. 'job_parameters',
  39. 'job_parameters_short'
  40. ];
  41. // attrlist start
  42. protected $fillable = [
  43. 'id',
  44. 'queue',
  45. 'payload',
  46. 'runclass',
  47. 'attempts',
  48. 'reserved_at',
  49. 'available_at',
  50. 'status',
  51. 'desc',
  52. 'runtime',
  53. ];
  54. // attrlist end
  55. /**
  56. * 获取队列名称访问器
  57. */
  58. public function getQueueNameAttribute(): string
  59. {
  60. return $this->queue ?? 'default';
  61. }
  62. /**
  63. * 获取格式化的运行时间
  64. */
  65. public function getRuntimeFormattedAttribute(): string
  66. {
  67. if (!$this->runtime) {
  68. return '';
  69. }
  70. if ($this->runtime < 1) {
  71. return round($this->runtime * 1000, 2) . 'ms';
  72. } else {
  73. return round($this->runtime, 3) . 's';
  74. }
  75. }
  76. /**
  77. * 获取任务类名(从payload中提取)
  78. */
  79. public function getJobClassAttribute(): string
  80. {
  81. if (!$this->payload) {
  82. return $this->runclass ?? '';
  83. }
  84. $payload = is_array($this->payload) ? $this->payload : json_decode($this->payload, true);
  85. return $payload['displayName'] ?? $payload['job'] ?? $this->runclass ?? '';
  86. }
  87. /**
  88. * 获取描述信息(截断)
  89. */
  90. public function getDescShortAttribute(): string
  91. {
  92. if (!$this->desc) {
  93. return '';
  94. }
  95. return mb_strlen($this->desc) > 100 ? mb_substr($this->desc, 0, 100) . '...' : $this->desc;
  96. }
  97. /**
  98. * 获取任务参数 - 直接反序列化payload原样展示
  99. */
  100. public function getJobParametersAttribute(): string
  101. {
  102. if (!$this->payload) {
  103. return '无参数';
  104. }
  105. // 尝试反序列化payload
  106. $payload = $this->payload;
  107. // 如果是字符串,尝试反序列化
  108. if (is_string($payload)) {
  109. // 去掉外层引号
  110. $payload = trim($payload, '"');
  111. // 尝试反序列化PHP数组
  112. if (strpos($payload, 'a:') === 0) {
  113. try {
  114. $unserialized = unserialize($payload);
  115. if (is_array($unserialized)) {
  116. return $this->formatArrayForDisplay($unserialized);
  117. }
  118. } catch (\Exception) {
  119. // 反序列化失败,返回原始字符串
  120. return $payload;
  121. }
  122. } else {
  123. // 尝试JSON解码
  124. $decoded = json_decode($payload, true);
  125. if (json_last_error() === JSON_ERROR_NONE) {
  126. return $this->formatArrayForDisplay($decoded);
  127. }
  128. }
  129. }
  130. // 如果已经是数组,直接格式化
  131. if (is_array($payload)) {
  132. return $this->formatArrayForDisplay($payload);
  133. }
  134. // 其他情况返回原始内容
  135. return is_scalar($payload) ? (string)$payload : json_encode($payload);
  136. }
  137. /**
  138. * 格式化数组为显示字符串
  139. */
  140. private function formatArrayForDisplay(array $data): string
  141. {
  142. return json_encode($data, JSON_UNESCAPED_UNICODE);
  143. $result = [];
  144. foreach ($data as $key => $value) {
  145. if (is_array($value)) {
  146. $result[] = $key . ': ' . json_encode($value, JSON_UNESCAPED_UNICODE);
  147. } elseif (is_object($value)) {
  148. $result[] = $key . ': ' . json_encode($value, JSON_UNESCAPED_UNICODE);
  149. } elseif (is_bool($value)) {
  150. $result[] = $key . ': ' . ($value ? 'true' : 'false');
  151. } elseif (is_null($value)) {
  152. $result[] = $key . ': null';
  153. } else {
  154. $result[] = $key . ': ' . $value;
  155. }
  156. }
  157. return implode(', ', $result);
  158. }
  159. /**
  160. * 获取简化的任务参数(用于列表显示)
  161. */
  162. public function getJobParametersShortAttribute(): string
  163. {
  164. $params = $this->job_parameters;
  165. if (mb_strlen($params) > 50) {
  166. return mb_substr($params, 0, 50) . '...';
  167. }
  168. return $params;
  169. }
  170. }