| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697 |
- <?php
- namespace App\Module\Ulogic\Queues;
- use App\Module\Ulogic\Models\AppMessage;
- use App\Module\Ulogic\Services\AppMessageService;
- use Illuminate\Bus\Queueable;
- use Illuminate\Contracts\Queue\ShouldQueue;
- use Illuminate\Foundation\Bus\Dispatchable;
- use Illuminate\Queue\InteractsWithQueue;
- use Illuminate\Queue\SerializesModels;
- /**
- * 应用消息发送队列任务类
- *
- * 该类负责异步处理消息的发送,支持失败重试和超时控制
- * 使用Laravel的队列系统实现异步发送,避免阻塞主流程
- */
- class SendAppMessageQueue implements ShouldQueue
- {
- use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
- /**
- * 待发送的消息实例
- *
- * @var AppMessage
- */
- protected $message;
- /**
- * 最大重试次数
- *
- * 当任务执行失败时,系统会自动重试,最多重试3次
- *
- * @var int
- */
- public $tries = 3;
- /**
- * 任务超时时间(秒)
- *
- * 如果任务执行超过60秒仍未完成,系统会认为任务超时并终止执行
- *
- * @var int
- */
- public $timeout = 60;
- /**
- * 创建队列任务实例
- *
- * @param AppMessage $message 需要发送的消息实例
- */
- public function __construct(AppMessage $message)
- {
- $this->message = $message;
- }
- /**
- * 执行队列任务
- *
- * 该方法由队列工作进程调用,用于处理实际的消息发送逻辑
- * 可以在这里实现:
- * 1. 发送WebSocket实时通知
- * 2. 推送APP消息
- * 3. 发送邮件通知
- * 4. 更新消息状态
- * 等具体的业务逻辑
- *
- * @param AppMessageService $messageService 消息服务实例,由Laravel容器自动注入
- * @return void
- */
- public function handle(AppMessageService $messageService): void
- {
- // TODO: 实现消息发送逻辑
- // 例如:发送WebSocket通知、更新消息状态等
- }
- /**
- * 处理失败的任务
- *
- * 当任务执行失败或发生异常时会调用此方法
- * 可以在这里实现:
- * 1. 记录错误日志
- * 2. 发送失败通知
- * 3. 更新消息状态为发送失败
- * 4. 触发告警
- * 等失败处理逻辑
- *
- * @param \Throwable $exception 导致任务失败的异常实例
- * @return void
- */
- public function failed(\Throwable $exception): void
- {
- // TODO: 实现失败处理逻辑
- // 例如:记录失败日志、更新消息状态等
- }
- }
|