6
6
use Carbon \Carbon ;
7
7
use Illuminate \Database \Connection ;
8
8
use Illuminate \Queue \Jobs \DatabaseJob ;
9
+ use Illuminate \Queue \Jobs \DatabaseJobRecord ;
9
10
use Illuminate \Contracts \Queue \Queue as QueueContract ;
10
11
11
12
class DatabaseQueue extends Queue implements QueueContract
@@ -36,23 +37,23 @@ class DatabaseQueue extends Queue implements QueueContract
36
37
*
37
38
* @var int|null
38
39
*/
39
- protected $ expire = 60 ;
40
+ protected $ retryAfter = 60 ;
40
41
41
42
/**
42
43
* Create a new database queue instance.
43
44
*
44
45
* @param \Illuminate\Database\Connection $database
45
46
* @param string $table
46
47
* @param string $default
47
- * @param int $expire
48
+ * @param int $retryAfter
48
49
* @return void
49
50
*/
50
- public function __construct (Connection $ database , $ table , $ default = 'default ' , $ expire = 60 )
51
+ public function __construct (Connection $ database , $ table , $ default = 'default ' , $ retryAfter = 60 )
51
52
{
52
53
$ this ->table = $ table ;
53
- $ this ->expire = $ expire ;
54
54
$ this ->default = $ default ;
55
55
$ this ->database = $ database ;
56
+ $ this ->retryAfter = $ retryAfter ;
56
57
}
57
58
58
59
/**
@@ -78,7 +79,7 @@ public function size($queue = null)
78
79
*/
79
80
public function push ($ job , $ data = '' , $ queue = null )
80
81
{
81
- return $ this ->pushToDatabase (0 , $ queue , $ this ->createPayload ($ job , $ data ));
82
+ return $ this ->pushToDatabase ($ queue , $ this ->createPayload ($ job , $ data ));
82
83
}
83
84
84
85
/**
@@ -91,7 +92,7 @@ public function push($job, $data = '', $queue = null)
91
92
*/
92
93
public function pushRaw ($ payload , $ queue = null , array $ options = [])
93
94
{
94
- return $ this ->pushToDatabase (0 , $ queue , $ payload );
95
+ return $ this ->pushToDatabase ($ queue , $ payload );
95
96
}
96
97
97
98
/**
@@ -105,7 +106,7 @@ public function pushRaw($payload, $queue = null, array $options = [])
105
106
*/
106
107
public function later ($ delay , $ job , $ data = '' , $ queue = null )
107
108
{
108
- return $ this ->pushToDatabase ($ delay , $ queue , $ this ->createPayload ($ job , $ data ));
109
+ return $ this ->pushToDatabase ($ queue , $ this ->createPayload ($ job , $ data ), $ delay );
109
110
}
110
111
111
112
/**
@@ -120,46 +121,63 @@ public function bulk($jobs, $data = '', $queue = null)
120
121
{
121
122
$ queue = $ this ->getQueue ($ queue );
122
123
123
- $ availableAt = $ this ->getAvailableAt ( 0 );
124
+ $ availableAt = $ this ->availableAt ( );
124
125
125
- $ records = array_map (function ($ job ) use ($ queue , $ data , $ availableAt ) {
126
- return $ this ->buildDatabaseRecord (
127
- $ queue , $ this ->createPayload ($ job , $ data ), $ availableAt
128
- );
129
- }, (array ) $ jobs );
130
-
131
- return $ this ->database ->table ($ this ->table )->insert ($ records );
126
+ return $ this ->database ->table ($ this ->table )->insert (collect ((array ) $ jobs )->map (
127
+ function ($ job ) use ($ queue , $ data , $ availableAt ) {
128
+ return $ this ->buildDatabaseRecord ($ queue , $ this ->createPayload ($ job , $ data ), $ availableAt );
129
+ }
130
+ )->all ());
132
131
}
133
132
134
133
/**
135
134
* Release a reserved job back onto the queue.
136
135
*
137
136
* @param string $queue
138
- * @param \StdClass $job
137
+ * @param \Illuminate\Queue\Jobs\DatabaseJobRecord $job
139
138
* @param int $delay
140
139
* @return mixed
141
140
*/
142
141
public function release ($ queue , $ job , $ delay )
143
142
{
144
- return $ this ->pushToDatabase ($ delay , $ queue , $ job ->payload , $ job ->attempts );
143
+ return $ this ->pushToDatabase ($ queue , $ job ->payload , $ delay , $ job ->attempts );
145
144
}
146
145
147
146
/**
148
147
* Push a raw payload to the database with a given delay.
149
148
*
150
- * @param \DateTime|int $delay
151
149
* @param string|null $queue
152
150
* @param string $payload
151
+ * @param \DateTime|int $delay
153
152
* @param int $attempts
154
153
* @return mixed
155
154
*/
156
- protected function pushToDatabase ($ delay , $ queue , $ payload , $ attempts = 0 )
155
+ protected function pushToDatabase ($ queue , $ payload , $ delay = 0 , $ attempts = 0 )
157
156
{
158
- $ attributes = $ this ->buildDatabaseRecord (
159
- $ this ->getQueue ($ queue ), $ payload , $ this ->getAvailableAt ($ delay ), $ attempts
160
- );
157
+ return $ this ->database ->table ($ this ->table )->insertGetId ($ this ->buildDatabaseRecord (
158
+ $ this ->getQueue ($ queue ), $ payload , $ this ->availableAt ($ delay ), $ attempts
159
+ ));
160
+ }
161
161
162
- return $ this ->database ->table ($ this ->table )->insertGetId ($ attributes );
162
+ /**
163
+ * Create an array to insert for the given job.
164
+ *
165
+ * @param string|null $queue
166
+ * @param string $payload
167
+ * @param int $availableAt
168
+ * @param int $attempts
169
+ * @return array
170
+ */
171
+ protected function buildDatabaseRecord ($ queue , $ payload , $ availableAt , $ attempts = 0 )
172
+ {
173
+ return [
174
+ 'queue ' => $ queue ,
175
+ 'payload ' => $ payload ,
176
+ 'attempts ' => $ attempts ,
177
+ 'reserved_at ' => null ,
178
+ 'available_at ' => $ availableAt ,
179
+ 'created_at ' => $ this ->currentTime (),
180
+ ];
163
181
}
164
182
165
183
/**
@@ -175,13 +193,7 @@ public function pop($queue = null)
175
193
$ this ->database ->beginTransaction ();
176
194
177
195
if ($ job = $ this ->getNextAvailableJob ($ queue )) {
178
- $ job = $ this ->markJobAsReserved ($ job );
179
-
180
- $ this ->database ->commit ();
181
-
182
- return new DatabaseJob (
183
- $ this ->container , $ this , $ job , $ queue
184
- );
196
+ return $ this ->marshalJob ($ queue , $ job );
185
197
}
186
198
187
199
$ this ->database ->commit ();
@@ -191,7 +203,7 @@ public function pop($queue = null)
191
203
* Get the next available job for the queue.
192
204
*
193
205
* @param string|null $queue
194
- * @return \StdClass |null
206
+ * @return \Illuminate\Queue\Jobs\DatabaseJobRecord |null
195
207
*/
196
208
protected function getNextAvailableJob ($ queue )
197
209
{
@@ -205,7 +217,7 @@ protected function getNextAvailableJob($queue)
205
217
->orderBy ('id ' , 'asc ' )
206
218
->first ();
207
219
208
- return $ job ? ( object ) $ job : null ;
220
+ return $ job ? new DatabaseJobRecord (( object ) $ job) : null ;
209
221
}
210
222
211
223
/**
@@ -217,8 +229,8 @@ protected function getNextAvailableJob($queue)
217
229
protected function isAvailable ($ query )
218
230
{
219
231
$ query ->where (function ($ query ) {
220
- $ query ->whereNull ('reserved_at ' );
221
- $ query ->where ('available_at ' , '<= ' , $ this ->currentTime ());
232
+ $ query ->whereNull ('reserved_at ' )
233
+ ->where ('available_at ' , '<= ' , $ this ->currentTime ());
222
234
});
223
235
}
224
236
@@ -230,27 +242,42 @@ protected function isAvailable($query)
230
242
*/
231
243
protected function isReservedButExpired ($ query )
232
244
{
233
- $ expiration = Carbon::now ()->subSeconds ($ this ->expire )->getTimestamp ();
245
+ $ expiration = Carbon::now ()->subSeconds ($ this ->retryAfter )->getTimestamp ();
234
246
235
247
$ query ->orWhere (function ($ query ) use ($ expiration ) {
236
248
$ query ->where ('reserved_at ' , '<= ' , $ expiration );
237
249
});
238
250
}
239
251
252
+ /**
253
+ * Marshal the reserved job into a DatabaseJob instance.
254
+ *
255
+ * @param string $queue
256
+ * @param \Illuminate\Queue\Jobs\DatabaseJobRecord $job
257
+ * @return \Illuminate\Queue\Jobs\DatabaseJob
258
+ */
259
+ protected function marshalJob ($ queue , $ job )
260
+ {
261
+ $ job = $ this ->markJobAsReserved ($ job );
262
+
263
+ $ this ->database ->commit ();
264
+
265
+ return new DatabaseJob (
266
+ $ this ->container , $ this , $ job , $ queue
267
+ );
268
+ }
269
+
240
270
/**
241
271
* Mark the given job ID as reserved.
242
272
*
243
- * @param \stdClass $job
244
- * @return \stdClass
273
+ * @param \Illuminate\Queue\Jobs\DatabaseJobRecord $job
274
+ * @return \Illuminate\Queue\Jobs\DatabaseJobRecord
245
275
*/
246
276
protected function markJobAsReserved ($ job )
247
277
{
248
- $ job ->attempts = $ job ->attempts + 1 ;
249
- $ job ->reserved_at = $ this ->currentTime ();
250
-
251
278
$ this ->database ->table ($ this ->table )->where ('id ' , $ job ->id )->update ([
252
- 'reserved_at ' => $ job ->reserved_at ,
253
- 'attempts ' => $ job ->attempts ,
279
+ 'reserved_at ' => $ job ->touch () ,
280
+ 'attempts ' => $ job ->increment () ,
254
281
]);
255
282
256
283
return $ job ;
@@ -274,40 +301,6 @@ public function deleteReserved($queue, $id)
274
301
$ this ->database ->commit ();
275
302
}
276
303
277
- /**
278
- * Get the "available at" UNIX timestamp.
279
- *
280
- * @param \DateTime|int $delay
281
- * @return int
282
- */
283
- protected function getAvailableAt ($ delay )
284
- {
285
- $ availableAt = $ delay instanceof DateTime ? $ delay : Carbon::now ()->addSeconds ($ delay );
286
-
287
- return $ availableAt ->getTimestamp ();
288
- }
289
-
290
- /**
291
- * Create an array to insert for the given job.
292
- *
293
- * @param string|null $queue
294
- * @param string $payload
295
- * @param int $availableAt
296
- * @param int $attempts
297
- * @return array
298
- */
299
- protected function buildDatabaseRecord ($ queue , $ payload , $ availableAt , $ attempts = 0 )
300
- {
301
- return [
302
- 'queue ' => $ queue ,
303
- 'attempts ' => $ attempts ,
304
- 'reserved_at ' => null ,
305
- 'available_at ' => $ availableAt ,
306
- 'created_at ' => $ this ->currentTime (),
307
- 'payload ' => $ payload ,
308
- ];
309
- }
310
-
311
304
/**
312
305
* Get the queue or return the default.
313
306
*
0 commit comments