notification = $notification; parent::__construct(['notification_id' => $notification->id]); } /** * 执行队列任务 * * @return bool */ public function run(): bool { try { // 获取通知服务实例 $notificationService = app(NotificationService::class); // 处理通知发送 $notificationService->handleNotification($this->notification); return true; } catch (\Exception $e) { // 记录错误日志 Log::error('发送通知失败', [ 'notification_id' => $this->notification->id, 'error' => $e->getMessage(), 'trace' => $e->getTraceAsString() ]); return false; } } /** * 获取任务数据 * * @return array */ public function payload() { return [ 'notification_id' => $this->notification->id, 'notification_data' => $this->notification->toArray() ]; } /** * 处理失败的任务 * * @param \Throwable $exception * @return void */ public function failed(\Throwable $exception): void { $this->notification->update([ 'status' => 'FAILED', 'message' => $exception->getMessage() ]); } }