adrema/app/Lib/Queue/TracksJob.php

42 lines
1.1 KiB
PHP
Raw Normal View History

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-12-16 11:44:32 +01:00
tap($this->jobState(...[$jobState, ...$parameters])->beforeMessage, fn ($beforeMessage) => $beforeMessage && $beforeMessage->dispatch());;
2023-08-17 12:46:48 +02:00
$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
];
}
}