Skip to content

Commit b204b85

Browse files
Add payload creation and original delay info to job payload (#55529)
* add payload creation and original delay info to job payload * Apply fixes from StyleCI * nullable type --------- Co-authored-by: StyleCI Bot <[email protected]>
1 parent 1a6198b commit b204b85

File tree

8 files changed

+68
-22
lines changed

8 files changed

+68
-22
lines changed

src/Illuminate/Queue/BeanstalkdQueue.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ public function later($delay, $job, $data = '', $queue = null)
125125
{
126126
return $this->enqueueUsing(
127127
$job,
128-
$this->createPayload($job, $this->getQueue($queue), $data),
128+
$this->createPayload($job, $this->getQueue($queue), $data, $delay),
129129
$queue,
130130
$delay,
131131
function ($payload, $queue, $delay) {

src/Illuminate/Queue/DatabaseQueue.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ public function later($delay, $job, $data = '', $queue = null)
126126
{
127127
return $this->enqueueUsing(
128128
$job,
129-
$this->createPayload($job, $this->getQueue($queue), $data),
129+
$this->createPayload($job, $this->getQueue($queue), $data, $delay),
130130
$queue,
131131
$delay,
132132
function ($payload, $queue, $delay) {

src/Illuminate/Queue/Queue.php

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

33
namespace Illuminate\Queue;
44

5+
use Carbon\Carbon;
56
use Closure;
67
use DateTimeInterface;
78
use Illuminate\Bus\UniqueLock;
@@ -97,17 +98,24 @@ public function bulk($jobs, $data = '', $queue = null)
9798
* @param \Closure|string|object $job
9899
* @param string $queue
99100
* @param mixed $data
101+
* @param \DateTimeInterface|\DateInterval|int|null $delay
100102
* @return string
101103
*
102104
* @throws \Illuminate\Queue\InvalidPayloadException
103105
*/
104-
protected function createPayload($job, $queue, $data = '')
106+
protected function createPayload($job, $queue, $data = '', $delay = null)
105107
{
106108
if ($job instanceof Closure) {
107109
$job = CallQueuedClosure::create($job);
108110
}
109111

110-
$payload = json_encode($value = $this->createPayloadArray($job, $queue, $data), \JSON_UNESCAPED_UNICODE);
112+
$value = $this->createPayloadArray($job, $queue, $data);
113+
114+
$value['delay'] = isset($delay)
115+
? $this->secondsUntil($delay)
116+
: null;
117+
118+
$payload = json_encode($value, \JSON_UNESCAPED_UNICODE);
111119

112120
if (json_last_error() !== JSON_ERROR_NONE) {
113121
throw new InvalidPayloadException(
@@ -156,6 +164,7 @@ protected function createObjectPayload($job, $queue)
156164
'commandName' => $job,
157165
'command' => $job,
158166
],
167+
'createdAt' => Carbon::now()->getTimestamp(),
159168
]);
160169

161170
$command = $this->jobShouldBeEncrypted($job) && $this->container->bound(Encrypter::class)
@@ -277,6 +286,7 @@ protected function createStringPayload($job, $queue, $data)
277286
'backoff' => null,
278287
'timeout' => null,
279288
'data' => $data,
289+
'createdAt' => Carbon::now()->getTimestamp(),
280290
]);
281291
}
282292

src/Illuminate/Queue/RedisQueue.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ public function later($delay, $job, $data = '', $queue = null)
192192
{
193193
return $this->enqueueUsing(
194194
$job,
195-
$this->createPayload($job, $this->getQueue($queue), $data),
195+
$this->createPayload($job, $this->getQueue($queue), $data, $delay),
196196
$queue,
197197
$delay,
198198
function ($payload, $queue, $delay) {

src/Illuminate/Queue/SqsQueue.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ public function later($delay, $job, $data = '', $queue = null)
128128
{
129129
return $this->enqueueUsing(
130130
$job,
131-
$this->createPayload($job, $queue ?: $this->default, $data),
131+
$this->createPayload($job, $queue ?: $this->default, $data, $delay),
132132
$queue,
133133
$delay,
134134
function ($payload, $queue, $delay) {

tests/Queue/QueueBeanstalkdQueueTest.php

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

33
namespace Illuminate\Tests\Queue;
44

5+
use Carbon\Carbon;
56
use Illuminate\Container\Container;
67
use Illuminate\Queue\BeanstalkdQueue;
78
use Illuminate\Queue\Jobs\BeanstalkdJob;
@@ -38,6 +39,9 @@ public function testPushProperlyPushesJobOntoBeanstalkd()
3839
{
3940
$uuid = Str::uuid();
4041

42+
$time = Carbon::now();
43+
Carbon::setTestNow($time);
44+
4145
Str::createUuidsUsing(function () use ($uuid) {
4246
return $uuid;
4347
});
@@ -46,13 +50,14 @@ public function testPushProperlyPushesJobOntoBeanstalkd()
4650
$pheanstalk = $this->queue->getPheanstalk();
4751
$pheanstalk->shouldReceive('useTube')->once()->with(m::type(TubeName::class));
4852
$pheanstalk->shouldReceive('useTube')->once()->with(m::type(TubeName::class));
49-
$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);
53+
$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);
5054

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

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

60+
Carbon::setTestNow();
5661
Str::createUuidsNormally();
5762
}
5863

@@ -64,17 +69,21 @@ public function testDelayedPushProperlyPushesJobOntoBeanstalkd()
6469
return $uuid;
6570
});
6671

72+
$time = Carbon::now();
73+
Carbon::setTestNow($time);
74+
6775
$this->setQueue('default', 60);
6876
$pheanstalk = $this->queue->getPheanstalk();
6977
$pheanstalk->shouldReceive('useTube')->once()->with(m::type(TubeName::class));
7078
$pheanstalk->shouldReceive('useTube')->once()->with(m::type(TubeName::class));
71-
$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);
79+
$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);
7280

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

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

86+
Carbon::setTestNow();
7887
Str::createUuidsNormally();
7988
}
8089

tests/Queue/QueueDatabaseQueueUnitTest.php

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

33
namespace Illuminate\Tests\Queue;
44

5+
use Carbon\Carbon;
56
use Illuminate\Container\Container;
67
use Illuminate\Database\Connection;
78
use Illuminate\Queue\DatabaseQueue;
@@ -69,16 +70,19 @@ public function testDelayedPushProperlyPushesJobOntoDatabase()
6970
return $uuid;
7071
});
7172

73+
$time = Carbon::now();
74+
Carbon::setTestNow($time);
75+
7276
$queue = $this->getMockBuilder(DatabaseQueue::class)
7377
->onlyMethods(['currentTime'])
7478
->setConstructorArgs([$database = m::mock(Connection::class), 'table', 'default'])
7579
->getMock();
7680
$queue->expects($this->any())->method('currentTime')->willReturn('time');
7781
$queue->setContainer($container = m::spy(Container::class));
7882
$database->shouldReceive('table')->with('table')->andReturn($query = m::mock(stdClass::class));
79-
$query->shouldReceive('insertGetId')->once()->andReturnUsing(function ($array) use ($uuid) {
83+
$query->shouldReceive('insertGetId')->once()->andReturnUsing(function ($array) use ($uuid, $time) {
8084
$this->assertSame('default', $array['queue']);
81-
$this->assertSame(json_encode(['uuid' => $uuid, 'displayName' => 'foo', 'job' => 'foo', 'maxTries' => null, 'maxExceptions' => null, 'failOnTimeout' => false, 'backoff' => null, 'timeout' => null, 'data' => ['data']]), $array['payload']);
85+
$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']);
8286
$this->assertEquals(0, $array['attempts']);
8387
$this->assertNull($array['reserved_at']);
8488
$this->assertIsInt($array['available_at']);
@@ -88,6 +92,7 @@ public function testDelayedPushProperlyPushesJobOntoDatabase()
8892

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

95+
Carbon::setTestNow();
9196
Str::createUuidsNormally();
9297
}
9398

@@ -130,22 +135,25 @@ public function testBulkBatchPushesOntoDatabase()
130135
return $uuid;
131136
});
132137

138+
$time = Carbon::now();
139+
Carbon::setTestNow($time);
140+
133141
$database = m::mock(Connection::class);
134142
$queue = $this->getMockBuilder(DatabaseQueue::class)->onlyMethods(['currentTime', 'availableAt'])->setConstructorArgs([$database, 'table', 'default'])->getMock();
135143
$queue->expects($this->any())->method('currentTime')->willReturn('created');
136144
$queue->expects($this->any())->method('availableAt')->willReturn('available');
137145
$database->shouldReceive('table')->with('table')->andReturn($query = m::mock(stdClass::class));
138-
$query->shouldReceive('insert')->once()->andReturnUsing(function ($records) use ($uuid) {
146+
$query->shouldReceive('insert')->once()->andReturnUsing(function ($records) use ($uuid, $time) {
139147
$this->assertEquals([[
140148
'queue' => 'queue',
141-
'payload' => json_encode(['uuid' => $uuid, 'displayName' => 'foo', 'job' => 'foo', 'maxTries' => null, 'maxExceptions' => null, 'failOnTimeout' => false, 'backoff' => null, 'timeout' => null, 'data' => ['data']]),
149+
'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]),
142150
'attempts' => 0,
143151
'reserved_at' => null,
144152
'available_at' => 'available',
145153
'created_at' => 'created',
146154
], [
147155
'queue' => 'queue',
148-
'payload' => json_encode(['uuid' => $uuid, 'displayName' => 'bar', 'job' => 'bar', 'maxTries' => null, 'maxExceptions' => null, 'failOnTimeout' => false, 'backoff' => null, 'timeout' => null, 'data' => ['data']]),
156+
'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]),
149157
'attempts' => 0,
150158
'reserved_at' => null,
151159
'available_at' => 'available',
@@ -155,6 +163,7 @@ public function testBulkBatchPushesOntoDatabase()
155163

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

166+
Carbon::setTestNow();
158167
Str::createUuidsNormally();
159168
}
160169

tests/Queue/QueueRedisQueueTest.php

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,20 @@ public function testPushProperlyPushesJobOntoRedis()
2727
return $uuid;
2828
});
2929

30+
$time = Carbon::now();
31+
Carbon::setTestNow($time);
32+
3033
$queue = $this->getMockBuilder(RedisQueue::class)->onlyMethods(['getRandomId'])->setConstructorArgs([$redis = m::mock(Factory::class), 'default'])->getMock();
3134
$queue->expects($this->once())->method('getRandomId')->willReturn('foo');
3235
$queue->setContainer($container = m::spy(Container::class));
3336
$redis->shouldReceive('connection')->once()->andReturn($redis);
34-
$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]));
37+
$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]));
3538

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

43+
Carbon::setTestNow();
4044
Str::createUuidsNormally();
4145
}
4246

@@ -48,11 +52,14 @@ public function testPushProperlyPushesJobOntoRedisWithCustomPayloadHook()
4852
return $uuid;
4953
});
5054

55+
$time = Carbon::now();
56+
Carbon::setTestNow($time);
57+
5158
$queue = $this->getMockBuilder(RedisQueue::class)->onlyMethods(['getRandomId'])->setConstructorArgs([$redis = m::mock(Factory::class), 'default'])->getMock();
5259
$queue->expects($this->once())->method('getRandomId')->willReturn('foo');
5360
$queue->setContainer($container = m::spy(Container::class));
5461
$redis->shouldReceive('connection')->once()->andReturn($redis);
55-
$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]));
62+
$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]));
5663

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

6572
Queue::createPayloadUsing(null);
6673

74+
Carbon::setTestNow();
6775
Str::createUuidsNormally();
6876
}
6977

@@ -75,11 +83,14 @@ public function testPushProperlyPushesJobOntoRedisWithTwoCustomPayloadHook()
7583
return $uuid;
7684
});
7785

86+
$time = Carbon::now();
87+
Carbon::setTestNow($time);
88+
7889
$queue = $this->getMockBuilder(RedisQueue::class)->onlyMethods(['getRandomId'])->setConstructorArgs([$redis = m::mock(Factory::class), 'default'])->getMock();
7990
$queue->expects($this->once())->method('getRandomId')->willReturn('foo');
8091
$queue->setContainer($container = m::spy(Container::class));
8192
$redis->shouldReceive('connection')->once()->andReturn($redis);
82-
$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]));
93+
$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]));
8394

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

96107
Queue::createPayloadUsing(null);
97108

109+
Carbon::setTestNow();
98110
Str::createUuidsNormally();
99111
}
100112

@@ -106,6 +118,9 @@ public function testDelayedPushProperlyPushesJobOntoRedis()
106118
return $uuid;
107119
});
108120

121+
$time = Carbon::now();
122+
Carbon::setTestNow($time);
123+
109124
$queue = $this->getMockBuilder(RedisQueue::class)->onlyMethods(['availableAt', 'getRandomId'])->setConstructorArgs([$redis = m::mock(Factory::class), 'default'])->getMock();
110125
$queue->setContainer($container = m::spy(Container::class));
111126
$queue->expects($this->once())->method('getRandomId')->willReturn('foo');
@@ -115,13 +130,14 @@ public function testDelayedPushProperlyPushesJobOntoRedis()
115130
$redis->shouldReceive('zadd')->once()->with(
116131
'queues:default:delayed',
117132
2,
118-
json_encode(['uuid' => $uuid, 'displayName' => 'foo', 'job' => 'foo', 'maxTries' => null, 'maxExceptions' => null, 'failOnTimeout' => false, 'backoff' => null, 'timeout' => null, 'data' => ['data'], 'id' => 'foo', 'attempts' => 0])
133+
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])
119134
);
120135

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

140+
Carbon::setTestNow();
125141
Str::createUuidsNormally();
126142
}
127143

@@ -133,22 +149,24 @@ public function testDelayedPushWithDateTimeProperlyPushesJobOntoRedis()
133149
return $uuid;
134150
});
135151

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

142159
$redis->shouldReceive('connection')->once()->andReturn($redis);
143160
$redis->shouldReceive('zadd')->once()->with(
144161
'queues:default:delayed',
145-
2,
146-
json_encode(['uuid' => $uuid, 'displayName' => 'foo', 'job' => 'foo', 'maxTries' => null, 'maxExceptions' => null, 'failOnTimeout' => false, 'backoff' => null, 'timeout' => null, 'data' => ['data'], 'id' => 'foo', 'attempts' => 0])
162+
5,
163+
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])
147164
);
148165

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

169+
Carbon::setTestNow();
152170
Str::createUuidsNormally();
153171
}
154172
}

0 commit comments

Comments
 (0)