diff --git a/src/CloudTasksQueue.php b/src/CloudTasksQueue.php index 058da45..be377e8 100644 --- a/src/CloudTasksQueue.php +++ b/src/CloudTasksQueue.php @@ -133,7 +133,13 @@ public function push($job, $data = '', $queue = null) $queue, null, function ($payload, $queue) use ($job) { - return $this->pushRaw($payload, $queue, ['job' => $job]); + $options = ['job' => $job]; + + if (is_object($job) && property_exists($job, 'delay') && $job->delay !== null) { + $options['delay'] = $job->delay; + } + + return $this->pushRaw($payload, $queue, $options); } ); } diff --git a/tests/QueueTest.php b/tests/QueueTest.php index 1e9110e..54e2176 100644 --- a/tests/QueueTest.php +++ b/tests/QueueTest.php @@ -25,6 +25,7 @@ use Tests\Support\JobThatWillBeReleased; use Illuminate\Queue\Events\JobProcessed; use Illuminate\Queue\Events\JobProcessing; +use Tests\Support\SimpleJobWithDelayProperty; use Tests\Support\FailingJobWithExponentialBackoff; use Illuminate\Queue\Events\JobReleasedAfterException; use Stackkit\LaravelGoogleCloudTasksQueue\IncomingTask; @@ -613,4 +614,39 @@ public function task_has_configured_dispatch_deadline(): void return $task->getDispatchDeadline()->getSeconds() === 1800; }); } + + #[Test] + public function it_will_set_the_scheduled_time_when_job_has_delay_property(): void + { + // Arrange + CloudTasksApi::fake(); + Carbon::setTestNow(now()->addDay()); + + // Act + $this->dispatch(Bus::batch([ + new SimpleJobWithDelayProperty(500), + ])); + + // Assert + CloudTasksApi::assertTaskCreated(function (Task $task): bool { + return $task->getScheduleTime()->getSeconds() === now()->addSeconds(500)->timestamp; + }); + } + + #[Test] + public function it_will_not_set_scheduled_time_when_job_delay_property_is_null(): void + { + // Arrange + CloudTasksApi::fake(); + + // Act + $this->dispatch(Bus::batch([ + new SimpleJobWithDelayProperty(null), + ])); + + // Assert + CloudTasksApi::assertTaskCreated(function (Task $task): bool { + return $task->getScheduleTime() === null; + }); + } } diff --git a/tests/Support/SimpleJobWithDelayProperty.php b/tests/Support/SimpleJobWithDelayProperty.php new file mode 100644 index 0000000..64833fa --- /dev/null +++ b/tests/Support/SimpleJobWithDelayProperty.php @@ -0,0 +1,30 @@ +delay = $delay; + } + + /** + * Execute the job. + * + * @return void + */ + public function handle() + { + event(new JobOutput('SimpleJobWithDelayProperty:success')); + } +}