FailedJob.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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 $uuid 唯一标识
  10. * @property string $connection 连接名称
  11. * @property string $queue 队列名称
  12. * @property string $payload 任务载荷
  13. * @property string $exception 异常信息
  14. * @property \Carbon\Carbon $failed_at 失败时间
  15. * field end
  16. *
  17. */
  18. class FailedJob extends ModelCore
  19. {
  20. protected $table = 'failed_jobs';
  21. public $timestamps = false;
  22. protected $fillable = [
  23. 'uuid',
  24. 'connection',
  25. 'queue',
  26. 'payload',
  27. 'exception',
  28. 'failed_at'
  29. ];
  30. protected $casts = [
  31. 'payload' => 'array',
  32. 'failed_at' => 'datetime'
  33. ];
  34. protected $appends = [
  35. 'queue_name',
  36. 'connection_name',
  37. 'job_class',
  38. 'exception_class',
  39. 'exception_message',
  40. 'failed_at_formatted',
  41. 'exception_stack_trace'
  42. ];
  43. //attrlist start
  44. public static $attrlist = [
  45. 'id',
  46. 'uuid',
  47. 'connection',
  48. 'queue',
  49. 'payload',
  50. 'exception',
  51. 'failed_at'
  52. ];
  53. //attrlist end
  54. /**
  55. * 获取队列名称访问器
  56. */
  57. public function getQueueNameAttribute(): string
  58. {
  59. return $this->queue ?? 'default';
  60. }
  61. /**
  62. * 获取连接名称访问器
  63. */
  64. public function getConnectionNameAttribute(): string
  65. {
  66. return $this->connection ?? 'default';
  67. }
  68. /**
  69. * 获取任务类名
  70. */
  71. public function getJobClassAttribute(): string
  72. {
  73. if (!$this->payload) {
  74. return '';
  75. }
  76. $payload = is_array($this->payload) ? $this->payload : json_decode($this->payload, true);
  77. return $payload['displayName'] ?? $payload['job'] ?? '';
  78. }
  79. /**
  80. * 获取异常类名
  81. */
  82. public function getExceptionClassAttribute(): string
  83. {
  84. if (!$this->exception) {
  85. return '';
  86. }
  87. // 从异常信息中提取异常类名
  88. if (preg_match('/^([^\s:]+)/', $this->exception, $matches)) {
  89. return $matches[1];
  90. }
  91. return '';
  92. }
  93. /**
  94. * 获取异常消息
  95. */
  96. public function getExceptionMessageAttribute(): string
  97. {
  98. if (!$this->exception) {
  99. return '';
  100. }
  101. // 从异常信息中提取异常消息
  102. $lines = explode("\n", $this->exception);
  103. if (count($lines) > 0) {
  104. // 第一行通常包含异常类和消息
  105. $firstLine = $lines[0];
  106. if (strpos($firstLine, ':') !== false) {
  107. return trim(substr($firstLine, strpos($firstLine, ':') + 1));
  108. }
  109. }
  110. return '';
  111. }
  112. /**
  113. * 获取格式化的失败时间
  114. */
  115. public function getFailedAtFormattedAttribute(): string
  116. {
  117. return $this->failed_at ? $this->failed_at->format('Y-m-d H:i:s') : '';
  118. }
  119. /**
  120. * 获取异常堆栈跟踪(前几行)
  121. */
  122. public function getExceptionStackTraceAttribute(): string
  123. {
  124. if (!$this->exception) {
  125. return '';
  126. }
  127. $lines = explode("\n", $this->exception);
  128. // 返回前5行堆栈跟踪
  129. return implode("\n", array_slice($lines, 0, 5));
  130. }
  131. }