diff --git a/src/Decorators/JobDecorator.php b/src/Decorators/JobDecorator.php index a991c9f..8c5927a 100644 --- a/src/Decorators/JobDecorator.php +++ b/src/Decorators/JobDecorator.php @@ -33,6 +33,8 @@ class JobDecorator implements ShouldQueue protected string $actionClass; protected array $parameters = []; + public ?bool $deleteWhenMissingModels; + public function __construct(string $action, ...$parameters) { $this->actionClass = $action; @@ -48,6 +50,7 @@ protected function constructed() $this->setTries($this->fromActionProperty('jobTries')); $this->setMaxExceptions($this->fromActionProperty('jobMaxExceptions')); $this->setTimeout($this->fromActionProperty('jobTimeout')); + $this->setDeleteWhenMissingModels($this->fromActionProperty('jobDeleteWhenMissingModels')); $this->fromActionMethod('configureJob', [$this]); } @@ -105,6 +108,13 @@ public function setTimeout(?int $timeout) return $this; } + public function setDeleteWhenMissingModels(?bool $deleteWhenMissingModels) + { + $this->deleteWhenMissingModels = $deleteWhenMissingModels; + + return $this; + } + public function decorates(string $actionClass): bool { return $this->getAction() instanceof $actionClass; @@ -180,11 +190,12 @@ protected function getPrependedParameters(string $method): array if ($firstParameter->allowsNull() && $firstParameterClass === Batch::class) { return [$this->batch(), ...$this->parameters]; - } elseif (is_subclass_of($firstParameterClass, self::class) || $firstParameterClass === self::class) { + } + if (is_subclass_of($firstParameterClass, self::class) || $firstParameterClass === self::class) { return [$this, ...$this->parameters]; - } else { - return $this->parameters; } + + return $this->parameters; } protected function serializeProperties() diff --git a/tests/AsJobConfiguredWithMethodsTest.php b/tests/AsJobConfiguredWithMethodsTest.php index c0be109..3ff3893 100644 --- a/tests/AsJobConfiguredWithMethodsTest.php +++ b/tests/AsJobConfiguredWithMethodsTest.php @@ -18,7 +18,8 @@ public function configureJob(JobDecorator $job) ->onQueue('my_queue') ->through(['my_middleware']) ->chain(['my_chain']) - ->delay(60); + ->delay(60) + ->setDeleteWhenMissingModels(true); } public function getJobBackoff(): array @@ -56,6 +57,7 @@ public function handle() && $job->middleware === ['my_middleware'] && $job->chained === [serialize('my_chain')] && $job->delay === 60 + && $job->deleteWhenMissingModels === true && $job->backoff() === [30, 60, 120] && $job->retryUntil()->timestamp === now()->addMinutes(30)->timestamp; }); diff --git a/tests/AsJobConfiguredWithPropertiesTest.php b/tests/AsJobConfiguredWithPropertiesTest.php index bd5ad37..af25d93 100644 --- a/tests/AsJobConfiguredWithPropertiesTest.php +++ b/tests/AsJobConfiguredWithPropertiesTest.php @@ -17,6 +17,7 @@ class AsJobConfiguredWithPropertiesTest public int $jobBackoff = 60 * 5; public int $jobTimeout = 60 * 30; public int $jobRetryUntil = 3600 * 2; + public bool $jobDeleteWhenMissingModels = true; public function handle() { @@ -41,6 +42,7 @@ public function handle() && $job->maxExceptions === 3 && $job->backoff() === 60 * 5 && $job->timeout === 60 * 30 + && $job->deleteWhenMissingModels === true && $job->retryUntil() === 3600 * 2; }); });