Skip to content
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
8 changes: 7 additions & 1 deletion src/CloudTasksQueue.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
);
}
Expand Down
36 changes: 36 additions & 0 deletions tests/QueueTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
});
}
}
30 changes: 30 additions & 0 deletions tests/Support/SimpleJobWithDelayProperty.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

declare(strict_types=1);

namespace Tests\Support;

class SimpleJobWithDelayProperty extends BaseJob
{
public $tries = 3;

/**
* Create a new job instance.
*
* @return void
*/
public function __construct($delay = null)
{
$this->delay = $delay;
}

/**
* Execute the job.
*
* @return void
*/
public function handle()
{
event(new JobOutput('SimpleJobWithDelayProperty:success'));
}
}
Loading