Skip to content

Commit

Permalink
Handle $deleteWhenMissingModels on JobDecorator (#283)
Browse files Browse the repository at this point in the history
  • Loading branch information
patrickomeara authored Aug 8, 2024
1 parent d5c2ca5 commit 18346c0
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 4 deletions.
17 changes: 14 additions & 3 deletions src/Decorators/JobDecorator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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]);
}

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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()
Expand Down
4 changes: 3 additions & 1 deletion tests/AsJobConfiguredWithMethodsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
});
Expand Down
2 changes: 2 additions & 0 deletions tests/AsJobConfiguredWithPropertiesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand All @@ -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;
});
});

0 comments on commit 18346c0

Please sign in to comment.