Skip to content

Commit

Permalink
Merge pull request #108 from ricventu/retry-until-job-method
Browse files Browse the repository at this point in the history
Support for retryUntil method
  • Loading branch information
freekmurze authored Jan 17, 2025
2 parents fdcfeda + ea22054 commit 5aecff7
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/ActionJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Throwable;
use DateTime;

class ActionJob implements ShouldQueue
{
Expand All @@ -33,6 +34,9 @@ class ActionJob implements ShouldQueue

protected $backoff;

/** @var DateTime|null */
protected $retryUntil;

public function __construct($action, array $parameters = [])
{
$this->actionClass = is_string($action) ? $action : get_class($action);
Expand All @@ -46,6 +50,10 @@ public function __construct($action, array $parameters = [])
$this->backoff = $action->backoff();
}

if (method_exists($action, 'retryUntil')) {
$this->retryUntil = $action->retryUntil();
}

if (method_exists($action, 'failed')) {
$this->onFailCallback = [$action, 'failed'];
}
Expand Down Expand Up @@ -79,6 +87,11 @@ public function backoff()
return $this->backoff;
}

public function retryUntil()
{
return $this->retryUntil;
}

public function failed(Throwable $exception)
{
if ($this->onFailCallback) {
Expand Down
14 changes: 14 additions & 0 deletions tests/QueueableActionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Spatie\QueueableAction\Tests;

use DateTime;
use Exception;
use Illuminate\Bus\PendingBatch;
use Illuminate\Database\Schema\Blueprint;
Expand All @@ -24,6 +25,7 @@
use Spatie\QueueableAction\Tests\TestClasses\InvokeableAction;
use Spatie\QueueableAction\Tests\TestClasses\MiddlewareAction;
use Spatie\QueueableAction\Tests\TestClasses\ModelSerializationUser;
use Spatie\QueueableAction\Tests\TestClasses\RetryUntilAction;
use Spatie\QueueableAction\Tests\TestClasses\SimpleAction;
use Spatie\QueueableAction\Tests\TestClasses\TaggedAction;
use stdClass;
Expand Down Expand Up @@ -281,6 +283,18 @@ public function middleware(): array
});
});

test('an action can have a retryUntil function', function () {
Queue::fake();
$until = DateTime::createFromFormat("Y-m-d H:m:s", "2000-01-01 00:00:00");
$action = new RetryUntilAction();

$action->onQueue()->execute();

Queue::assertPushed(ActionJob::class, function ($action) use ($until) {
return $action->retryUntil()->getTimestamp() === $until->getTimestamp();
});
});

test('an action can be batched', function () {
Bus::fake();

Expand Down
21 changes: 21 additions & 0 deletions tests/TestClasses/RetryUntilAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace Spatie\QueueableAction\Tests\TestClasses;

use DateTime;
use Exception;
use Spatie\QueueableAction\QueueableAction;

class RetryUntilAction
{
use QueueableAction;

public function retryUntil()
{
return DateTime::createFromFormat("Y-m-d H:m:s", "2000-01-01 00:00:00");
}

public function execute()
{
}
}

0 comments on commit 5aecff7

Please sign in to comment.