SendAppMessageQueue.php 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <?php
  2. namespace App\Module\Ulogic\Queues;
  3. use App\Module\Ulogic\Models\AppMessage;
  4. use App\Module\Ulogic\Services\AppMessageService;
  5. use Illuminate\Bus\Queueable;
  6. use Illuminate\Contracts\Queue\ShouldQueue;
  7. use Illuminate\Foundation\Bus\Dispatchable;
  8. use Illuminate\Queue\InteractsWithQueue;
  9. use Illuminate\Queue\SerializesModels;
  10. /**
  11. * 应用消息发送队列任务类
  12. *
  13. * 该类负责异步处理消息的发送,支持失败重试和超时控制
  14. * 使用Laravel的队列系统实现异步发送,避免阻塞主流程
  15. */
  16. class SendAppMessageQueue implements ShouldQueue
  17. {
  18. use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
  19. /**
  20. * 待发送的消息实例
  21. *
  22. * @var AppMessage
  23. */
  24. protected $message;
  25. /**
  26. * 最大重试次数
  27. *
  28. * 当任务执行失败时,系统会自动重试,最多重试3次
  29. *
  30. * @var int
  31. */
  32. public $tries = 3;
  33. /**
  34. * 任务超时时间(秒)
  35. *
  36. * 如果任务执行超过60秒仍未完成,系统会认为任务超时并终止执行
  37. *
  38. * @var int
  39. */
  40. public $timeout = 60;
  41. /**
  42. * 创建队列任务实例
  43. *
  44. * @param AppMessage $message 需要发送的消息实例
  45. */
  46. public function __construct(AppMessage $message)
  47. {
  48. $this->message = $message;
  49. }
  50. /**
  51. * 执行队列任务
  52. *
  53. * 该方法由队列工作进程调用,用于处理实际的消息发送逻辑
  54. * 可以在这里实现:
  55. * 1. 发送WebSocket实时通知
  56. * 2. 推送APP消息
  57. * 3. 发送邮件通知
  58. * 4. 更新消息状态
  59. * 等具体的业务逻辑
  60. *
  61. * @param AppMessageService $messageService 消息服务实例,由Laravel容器自动注入
  62. * @return void
  63. */
  64. public function handle(AppMessageService $messageService): void
  65. {
  66. // TODO: 实现消息发送逻辑
  67. // 例如:发送WebSocket通知、更新消息状态等
  68. }
  69. /**
  70. * 处理失败的任务
  71. *
  72. * 当任务执行失败或发生异常时会调用此方法
  73. * 可以在这里实现:
  74. * 1. 记录错误日志
  75. * 2. 发送失败通知
  76. * 3. 更新消息状态为发送失败
  77. * 4. 触发告警
  78. * 等失败处理逻辑
  79. *
  80. * @param \Throwable $exception 导致任务失败的异常实例
  81. * @return void
  82. */
  83. public function failed(\Throwable $exception): void
  84. {
  85. // TODO: 实现失败处理逻辑
  86. // 例如:记录失败日志、更新消息状态等
  87. }
  88. }