jobData = $jobData; $this->userId = $userId; // Set the queue for this job $this->onQueue('register-creator'); } /** * Execute the job. */ public function handle(): void { $lineData = $this->jobData['line_data'] ?? []; $documents = $this->jobData['documents'] ?? []; $settings = $this->jobData['settings'] ?? []; $jobId = $this->jobData['job_id'] ?? uniqid('tp_', true); $lineIdentifier = $lineData['test_package_number'] ?? 'unknown'; Log::info("TpCreatorJob started", [ 'job_id' => $jobId, 'line' => $lineIdentifier, 'attempt' => $this->attempts(), 'user_id' => $this->userId ]); // Check if job is cancelled (deleted from queue cache) $queue = Cache::get('tp-creator-queue', []); if (!isset($queue[$jobId])) { Log::info("TpCreatorJob cancelled by user (job ID not found in cache)", ['job_id' => $jobId]); return; } try { setlocale(LC_ALL, 'ru_RU.utf8'); \App::setLocale("ru"); set_time_limit(-1); ini_set('max_execution_time', -1); ini_set('memory_limit', '2048M'); $service = new TpCreatorService(); $result = $service->processLine($lineData, $documents, array_merge($settings, ['job_id' => $jobId])); Log::info("TpCreatorJob completed successfully", ['job_id' => $jobId, 'line' => $lineIdentifier]); $queue = Cache::get('tp-creator-queue', []); if(isset($queue[$jobId])) { $queue[$jobId]['progress'] = 100; $queue[$jobId]['description'] = "Completed"; $queue[$jobId]['status'] = "completed"; Cache::put('tp-creator-queue', $queue, now()->addHours(24)); } } catch (\Throwable $th) { $errorDetails = [ 'job_id' => $jobId, 'line' => $lineIdentifier, 'error_message' => $th->getMessage(), 'file' => $th->getFile(), 'line_number' => $th->getLine(), 'trace' => $th->getTraceAsString() ]; Log::error("TpCreatorJob failed", $errorDetails); // Update cache status to failed $queue = Cache::get('tp-creator-queue', []); if(isset($queue[$jobId])) { $queue[$jobId]['status'] = "failed"; $queue[$jobId]['description'] = "Failed: " . $th->getMessage(); Cache::put('tp-creator-queue', $queue, now()->addHours(24)); } // Save detailed error to file for debugging try { $errorLogPath = "tp_creator_errors/" . date('Y-m-d') . "/"; if (!Storage::exists($errorLogPath)) { Storage::makeDirectory($errorLogPath); } $errorFileName = $errorLogPath . "{$jobId}_error.json"; Storage::put($errorFileName, json_encode($errorDetails, JSON_PRETTY_PRINT)); } catch (\Throwable $e) { // Ignore storage errors } throw $th; } } /** * Handle a job failure. */ public function failed(\Throwable $exception): void { $jobId = $this->jobData['job_id'] ?? 'unknown'; Log::error("TpCreatorJob permanently failed: " . $exception->getMessage()); $queue = Cache::get('tp-creator-queue', []); if(isset($queue[$jobId])) { $queue[$jobId]['status'] = "failed"; $queue[$jobId]['description'] = "Permanently Failed: " . $exception->getMessage(); Cache::put('tp-creator-queue', $queue, now()->addHours(24)); } } }