2023-08-17 12:46:48 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Lib\Queue;
|
|
|
|
|
2023-10-16 15:41:37 +02:00
|
|
|
use App\Lib\JobMiddleware\JobChannels;
|
2023-08-17 12:46:48 +02:00
|
|
|
use Illuminate\Support\Str;
|
|
|
|
use App\Lib\JobMiddleware\WithJobState;
|
|
|
|
|
|
|
|
trait TracksJob
|
|
|
|
{
|
|
|
|
abstract public function jobState(WithJobState $jobState, ...$parameters): WithJobState;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param mixed $parameters
|
|
|
|
*/
|
|
|
|
public function startJob(...$parameters): void
|
|
|
|
{
|
|
|
|
$jobId = Str::uuid();
|
2023-10-16 15:41:37 +02:00
|
|
|
$jobState = WithJobState::make($jobId);
|
2023-08-17 12:46:48 +02:00
|
|
|
$this->jobState(...[$jobState, ...$parameters])->beforeMessage->dispatch();
|
|
|
|
$parameters[] = $jobId;
|
|
|
|
static::dispatch(...$parameters);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param mixed $parameters
|
|
|
|
*
|
|
|
|
* @return array<int, object>
|
|
|
|
*/
|
|
|
|
public function getJobMiddleware(...$parameters): array
|
|
|
|
{
|
|
|
|
$jobId = array_pop($parameters);
|
2023-10-16 15:41:37 +02:00
|
|
|
$jobState = WithJobState::make($jobId);
|
2023-08-17 12:46:48 +02:00
|
|
|
$jobState = $this->jobState(...[$jobState, ...$parameters]);
|
|
|
|
$jobState->beforeMessage = null;
|
|
|
|
|
|
|
|
return [
|
|
|
|
$jobState
|
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|