Skip to content

Add payload creation and original delay info to job payload #55529

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

Merged
merged 4 commits into from
Apr 24, 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
2 changes: 1 addition & 1 deletion src/Illuminate/Queue/BeanstalkdQueue.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ public function later($delay, $job, $data = '', $queue = null)
{
return $this->enqueueUsing(
$job,
$this->createPayload($job, $this->getQueue($queue), $data),
$this->createPayload($job, $this->getQueue($queue), $data, $delay),
$queue,
$delay,
function ($payload, $queue, $delay) {
Expand Down
2 changes: 1 addition & 1 deletion src/Illuminate/Queue/DatabaseQueue.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ public function later($delay, $job, $data = '', $queue = null)
{
return $this->enqueueUsing(
$job,
$this->createPayload($job, $this->getQueue($queue), $data),
$this->createPayload($job, $this->getQueue($queue), $data, $delay),
$queue,
$delay,
function ($payload, $queue, $delay) {
Expand Down
14 changes: 12 additions & 2 deletions src/Illuminate/Queue/Queue.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Illuminate\Queue;

use Carbon\Carbon;
use Closure;
use DateTimeInterface;
use Illuminate\Bus\UniqueLock;
Expand Down Expand Up @@ -97,17 +98,24 @@ public function bulk($jobs, $data = '', $queue = null)
* @param \Closure|string|object $job
* @param string $queue
* @param mixed $data
* @param \DateTimeInterface|\DateInterval|int|null $delay
* @return string
*
* @throws \Illuminate\Queue\InvalidPayloadException
*/
protected function createPayload($job, $queue, $data = '')
protected function createPayload($job, $queue, $data = '', $delay = null)
{
if ($job instanceof Closure) {
$job = CallQueuedClosure::create($job);
}

$payload = json_encode($value = $this->createPayloadArray($job, $queue, $data), \JSON_UNESCAPED_UNICODE);
$value = $this->createPayloadArray($job, $queue, $data);

$value['delay'] = isset($delay)
? $this->secondsUntil($delay)
: null;

$payload = json_encode($value, \JSON_UNESCAPED_UNICODE);

if (json_last_error() !== JSON_ERROR_NONE) {
throw new InvalidPayloadException(
Expand Down Expand Up @@ -156,6 +164,7 @@ protected function createObjectPayload($job, $queue)
'commandName' => $job,
'command' => $job,
],
'createdAt' => Carbon::now()->getTimestamp(),
]);

$command = $this->jobShouldBeEncrypted($job) && $this->container->bound(Encrypter::class)
Expand Down Expand Up @@ -277,6 +286,7 @@ protected function createStringPayload($job, $queue, $data)
'backoff' => null,
'timeout' => null,
'data' => $data,
'createdAt' => Carbon::now()->getTimestamp(),
]);
}

Expand Down
2 changes: 1 addition & 1 deletion src/Illuminate/Queue/RedisQueue.php
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ public function later($delay, $job, $data = '', $queue = null)
{
return $this->enqueueUsing(
$job,
$this->createPayload($job, $this->getQueue($queue), $data),
$this->createPayload($job, $this->getQueue($queue), $data, $delay),
$queue,
$delay,
function ($payload, $queue, $delay) {
Expand Down
2 changes: 1 addition & 1 deletion src/Illuminate/Queue/SqsQueue.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ public function later($delay, $job, $data = '', $queue = null)
{
return $this->enqueueUsing(
$job,
$this->createPayload($job, $queue ?: $this->default, $data),
$this->createPayload($job, $queue ?: $this->default, $data, $delay),
$queue,
$delay,
function ($payload, $queue, $delay) {
Expand Down
13 changes: 11 additions & 2 deletions tests/Queue/QueueBeanstalkdQueueTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Illuminate\Tests\Queue;

use Carbon\Carbon;
use Illuminate\Container\Container;
use Illuminate\Queue\BeanstalkdQueue;
use Illuminate\Queue\Jobs\BeanstalkdJob;
Expand Down Expand Up @@ -38,6 +39,9 @@ public function testPushProperlyPushesJobOntoBeanstalkd()
{
$uuid = Str::uuid();

$time = Carbon::now();
Carbon::setTestNow($time);

Str::createUuidsUsing(function () use ($uuid) {
return $uuid;
});
Expand All @@ -46,13 +50,14 @@ public function testPushProperlyPushesJobOntoBeanstalkd()
$pheanstalk = $this->queue->getPheanstalk();
$pheanstalk->shouldReceive('useTube')->once()->with(m::type(TubeName::class));
$pheanstalk->shouldReceive('useTube')->once()->with(m::type(TubeName::class));
$pheanstalk->shouldReceive('put')->twice()->with(json_encode(['uuid' => $uuid, 'displayName' => 'foo', 'job' => 'foo', 'maxTries' => null, 'maxExceptions' => null, 'failOnTimeout' => false, 'backoff' => null, 'timeout' => null, 'data' => ['data']]), 1024, 0, 60);
$pheanstalk->shouldReceive('put')->twice()->with(json_encode(['uuid' => $uuid, 'displayName' => 'foo', 'job' => 'foo', 'maxTries' => null, 'maxExceptions' => null, 'failOnTimeout' => false, 'backoff' => null, 'timeout' => null, 'data' => ['data'], 'createdAt' => $time->getTimestamp(), 'delay' => null]), 1024, 0, 60);

$this->queue->push('foo', ['data'], 'stack');
$this->queue->push('foo', ['data']);

$this->container->shouldHaveReceived('bound')->with('events')->times(4);

Carbon::setTestNow();
Str::createUuidsNormally();
}

Expand All @@ -64,17 +69,21 @@ public function testDelayedPushProperlyPushesJobOntoBeanstalkd()
return $uuid;
});

$time = Carbon::now();
Carbon::setTestNow($time);

$this->setQueue('default', 60);
$pheanstalk = $this->queue->getPheanstalk();
$pheanstalk->shouldReceive('useTube')->once()->with(m::type(TubeName::class));
$pheanstalk->shouldReceive('useTube')->once()->with(m::type(TubeName::class));
$pheanstalk->shouldReceive('put')->twice()->with(json_encode(['uuid' => $uuid, 'displayName' => 'foo', 'job' => 'foo', 'maxTries' => null, 'maxExceptions' => null, 'failOnTimeout' => false, 'backoff' => null, 'timeout' => null, 'data' => ['data']]), Pheanstalk::DEFAULT_PRIORITY, 5, Pheanstalk::DEFAULT_TTR);
$pheanstalk->shouldReceive('put')->twice()->with(json_encode(['uuid' => $uuid, 'displayName' => 'foo', 'job' => 'foo', 'maxTries' => null, 'maxExceptions' => null, 'failOnTimeout' => false, 'backoff' => null, 'timeout' => null, 'data' => ['data'], 'createdAt' => $time->getTimestamp(), 'delay' => 5]), Pheanstalk::DEFAULT_PRIORITY, 5, Pheanstalk::DEFAULT_TTR);

$this->queue->later(5, 'foo', ['data'], 'stack');
$this->queue->later(5, 'foo', ['data']);

$this->container->shouldHaveReceived('bound')->with('events')->times(4);

Carbon::setTestNow();
Str::createUuidsNormally();
}

Expand Down
19 changes: 14 additions & 5 deletions tests/Queue/QueueDatabaseQueueUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Illuminate\Tests\Queue;

use Carbon\Carbon;
use Illuminate\Container\Container;
use Illuminate\Database\Connection;
use Illuminate\Queue\DatabaseQueue;
Expand Down Expand Up @@ -69,16 +70,19 @@ public function testDelayedPushProperlyPushesJobOntoDatabase()
return $uuid;
});

$time = Carbon::now();
Carbon::setTestNow($time);

$queue = $this->getMockBuilder(DatabaseQueue::class)
->onlyMethods(['currentTime'])
->setConstructorArgs([$database = m::mock(Connection::class), 'table', 'default'])
->getMock();
$queue->expects($this->any())->method('currentTime')->willReturn('time');
$queue->setContainer($container = m::spy(Container::class));
$database->shouldReceive('table')->with('table')->andReturn($query = m::mock(stdClass::class));
$query->shouldReceive('insertGetId')->once()->andReturnUsing(function ($array) use ($uuid) {
$query->shouldReceive('insertGetId')->once()->andReturnUsing(function ($array) use ($uuid, $time) {
$this->assertSame('default', $array['queue']);
$this->assertSame(json_encode(['uuid' => $uuid, 'displayName' => 'foo', 'job' => 'foo', 'maxTries' => null, 'maxExceptions' => null, 'failOnTimeout' => false, 'backoff' => null, 'timeout' => null, 'data' => ['data']]), $array['payload']);
$this->assertSame(json_encode(['uuid' => $uuid, 'displayName' => 'foo', 'job' => 'foo', 'maxTries' => null, 'maxExceptions' => null, 'failOnTimeout' => false, 'backoff' => null, 'timeout' => null, 'data' => ['data'], 'createdAt' => $time->getTimestamp(), 'delay' => 10]), $array['payload']);
$this->assertEquals(0, $array['attempts']);
$this->assertNull($array['reserved_at']);
$this->assertIsInt($array['available_at']);
Expand All @@ -88,6 +92,7 @@ public function testDelayedPushProperlyPushesJobOntoDatabase()

$container->shouldHaveReceived('bound')->with('events')->twice();

Carbon::setTestNow();
Str::createUuidsNormally();
}

Expand Down Expand Up @@ -130,22 +135,25 @@ public function testBulkBatchPushesOntoDatabase()
return $uuid;
});

$time = Carbon::now();
Carbon::setTestNow($time);

$database = m::mock(Connection::class);
$queue = $this->getMockBuilder(DatabaseQueue::class)->onlyMethods(['currentTime', 'availableAt'])->setConstructorArgs([$database, 'table', 'default'])->getMock();
$queue->expects($this->any())->method('currentTime')->willReturn('created');
$queue->expects($this->any())->method('availableAt')->willReturn('available');
$database->shouldReceive('table')->with('table')->andReturn($query = m::mock(stdClass::class));
$query->shouldReceive('insert')->once()->andReturnUsing(function ($records) use ($uuid) {
$query->shouldReceive('insert')->once()->andReturnUsing(function ($records) use ($uuid, $time) {
$this->assertEquals([[
'queue' => 'queue',
'payload' => json_encode(['uuid' => $uuid, 'displayName' => 'foo', 'job' => 'foo', 'maxTries' => null, 'maxExceptions' => null, 'failOnTimeout' => false, 'backoff' => null, 'timeout' => null, 'data' => ['data']]),
'payload' => json_encode(['uuid' => $uuid, 'displayName' => 'foo', 'job' => 'foo', 'maxTries' => null, 'maxExceptions' => null, 'failOnTimeout' => false, 'backoff' => null, 'timeout' => null, 'data' => ['data'], 'createdAt' => $time->getTimestamp(), 'delay' => null]),
'attempts' => 0,
'reserved_at' => null,
'available_at' => 'available',
'created_at' => 'created',
], [
'queue' => 'queue',
'payload' => json_encode(['uuid' => $uuid, 'displayName' => 'bar', 'job' => 'bar', 'maxTries' => null, 'maxExceptions' => null, 'failOnTimeout' => false, 'backoff' => null, 'timeout' => null, 'data' => ['data']]),
'payload' => json_encode(['uuid' => $uuid, 'displayName' => 'bar', 'job' => 'bar', 'maxTries' => null, 'maxExceptions' => null, 'failOnTimeout' => false, 'backoff' => null, 'timeout' => null, 'data' => ['data'], 'createdAt' => $time->getTimestamp(), 'delay' => null]),
'attempts' => 0,
'reserved_at' => null,
'available_at' => 'available',
Expand All @@ -155,6 +163,7 @@ public function testBulkBatchPushesOntoDatabase()

$queue->bulk(['foo', 'bar'], ['data'], 'queue');

Carbon::setTestNow();
Str::createUuidsNormally();
}

Expand Down
36 changes: 27 additions & 9 deletions tests/Queue/QueueRedisQueueTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,20 @@ public function testPushProperlyPushesJobOntoRedis()
return $uuid;
});

$time = Carbon::now();
Carbon::setTestNow($time);

$queue = $this->getMockBuilder(RedisQueue::class)->onlyMethods(['getRandomId'])->setConstructorArgs([$redis = m::mock(Factory::class), 'default'])->getMock();
$queue->expects($this->once())->method('getRandomId')->willReturn('foo');
$queue->setContainer($container = m::spy(Container::class));
$redis->shouldReceive('connection')->once()->andReturn($redis);
$redis->shouldReceive('eval')->once()->with(LuaScripts::push(), 2, 'queues:default', 'queues:default:notify', json_encode(['uuid' => $uuid, 'displayName' => 'foo', 'job' => 'foo', 'maxTries' => null, 'maxExceptions' => null, 'failOnTimeout' => false, 'backoff' => null, 'timeout' => null, 'data' => ['data'], 'id' => 'foo', 'attempts' => 0]));
$redis->shouldReceive('eval')->once()->with(LuaScripts::push(), 2, 'queues:default', 'queues:default:notify', json_encode(['uuid' => $uuid, 'displayName' => 'foo', 'job' => 'foo', 'maxTries' => null, 'maxExceptions' => null, 'failOnTimeout' => false, 'backoff' => null, 'timeout' => null, 'data' => ['data'], 'createdAt' => $time->getTimestamp(), 'id' => 'foo', 'attempts' => 0, 'delay' => null]));

$id = $queue->push('foo', ['data']);
$this->assertSame('foo', $id);
$container->shouldHaveReceived('bound')->with('events')->twice();

Carbon::setTestNow();
Str::createUuidsNormally();
}

Expand All @@ -48,11 +52,14 @@ public function testPushProperlyPushesJobOntoRedisWithCustomPayloadHook()
return $uuid;
});

$time = Carbon::now();
Carbon::setTestNow($time);

$queue = $this->getMockBuilder(RedisQueue::class)->onlyMethods(['getRandomId'])->setConstructorArgs([$redis = m::mock(Factory::class), 'default'])->getMock();
$queue->expects($this->once())->method('getRandomId')->willReturn('foo');
$queue->setContainer($container = m::spy(Container::class));
$redis->shouldReceive('connection')->once()->andReturn($redis);
$redis->shouldReceive('eval')->once()->with(LuaScripts::push(), 2, 'queues:default', 'queues:default:notify', json_encode(['uuid' => $uuid, 'displayName' => 'foo', 'job' => 'foo', 'maxTries' => null, 'maxExceptions' => null, 'failOnTimeout' => false, 'backoff' => null, 'timeout' => null, 'data' => ['data'], 'custom' => 'taylor', 'id' => 'foo', 'attempts' => 0]));
$redis->shouldReceive('eval')->once()->with(LuaScripts::push(), 2, 'queues:default', 'queues:default:notify', json_encode(['uuid' => $uuid, 'displayName' => 'foo', 'job' => 'foo', 'maxTries' => null, 'maxExceptions' => null, 'failOnTimeout' => false, 'backoff' => null, 'timeout' => null, 'data' => ['data'], 'createdAt' => $time->getTimestamp(), 'custom' => 'taylor', 'id' => 'foo', 'attempts' => 0, 'delay' => null]));

Queue::createPayloadUsing(function ($connection, $queue, $payload) {
return ['custom' => 'taylor'];
Expand All @@ -64,6 +71,7 @@ public function testPushProperlyPushesJobOntoRedisWithCustomPayloadHook()

Queue::createPayloadUsing(null);

Carbon::setTestNow();
Str::createUuidsNormally();
}

Expand All @@ -75,11 +83,14 @@ public function testPushProperlyPushesJobOntoRedisWithTwoCustomPayloadHook()
return $uuid;
});

$time = Carbon::now();
Carbon::setTestNow($time);

$queue = $this->getMockBuilder(RedisQueue::class)->onlyMethods(['getRandomId'])->setConstructorArgs([$redis = m::mock(Factory::class), 'default'])->getMock();
$queue->expects($this->once())->method('getRandomId')->willReturn('foo');
$queue->setContainer($container = m::spy(Container::class));
$redis->shouldReceive('connection')->once()->andReturn($redis);
$redis->shouldReceive('eval')->once()->with(LuaScripts::push(), 2, 'queues:default', 'queues:default:notify', json_encode(['uuid' => $uuid, 'displayName' => 'foo', 'job' => 'foo', 'maxTries' => null, 'maxExceptions' => null, 'failOnTimeout' => false, 'backoff' => null, 'timeout' => null, 'data' => ['data'], 'custom' => 'taylor', 'bar' => 'foo', 'id' => 'foo', 'attempts' => 0]));
$redis->shouldReceive('eval')->once()->with(LuaScripts::push(), 2, 'queues:default', 'queues:default:notify', json_encode(['uuid' => $uuid, 'displayName' => 'foo', 'job' => 'foo', 'maxTries' => null, 'maxExceptions' => null, 'failOnTimeout' => false, 'backoff' => null, 'timeout' => null, 'data' => ['data'], 'createdAt' => $time->getTimestamp(), 'custom' => 'taylor', 'bar' => 'foo', 'id' => 'foo', 'attempts' => 0, 'delay' => null]));

Queue::createPayloadUsing(function ($connection, $queue, $payload) {
return ['custom' => 'taylor'];
Expand All @@ -95,6 +106,7 @@ public function testPushProperlyPushesJobOntoRedisWithTwoCustomPayloadHook()

Queue::createPayloadUsing(null);

Carbon::setTestNow();
Str::createUuidsNormally();
}

Expand All @@ -106,6 +118,9 @@ public function testDelayedPushProperlyPushesJobOntoRedis()
return $uuid;
});

$time = Carbon::now();
Carbon::setTestNow($time);

$queue = $this->getMockBuilder(RedisQueue::class)->onlyMethods(['availableAt', 'getRandomId'])->setConstructorArgs([$redis = m::mock(Factory::class), 'default'])->getMock();
$queue->setContainer($container = m::spy(Container::class));
$queue->expects($this->once())->method('getRandomId')->willReturn('foo');
Expand All @@ -115,13 +130,14 @@ public function testDelayedPushProperlyPushesJobOntoRedis()
$redis->shouldReceive('zadd')->once()->with(
'queues:default:delayed',
2,
json_encode(['uuid' => $uuid, 'displayName' => 'foo', 'job' => 'foo', 'maxTries' => null, 'maxExceptions' => null, 'failOnTimeout' => false, 'backoff' => null, 'timeout' => null, 'data' => ['data'], 'id' => 'foo', 'attempts' => 0])
json_encode(['uuid' => $uuid, 'displayName' => 'foo', 'job' => 'foo', 'maxTries' => null, 'maxExceptions' => null, 'failOnTimeout' => false, 'backoff' => null, 'timeout' => null, 'data' => ['data'], 'createdAt' => $time->getTimestamp(), 'id' => 'foo', 'attempts' => 0, 'delay' => 1])
);

$id = $queue->later(1, 'foo', ['data']);
$this->assertSame('foo', $id);
$container->shouldHaveReceived('bound')->with('events')->twice();

Carbon::setTestNow();
Str::createUuidsNormally();
}

Expand All @@ -133,22 +149,24 @@ public function testDelayedPushWithDateTimeProperlyPushesJobOntoRedis()
return $uuid;
});

$date = Carbon::now();
$time = $date = Carbon::now();
Carbon::setTestNow($time);
$queue = $this->getMockBuilder(RedisQueue::class)->onlyMethods(['availableAt', 'getRandomId'])->setConstructorArgs([$redis = m::mock(Factory::class), 'default'])->getMock();
$queue->setContainer($container = m::spy(Container::class));
$queue->expects($this->once())->method('getRandomId')->willReturn('foo');
$queue->expects($this->once())->method('availableAt')->with($date)->willReturn(2);
$queue->expects($this->once())->method('availableAt')->with($date)->willReturn(5);

$redis->shouldReceive('connection')->once()->andReturn($redis);
$redis->shouldReceive('zadd')->once()->with(
'queues:default:delayed',
2,
json_encode(['uuid' => $uuid, 'displayName' => 'foo', 'job' => 'foo', 'maxTries' => null, 'maxExceptions' => null, 'failOnTimeout' => false, 'backoff' => null, 'timeout' => null, 'data' => ['data'], 'id' => 'foo', 'attempts' => 0])
5,
json_encode(['uuid' => $uuid, 'displayName' => 'foo', 'job' => 'foo', 'maxTries' => null, 'maxExceptions' => null, 'failOnTimeout' => false, 'backoff' => null, 'timeout' => null, 'data' => ['data'], 'createdAt' => $time->getTimestamp(), 'id' => 'foo', 'attempts' => 0, 'delay' => 5])
);

$queue->later($date, 'foo', ['data']);
$queue->later($date->addSeconds(5), 'foo', ['data']);
$container->shouldHaveReceived('bound')->with('events')->twice();

Carbon::setTestNow();
Str::createUuidsNormally();
}
}