ExportMafRegistryJob.php 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. <?php
  2. namespace App\Jobs;
  3. use App\Events\SendWebSocketMessageEvent;
  4. use App\Services\ExportMafRegistryService;
  5. use Illuminate\Contracts\Queue\ShouldQueue;
  6. use Illuminate\Foundation\Queue\Queueable;
  7. use Illuminate\Support\Facades\Log;
  8. use Throwable;
  9. class ExportMafRegistryJob implements ShouldQueue
  10. {
  11. use Queueable;
  12. public function __construct(
  13. private readonly int $userId,
  14. private readonly string $updNumber,
  15. private readonly int $year,
  16. ) {
  17. }
  18. public function handle(): void
  19. {
  20. try {
  21. $link = (new ExportMafRegistryService())->handle($this->userId, $this->updNumber, $this->year);
  22. Log::info('ExportMafRegistry job done!');
  23. event(new SendWebSocketMessageEvent('Реестр на оплату сформирован!', $this->userId, ['success' => true, 'link' => $link]));
  24. } catch (Throwable $e) {
  25. $this->notifyFailure($e);
  26. }
  27. }
  28. public function failed(Throwable $e): void
  29. {
  30. $this->notifyFailure($e);
  31. }
  32. private function notifyFailure(Throwable $e): void
  33. {
  34. Log::error('ExportMafRegistry job failed.', [
  35. 'user_id' => $this->userId,
  36. 'upd_number' => $this->updNumber,
  37. 'year' => $this->year,
  38. 'exception' => $e,
  39. ]);
  40. event(new SendWebSocketMessageEvent(
  41. 'Ошибка формирования реестра на оплату! ' . $e->getMessage(),
  42. $this->userId,
  43. ['error' => $e->getMessage()]
  44. ));
  45. }
  46. }