ExportYearDataJob.php 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. <?php
  2. namespace App\Jobs\Export;
  3. use App\Events\SendWebSocketMessageEvent;
  4. use App\Services\Export\ExportYearDataService;
  5. use Exception;
  6. use Illuminate\Contracts\Queue\ShouldQueue;
  7. use Illuminate\Foundation\Queue\Queueable;
  8. use Illuminate\Support\Facades\Log;
  9. class ExportYearDataJob implements ShouldQueue
  10. {
  11. use Queueable;
  12. /**
  13. * Количество попыток выполнения задания.
  14. */
  15. public int $tries = 1;
  16. /**
  17. * Таймаут выполнения задания (30 минут).
  18. */
  19. public int $timeout = 1800;
  20. public function __construct(
  21. private readonly int $year,
  22. private readonly int $userId,
  23. ) {}
  24. public function handle(): void
  25. {
  26. try {
  27. Log::info("ExportYearDataJob started for year {$this->year}");
  28. $service = new ExportYearDataService($this->year, $this->userId);
  29. $downloadLink = $service->handle();
  30. $stats = $service->getStats();
  31. Log::info("ExportYearDataJob completed for year {$this->year}", $stats);
  32. event(new SendWebSocketMessageEvent(
  33. "Экспорт данных за {$this->year} год завершён!",
  34. $this->userId,
  35. [
  36. 'success' => true,
  37. 'download' => $downloadLink,
  38. 'year' => $this->year,
  39. 'stats' => $stats,
  40. ]
  41. ));
  42. } catch (Exception $e) {
  43. Log::error("ExportYearDataJob failed for year {$this->year}: " . $e->getMessage(), [
  44. 'trace' => $e->getTraceAsString()
  45. ]);
  46. event(new SendWebSocketMessageEvent(
  47. "Ошибка экспорта данных за {$this->year} год: " . $e->getMessage(),
  48. $this->userId,
  49. [
  50. 'error' => $e->getMessage(),
  51. 'year' => $this->year,
  52. ]
  53. ));
  54. }
  55. }
  56. }