| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576 |
- <?php
- namespace App\Jobs\Import;
- use App\Models\Import;
- use App\Services\Import\ImportSparePartsService;
- use Illuminate\Bus\Queueable;
- use Illuminate\Contracts\Queue\ShouldQueue;
- use Illuminate\Foundation\Bus\Dispatchable;
- use Illuminate\Queue\InteractsWithQueue;
- use Illuminate\Queue\SerializesModels;
- use Illuminate\Support\Facades\Log;
- use Illuminate\Support\Facades\Storage;
- class ImportSparePartsJob implements ShouldQueue
- {
- use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
- public function __construct(
- private readonly string $filePath,
- private readonly int $userId,
- private readonly ?int $importId = null,
- ) {}
- public function handle(): void
- {
- Log::info("ImportSparePartsJob started", [
- 'file_path' => $this->filePath,
- 'user_id' => $this->userId,
- ]);
- $import = null;
- if ($this->importId) {
- $import = Import::find($this->importId);
- }
- try {
- $service = new ImportSparePartsService($this->filePath, $this->userId);
- $result = $service->handle();
- if ($import) {
- if ($result['success']) {
- $import->update([
- 'status' => Import::STATUS_COMPLETED,
- 'result' => implode("\n", $result['logs']),
- ]);
- } else {
- $import->update([
- 'status' => Import::STATUS_FAILED,
- 'result' => implode("\n", $result['logs']) . "\n\nОШИБКА: " . ($result['error'] ?? 'Неизвестная ошибка'),
- ]);
- }
- }
- Log::info("ImportSparePartsJob finished successfully");
- } catch (\Exception $e) {
- Log::error("ImportSparePartsJob failed: " . $e->getMessage(), [
- 'trace' => $e->getTraceAsString(),
- ]);
- if ($import) {
- $import->update([
- 'status' => Import::STATUS_FAILED,
- 'result' => "ОШИБКА: " . $e->getMessage() . "\n\n" . $e->getTraceAsString(),
- ]);
- }
- throw $e;
- } finally {
- // Удаляем временный файл
- if (file_exists($this->filePath)) {
- unlink($this->filePath);
- }
- }
- }
- }
|