JobRun.php 5.3 KB

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