| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667 |
- <?php
- namespace App\Module\Notification\Queues;
- use App\Module\Notification\Models\NotificationLog;
- use App\Module\Notification\Services\NotificationService;
- use Illuminate\Bus\Queueable;
- use Illuminate\Contracts\Queue\ShouldQueue;
- use Illuminate\Foundation\Bus\Dispatchable;
- use Illuminate\Queue\InteractsWithQueue;
- use Illuminate\Queue\SerializesModels;
- class SendNotificationQueue implements ShouldQueue
- {
- use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
- protected $notification;
- /**
- * 最大重试次数
- *
- * @var int
- */
- public $tries = 3;
- /**
- * 超时时间
- *
- * @var int
- */
- public $timeout = 60;
- /**
- * 创建队列任务
- *
- * @param NotificationLog $notification
- */
- public function __construct(NotificationLog $notification)
- {
- $this->notification = $notification;
- }
- /**
- * 执行队列任务
- *
- * @param NotificationService $notificationService
- * @return void
- */
- public function handle(NotificationService $notificationService): void
- {
- $notificationService->handleNotification($this->notification);
- }
- /**
- * 处理失败的任务
- *
- * @param \Throwable $exception
- * @return void
- */
- public function failed(\Throwable $exception): void
- {
- $this->notification->update([
- 'status' => 'FAILED',
- 'message' => $exception->getMessage()
- ]);
- }
- }
|