| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320 |
- <?php
- namespace App\Module\System\Models;
- use UCore\ModelCore;
- /**
- * 队列运行记录模型
- *
- * field start
- * @property int $id
- * @property string $queue 队列名称
- * @property string $payload 任务载荷
- * @property string $runclass 运行类
- * @property int $attempts 尝试次数
- * @property int $reserved_at 保留时间
- * @property int $available_at 可用时间
- * @property int $created_at 创建时间
- * @property string $status 运行状态
- * @property string $desc 描述信息
- * @property float $runtime 运行时间
- * field end
- *
- */
- class JobRun extends ModelCore
- {
- protected $table = 'job_runs';
-
- public $timestamps = false;
- protected $fillable = [
- 'queue',
- 'payload',
- 'runclass',
- 'attempts',
- 'reserved_at',
- 'available_at',
- 'created_at',
- 'status',
- 'desc',
- 'runtime'
- ];
- protected $casts = [
- 'payload' => 'array',
- 'attempts' => 'integer',
- 'reserved_at' => 'integer',
- 'available_at' => 'integer',
- 'created_at' => 'integer',
- 'runtime' => 'float'
- ];
- protected $appends = [
- 'queue_name',
- 'run_class_name',
- 'created_at_formatted',
- 'available_at_formatted',
- 'reserved_at_formatted',
- 'status_label',
- 'status_color',
- 'runtime_formatted',
- 'job_class',
- 'desc_short',
- 'job_parameters',
- 'job_parameters_short'
- ];
- //attrlist start
- public static $attrlist = [
- 'id',
- 'queue',
- 'payload',
- 'runclass',
- 'attempts',
- 'reserved_at',
- 'available_at',
- 'created_at',
- 'status',
- 'desc',
- 'runtime'
- ];
- //attrlist end
- /**
- * 获取队列名称访问器
- */
- public function getQueueNameAttribute(): string
- {
- return $this->queue ?? 'default';
- }
- /**
- * 获取运行类名访问器
- */
- public function getRunClassNameAttribute(): string
- {
- if (!$this->runclass) {
- return '';
- }
-
- // 提取类名(去掉命名空间)
- $parts = explode('\\', $this->runclass);
- return end($parts);
- }
- /**
- * 获取格式化的创建时间
- */
- public function getCreatedAtFormattedAttribute(): string
- {
- return $this->created_at ? date('Y-m-d H:i:s', $this->created_at) : '';
- }
- /**
- * 获取格式化的可用时间
- */
- public function getAvailableAtFormattedAttribute(): string
- {
- return $this->available_at ? date('Y-m-d H:i:s', $this->available_at) : '';
- }
- /**
- * 获取格式化的保留时间
- */
- public function getReservedAtFormattedAttribute(): string
- {
- return $this->reserved_at ? date('Y-m-d H:i:s', $this->reserved_at) : '';
- }
- /**
- * 获取状态标签
- */
- public function getStatusLabelAttribute(): string
- {
- $status = $this->status;
- // 处理带数字后缀的状态
- if (strpos($status, 'runend-') === 0) {
- return '已完成';
- }
- if (strpos($status, 'run-') === 0) {
- return '运行中';
- }
- switch ($status) {
- case 'success':
- case 'run-end':
- return '已完成';
- case 'failed':
- case 'error':
- return '失败';
- case 'running':
- case 'run':
- return '运行中';
- case 'pending':
- case 'wait':
- return '待处理';
- default:
- return $status ?? '未知';
- }
- }
- /**
- * 获取状态颜色
- */
- public function getStatusColorAttribute(): string
- {
- $status = $this->status;
- // 处理带数字后缀的状态
- if (strpos($status, 'runend-') === 0) {
- return 'success';
- }
- if (strpos($status, 'run-') === 0) {
- return 'warning';
- }
- switch ($status) {
- case 'success':
- case 'run-end':
- return 'success';
- case 'failed':
- case 'error':
- return 'danger';
- case 'running':
- case 'run':
- return 'warning';
- case 'pending':
- case 'wait':
- return 'info';
- default:
- return 'secondary';
- }
- }
- /**
- * 获取格式化的运行时间
- */
- public function getRuntimeFormattedAttribute(): string
- {
- if (!$this->runtime) {
- return '';
- }
-
- if ($this->runtime < 1) {
- return round($this->runtime * 1000, 2) . 'ms';
- } else {
- return round($this->runtime, 3) . 's';
- }
- }
- /**
- * 获取任务类名(从payload中提取)
- */
- public function getJobClassAttribute(): string
- {
- if (!$this->payload) {
- return $this->runclass ?? '';
- }
-
- $payload = is_array($this->payload) ? $this->payload : json_decode($this->payload, true);
- return $payload['displayName'] ?? $payload['job'] ?? $this->runclass ?? '';
- }
- /**
- * 获取描述信息(截断)
- */
- public function getDescShortAttribute(): string
- {
- if (!$this->desc) {
- return '';
- }
- return mb_strlen($this->desc) > 100 ? mb_substr($this->desc, 0, 100) . '...' : $this->desc;
- }
- /**
- * 获取任务参数 - 直接反序列化payload原样展示
- */
- public function getJobParametersAttribute(): string
- {
- if (!$this->payload) {
- return '无参数';
- }
- // 尝试反序列化payload
- $payload = $this->payload;
- // 如果是字符串,尝试反序列化
- if (is_string($payload)) {
- // 去掉外层引号
- $payload = trim($payload, '"');
- // 尝试反序列化PHP数组
- if (strpos($payload, 'a:') === 0) {
- try {
- $unserialized = unserialize($payload);
- if (is_array($unserialized)) {
- return $this->formatArrayForDisplay($unserialized);
- }
- } catch (\Exception) {
- // 反序列化失败,返回原始字符串
- return $payload;
- }
- } else {
- // 尝试JSON解码
- $decoded = json_decode($payload, true);
- if (json_last_error() === JSON_ERROR_NONE) {
- return $this->formatArrayForDisplay($decoded);
- }
- }
- }
- // 如果已经是数组,直接格式化
- if (is_array($payload)) {
- return $this->formatArrayForDisplay($payload);
- }
- // 其他情况返回原始内容
- return is_scalar($payload) ? (string)$payload : json_encode($payload);
- }
- /**
- * 格式化数组为显示字符串
- */
- private function formatArrayForDisplay(array $data): string
- {
- $result = [];
- foreach ($data as $key => $value) {
- if (is_array($value)) {
- $result[] = $key . ': ' . json_encode($value, JSON_UNESCAPED_UNICODE);
- } elseif (is_object($value)) {
- $result[] = $key . ': ' . json_encode($value, JSON_UNESCAPED_UNICODE);
- } elseif (is_bool($value)) {
- $result[] = $key . ': ' . ($value ? 'true' : 'false');
- } elseif (is_null($value)) {
- $result[] = $key . ': null';
- } else {
- $result[] = $key . ': ' . $value;
- }
- }
- return implode(', ', $result);
- }
- /**
- * 获取简化的任务参数(用于列表显示)
- */
- public function getJobParametersShortAttribute(): string
- {
- $params = $this->job_parameters;
- if (mb_strlen($params) > 50) {
- return mb_substr($params, 0, 50) . '...';
- }
- return $params;
- }
- }
|