Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for retryUntil method #108

Merged
merged 1 commit into from
Jan 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()
{
}
}
Loading