JobRun.php 5.4 KB

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