ImportCatalog.php 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. <?php
  2. namespace App\Jobs\Import;
  3. use App\Events\SendWebSocketMessageEvent;
  4. use App\Services\ImportService;
  5. use Exception;
  6. use Illuminate\Contracts\Queue\ShouldBeUniqueUntilProcessing;
  7. use Illuminate\Contracts\Queue\ShouldQueue;
  8. use Illuminate\Foundation\Queue\Queueable;
  9. use Illuminate\Support\Facades\Log;
  10. class ImportCatalog implements ShouldQueue, ShouldBeUniqueUntilProcessing
  11. {
  12. use Queueable;
  13. /**
  14. * Create a new job instance.
  15. */
  16. public function __construct(private readonly string $path, private readonly int $year, private readonly int $userId)
  17. {
  18. //
  19. }
  20. /**
  21. * @return void
  22. */
  23. public function handle(): void
  24. {
  25. try {
  26. (new ImportService)->handle($this->path, $this->year);
  27. Log::info('ImportCatalog job done!');
  28. event(new SendWebSocketMessageEvent('Импорт завершён!', $this->userId, ['year' => $this->year, 'path' => $this->path]));
  29. } catch (Exception $e) {
  30. Log::info('ImportCatalog job failed!');
  31. event(new SendWebSocketMessageEvent('Ошибка импорта! ' . $e->getMessage(), $this->userId, ['error' => $e->getMessage()]));
  32. }
  33. }
  34. }