SendNotificationQueue.php 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. <?php
  2. namespace App\Module\Notification\Queues;
  3. use App\Module\Notification\Models\NotificationLog;
  4. use App\Module\Notification\Services\NotificationService;
  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. class SendNotificationQueue implements ShouldQueue
  11. {
  12. use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
  13. protected $notification;
  14. /**
  15. * 最大重试次数
  16. *
  17. * @var int
  18. */
  19. public $tries = 3;
  20. /**
  21. * 超时时间
  22. *
  23. * @var int
  24. */
  25. public $timeout = 60;
  26. /**
  27. * 创建队列任务
  28. *
  29. * @param NotificationLog $notification
  30. */
  31. public function __construct(NotificationLog $notification)
  32. {
  33. $this->notification = $notification;
  34. }
  35. /**
  36. * 执行队列任务
  37. *
  38. * @param NotificationService $notificationService
  39. * @return void
  40. */
  41. public function handle(NotificationService $notificationService): void
  42. {
  43. $notificationService->handleNotification($this->notification);
  44. }
  45. /**
  46. * 处理失败的任务
  47. *
  48. * @param \Throwable $exception
  49. * @return void
  50. */
  51. public function failed(\Throwable $exception): void
  52. {
  53. $this->notification->update([
  54. 'status' => 'FAILED',
  55. 'message' => $exception->getMessage()
  56. ]);
  57. }
  58. }