Skip to content

Commit d8298b6

Browse files
Merge pull request #184 from jobvink/master
Add support for job delay in CloudTasksQueue
2 parents feaef15 + 7631f3d commit d8298b6

File tree

3 files changed

+73
-1
lines changed

3 files changed

+73
-1
lines changed

src/CloudTasksQueue.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,13 @@ public function push($job, $data = '', $queue = null)
133133
$queue,
134134
null,
135135
function ($payload, $queue) use ($job) {
136-
return $this->pushRaw($payload, $queue, ['job' => $job]);
136+
$options = ['job' => $job];
137+
138+
if (is_object($job) && property_exists($job, 'delay') && $job->delay !== null) {
139+
$options['delay'] = $job->delay;
140+
}
141+
142+
return $this->pushRaw($payload, $queue, $options);
137143
}
138144
);
139145
}

tests/QueueTest.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
use Tests\Support\JobThatWillBeReleased;
2626
use Illuminate\Queue\Events\JobProcessed;
2727
use Illuminate\Queue\Events\JobProcessing;
28+
use Tests\Support\SimpleJobWithDelayProperty;
2829
use Tests\Support\FailingJobWithExponentialBackoff;
2930
use Illuminate\Queue\Events\JobReleasedAfterException;
3031
use Stackkit\LaravelGoogleCloudTasksQueue\IncomingTask;
@@ -613,4 +614,39 @@ public function task_has_configured_dispatch_deadline(): void
613614
return $task->getDispatchDeadline()->getSeconds() === 1800;
614615
});
615616
}
617+
618+
#[Test]
619+
public function it_will_set_the_scheduled_time_when_job_has_delay_property(): void
620+
{
621+
// Arrange
622+
CloudTasksApi::fake();
623+
Carbon::setTestNow(now()->addDay());
624+
625+
// Act
626+
$this->dispatch(Bus::batch([
627+
new SimpleJobWithDelayProperty(500),
628+
]));
629+
630+
// Assert
631+
CloudTasksApi::assertTaskCreated(function (Task $task): bool {
632+
return $task->getScheduleTime()->getSeconds() === now()->addSeconds(500)->timestamp;
633+
});
634+
}
635+
636+
#[Test]
637+
public function it_will_not_set_scheduled_time_when_job_delay_property_is_null(): void
638+
{
639+
// Arrange
640+
CloudTasksApi::fake();
641+
642+
// Act
643+
$this->dispatch(Bus::batch([
644+
new SimpleJobWithDelayProperty(null),
645+
]));
646+
647+
// Assert
648+
CloudTasksApi::assertTaskCreated(function (Task $task): bool {
649+
return $task->getScheduleTime() === null;
650+
});
651+
}
616652
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Tests\Support;
6+
7+
class SimpleJobWithDelayProperty extends BaseJob
8+
{
9+
public $tries = 3;
10+
11+
/**
12+
* Create a new job instance.
13+
*
14+
* @return void
15+
*/
16+
public function __construct($delay = null)
17+
{
18+
$this->delay = $delay;
19+
}
20+
21+
/**
22+
* Execute the job.
23+
*
24+
* @return void
25+
*/
26+
public function handle()
27+
{
28+
event(new JobOutput('SimpleJobWithDelayProperty:success'));
29+
}
30+
}

0 commit comments

Comments
 (0)