GenerateProductionOrderFileJob.php 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Jobs;
  4. use App\Events\SendWebSocketMessageEvent;
  5. use App\Services\ProductionOrderDocumentService;
  6. use Illuminate\Contracts\Queue\ShouldQueue;
  7. use Illuminate\Foundation\Queue\Queueable;
  8. use Illuminate\Support\Facades\Log;
  9. use Throwable;
  10. class GenerateProductionOrderFileJob implements ShouldQueue
  11. {
  12. use Queueable;
  13. public int $timeout = 600;
  14. public int $tries = 2;
  15. /** @param list<int> $itemIds */
  16. public function __construct(
  17. private readonly string $type,
  18. private readonly int $orderId,
  19. private readonly int $userId,
  20. private readonly ?int $relatedId = null,
  21. private readonly array $itemIds = [],
  22. ) {}
  23. public function handle(ProductionOrderDocumentService $service): void
  24. {
  25. try {
  26. $file = $service->generate(
  27. $this->type,
  28. $this->orderId,
  29. $this->userId,
  30. $this->relatedId,
  31. $this->itemIds,
  32. );
  33. event(new SendWebSocketMessageEvent(
  34. 'Файл по заказу готов!',
  35. $this->userId,
  36. ['link' => $file->link],
  37. ));
  38. } catch (Throwable $exception) {
  39. Log::error('Production order file generation failed.', [
  40. 'type' => $this->type,
  41. 'order_id' => $this->orderId,
  42. 'user_id' => $this->userId,
  43. 'error' => $exception->getMessage(),
  44. ]);
  45. event(new SendWebSocketMessageEvent(
  46. 'Ошибка формирования файла: '.$exception->getMessage(),
  47. $this->userId,
  48. ['error' => $exception->getMessage()],
  49. ));
  50. throw $exception;
  51. }
  52. }
  53. }