ExportProductionOrdersJob.php 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Jobs\Export;
  4. use App\Events\SendWebSocketMessageEvent;
  5. use App\Services\Export\ExportProductionOrdersService;
  6. use Illuminate\Contracts\Queue\ShouldQueue;
  7. use Illuminate\Foundation\Queue\Queueable;
  8. use Illuminate\Support\Facades\Log;
  9. use Throwable;
  10. class ExportProductionOrdersJob implements ShouldQueue
  11. {
  12. use Queueable;
  13. public int $timeout = 600;
  14. public int $tries = 2;
  15. /** @param list<int> $orderIds */
  16. public function __construct(
  17. public readonly array $orderIds,
  18. public readonly int $userId,
  19. ) {}
  20. public function handle(ExportProductionOrdersService $service): void
  21. {
  22. try {
  23. $file = $service->handle($this->orderIds, $this->userId);
  24. event(new SendWebSocketMessageEvent(
  25. 'Экспорт графика заказов завершён!',
  26. $this->userId,
  27. ['link' => $file->link],
  28. ));
  29. } catch (Throwable $exception) {
  30. Log::error('Production orders export failed.', [
  31. 'user_id' => $this->userId,
  32. 'orders_count' => count($this->orderIds),
  33. 'error' => $exception->getMessage(),
  34. ]);
  35. event(new SendWebSocketMessageEvent(
  36. 'Ошибка экспорта графика заказов: '.$exception->getMessage(),
  37. $this->userId,
  38. ['error' => $exception->getMessage()],
  39. ));
  40. throw $exception;
  41. }
  42. }
  43. }