Files
2026-04-28 21:14:25 +03:00

140 lines
4.8 KiB
PHP

<?php
namespace App\Jobs;
use App\Services\TpCreator\TpCreatorService;
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;
use Illuminate\Support\Facades\Cache;
class TpCreatorJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
public array $jobData;
public int $userId;
/**
* The number of times the job may be attempted.
*/
public int $tries = 3;
/**
* The number of seconds the job can run before timing out.
*/
public int $timeout = 3600; // 1 hour
/**
* Create a new job instance.
*/
public function __construct(array $jobData, int $userId = null)
{
$this->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));
}
}
}