Files
citrus-cms/app/Jobs/SpoolStatusChangerJob.php
2026-04-28 21:14:25 +03:00

152 lines
4.2 KiB
PHP

<?php
namespace App\Jobs;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Contracts\Queue\ShouldBeUnique;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Log;
class SpoolStatusChangerJob implements ShouldQueue, ShouldBeUnique
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
/**
* The number of seconds the job can run before timing out.
*
* @var int
*/
public $timeout = 120; // 2 minutes
/**
* The number of seconds after which the job's unique lock will be released.
*
* @var int
*/
public $uniqueFor = 300;
/**
* The number of times the job may be attempted.
*
* @var int
*/
public $tries = 2; // Try twice on failure
/**
* The maximum number of unhandled exceptions to allow before failing.
*
* @var int
*/
public $maxExceptions = 2;
protected $isoNumber;
protected $spoolNumber;
/**
* Create a new job instance.
*/
public function __construct(?string $isoNumber = null, ?string $spoolNumber = null)
{
$this->isoNumber = $isoNumber;
$this->spoolNumber = $spoolNumber;
// Set high priority queue
$this->onQueue('high-priority-save-trigger');
}
/**
* Get the unique ID for the job.
*/
public function uniqueId(): string
{
return md5($this->isoNumber . '_' . $this->spoolNumber);
}
/**
* Execute the job.
*/
public function handle(): void
{
$startTime = microtime(true);
Log::info("=== SpoolStatusChangerJob STARTED ===", [
'iso_number' => $this->isoNumber,
'spool_number' => $this->spoolNumber,
'timestamp' => now(),
'memory_start' => memory_get_usage(true) / 1024 / 1024 . ' MB'
]);
try {
if ($this->isoNumber !== null) {
if ($this->spoolNumber !== null) {
spoolStatusChanger($this->isoNumber, $this->spoolNumber);
} else {
spoolStatusChanger($this->isoNumber);
}
Log::info("Spool status updated successfully", [
'iso_number' => $this->isoNumber,
'spool_number' => $this->spoolNumber
]);
} else {
Log::warning("SpoolStatusChangerJob called with null iso_number", [
'spool_number' => $this->spoolNumber
]);
}
} catch (\Exception $e) {
Log::error("SpoolStatusChangerJob failed", [
'iso_number' => $this->isoNumber,
'spool_number' => $this->spoolNumber,
'error' => $e->getMessage(),
'trace' => $e->getTraceAsString()
]);
throw $e; // Re-throw to trigger retry mechanism
}
$endTime = microtime(true);
$executionTime = $endTime - $startTime;
$memoryUsage = memory_get_peak_usage(true) / 1024 / 1024;
Log::info("=== SpoolStatusChangerJob COMPLETED ===", [
'iso_number' => $this->isoNumber,
'spool_number' => $this->spoolNumber,
'execution_time_seconds' => round($executionTime, 2),
'memory_peak_mb' => round($memoryUsage, 2),
'timestamp' => now()
]);
}
/**
* Get the job's description for Telescope.
*/
public function getDescription(): string
{
return "Update spool status for ISO: {$this->isoNumber}" .
($this->spoolNumber ? ", Spool: {$this->spoolNumber}" : "");
}
/**
* Get the tags that should be assigned to the job.
*/
public function tags(): array
{
$tags = ["SpoolStatusChanger"];
if ($this->isoNumber) {
$tags[] = "ISO:{$this->isoNumber}";
}
if ($this->spoolNumber) {
$tags[] = "Spool:{$this->spoolNumber}";
}
return $tags;
}
}