Skip to content

Commit 5aecff7

Browse files
authored
Merge pull request #108 from ricventu/retry-until-job-method
Support for retryUntil method
2 parents fdcfeda + ea22054 commit 5aecff7

File tree

3 files changed

+48
-0
lines changed

3 files changed

+48
-0
lines changed

src/ActionJob.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Illuminate\Queue\InteractsWithQueue;
1010
use Illuminate\Queue\SerializesModels;
1111
use Throwable;
12+
use DateTime;
1213

1314
class ActionJob implements ShouldQueue
1415
{
@@ -33,6 +34,9 @@ class ActionJob implements ShouldQueue
3334

3435
protected $backoff;
3536

37+
/** @var DateTime|null */
38+
protected $retryUntil;
39+
3640
public function __construct($action, array $parameters = [])
3741
{
3842
$this->actionClass = is_string($action) ? $action : get_class($action);
@@ -46,6 +50,10 @@ public function __construct($action, array $parameters = [])
4650
$this->backoff = $action->backoff();
4751
}
4852

53+
if (method_exists($action, 'retryUntil')) {
54+
$this->retryUntil = $action->retryUntil();
55+
}
56+
4957
if (method_exists($action, 'failed')) {
5058
$this->onFailCallback = [$action, 'failed'];
5159
}
@@ -79,6 +87,11 @@ public function backoff()
7987
return $this->backoff;
8088
}
8189

90+
public function retryUntil()
91+
{
92+
return $this->retryUntil;
93+
}
94+
8295
public function failed(Throwable $exception)
8396
{
8497
if ($this->onFailCallback) {

tests/QueueableActionTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Spatie\QueueableAction\Tests;
44

5+
use DateTime;
56
use Exception;
67
use Illuminate\Bus\PendingBatch;
78
use Illuminate\Database\Schema\Blueprint;
@@ -24,6 +25,7 @@
2425
use Spatie\QueueableAction\Tests\TestClasses\InvokeableAction;
2526
use Spatie\QueueableAction\Tests\TestClasses\MiddlewareAction;
2627
use Spatie\QueueableAction\Tests\TestClasses\ModelSerializationUser;
28+
use Spatie\QueueableAction\Tests\TestClasses\RetryUntilAction;
2729
use Spatie\QueueableAction\Tests\TestClasses\SimpleAction;
2830
use Spatie\QueueableAction\Tests\TestClasses\TaggedAction;
2931
use stdClass;
@@ -281,6 +283,18 @@ public function middleware(): array
281283
});
282284
});
283285

286+
test('an action can have a retryUntil function', function () {
287+
Queue::fake();
288+
$until = DateTime::createFromFormat("Y-m-d H:m:s", "2000-01-01 00:00:00");
289+
$action = new RetryUntilAction();
290+
291+
$action->onQueue()->execute();
292+
293+
Queue::assertPushed(ActionJob::class, function ($action) use ($until) {
294+
return $action->retryUntil()->getTimestamp() === $until->getTimestamp();
295+
});
296+
});
297+
284298
test('an action can be batched', function () {
285299
Bus::fake();
286300

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace Spatie\QueueableAction\Tests\TestClasses;
4+
5+
use DateTime;
6+
use Exception;
7+
use Spatie\QueueableAction\QueueableAction;
8+
9+
class RetryUntilAction
10+
{
11+
use QueueableAction;
12+
13+
public function retryUntil()
14+
{
15+
return DateTime::createFromFormat("Y-m-d H:m:s", "2000-01-01 00:00:00");
16+
}
17+
18+
public function execute()
19+
{
20+
}
21+
}

0 commit comments

Comments
 (0)