ExportTechnicalDescriptionsJob.php 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Jobs\Export;
  4. use App\Events\SendWebSocketMessageEvent;
  5. use App\Services\Export\ExportTechnicalDescriptionsService;
  6. use Illuminate\Contracts\Queue\ShouldQueue;
  7. use Illuminate\Foundation\Queue\Queueable;
  8. use Illuminate\Support\Facades\Log;
  9. use Throwable;
  10. class ExportTechnicalDescriptionsJob 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 array $itemIds,
  18. private readonly string $descriptionMode,
  19. private readonly bool $separateDocuments,
  20. private readonly int $userId,
  21. ) {}
  22. public function handle(ExportTechnicalDescriptionsService $service): void
  23. {
  24. try {
  25. $file = $service->handle(
  26. $this->itemIds,
  27. $this->descriptionMode,
  28. $this->separateDocuments,
  29. $this->userId,
  30. );
  31. event(new SendWebSocketMessageEvent(
  32. 'Экспорт технических описаний завершён!',
  33. $this->userId,
  34. ['link' => $file->link],
  35. ));
  36. } catch (Throwable $exception) {
  37. Log::error('Technical descriptions export failed.', [
  38. 'user_id' => $this->userId,
  39. 'item_ids' => $this->itemIds,
  40. 'error' => $exception->getMessage(),
  41. ]);
  42. event(new SendWebSocketMessageEvent(
  43. 'Ошибка экспорта технических описаний: '.$exception->getMessage(),
  44. $this->userId,
  45. ['error' => $exception->getMessage()],
  46. ));
  47. throw $exception;
  48. }
  49. }
  50. }