@@ -29,14 +29,14 @@ public function __construct( BatchFactory $factory )
29
29
30
30
public function get ($ limit = 50 , $ before = null ): array
31
31
{
32
- if (!Redis::exists ('batches_list ' )) {
32
+ if (!Redis::connection ( config ( ' queue.batching.redis_connection ' , ' default ' ))-> exists ('batches_list ' )) {
33
33
return [];
34
34
}
35
35
36
- $ totalBatches = Redis::llen ('batches_list ' );
36
+ $ totalBatches = Redis::connection ( config ( ' queue.batching.redis_connection ' , ' default ' ))-> llen ('batches_list ' );
37
37
38
38
// If $before is specified, find its index
39
- $ beforeIndex = $ before ? array_search ($ before , Redis::lrange ('batches_list ' , 0 , -1 )) : null ;
39
+ $ beforeIndex = $ before ? array_search ($ before , Redis::connection ( config ( ' queue.batching.redis_connection ' , ' default ' ))-> lrange ('batches_list ' , 0 , -1 )) : null ;
40
40
41
41
// Determine the range to fetch
42
42
if ($ beforeIndex !== false && $ beforeIndex !== null ) {
@@ -48,10 +48,10 @@ public function get($limit = 50, $before = null): array
48
48
}
49
49
50
50
// Fetch only the required batch IDs
51
- $ batchIds = Redis::lrange ('batches_list ' , $ startIndex , $ rangeEnd );
51
+ $ batchIds = Redis::connection ( config ( ' queue.batching.redis_connection ' , ' default ' ))-> lrange ('batches_list ' , $ startIndex , $ rangeEnd );
52
52
53
53
// Use Redis pipeline to bulk fetch batch data
54
- $ responses = Redis::pipeline (function ($ pipe ) use ($ batchIds ) {
54
+ $ responses = Redis::connection ( config ( ' queue.batching.redis_connection ' , ' default ' ))-> pipeline (function ($ pipe ) use ($ batchIds ) {
55
55
foreach ($ batchIds as $ batchId ) {
56
56
$ pipe ->get ("batch: $ batchId " );
57
57
}
@@ -71,7 +71,7 @@ public function get($limit = 50, $before = null): array
71
71
72
72
public function find ( string $ batchId )
73
73
{
74
- $ data = Redis::get ( "batch: $ batchId " );
74
+ $ data = Redis::connection ( config ( ' queue.batching.redis_connection ' , ' default ' ))-> get ( "batch: $ batchId " );
75
75
76
76
if ( $ data === false ) {
77
77
// Return null or handle the case where the batch does not exist
@@ -99,31 +99,31 @@ public function store( PendingBatch $batch )
99
99
'finished_at ' => null ,
100
100
];
101
101
102
- Redis::set ( "batch: $ id " , json_encode ( $ batchData ) );
103
- Redis::rpush ( 'batches_list ' , $ id ); // Add the batch ID to the list
102
+ Redis::connection ( config ( ' queue.batching.redis_connection ' , ' default ' ))-> set ( "batch: $ id " , json_encode ( $ batchData ) );
103
+ Redis::connection ( config ( ' queue.batching.redis_connection ' , ' default ' ))-> rpush ( 'batches_list ' , $ id ); // Add the batch ID to the list
104
104
105
105
return $ this ->find ( $ id );
106
106
}
107
107
108
108
public function incrementTotalJobs ( string $ batchId , int $ amount )
109
109
{
110
110
return $ this ->executeWithLock ( "lock:batch: $ batchId " , function () use ( $ batchId , $ amount ) {
111
- $ data = Redis::get ( "batch: $ batchId " );
111
+ $ data = Redis::connection ( config ( ' queue.batching.redis_connection ' , ' default ' ))-> get ( "batch: $ batchId " );
112
112
if ( $ data === false ) {
113
113
Log::error ( "Batch not found for incrementTotalJobs: " . $ batchId );
114
114
return new UpdatedBatchJobCounts ( 0 , 0 );
115
115
}
116
116
$ batchData = json_decode ( $ data , true );
117
117
$ batchData ['total_jobs ' ] += $ amount ;
118
118
$ batchData ['pending_jobs ' ] += $ amount ;
119
- Redis::set ( "batch: $ batchId " , json_encode ( $ batchData ) );
119
+ Redis::connection ( config ( ' queue.batching.redis_connection ' , ' default ' ))-> set ( "batch: $ batchId " , json_encode ( $ batchData ) );
120
120
return new UpdatedBatchJobCounts ( $ batchData ['pending_jobs ' ], $ batchData ['failed_jobs ' ] );
121
121
}, 100 , 200 );
122
122
}
123
123
124
124
protected function acquireLock ( string $ key ): bool
125
125
{
126
- $ isAcquired = Redis::set ( $ key , true , 'EX ' , $ this ->lockTimeout , 'NX ' );
126
+ $ isAcquired = Redis::connection ( config ( ' queue.batching.redis_connection ' , ' default ' ))-> set ( $ key , true , 'EX ' , $ this ->lockTimeout , 'NX ' );
127
127
return (bool )$ isAcquired ;
128
128
}
129
129
@@ -158,29 +158,29 @@ protected function executeWithLock( string $lockKey, Closure $callback, $retryCo
158
158
159
159
protected function releaseLock ( string $ key )
160
160
{
161
- Redis::del ( $ key );
161
+ Redis::connection ( config ( ' queue.batching.redis_connection ' , ' default ' ))-> del ( $ key );
162
162
}
163
163
164
164
public function incrementFailedJobs ( string $ batchId , string $ jobId )
165
165
{
166
166
return $ this ->executeWithLock ( "lock:batch: $ batchId " , function () use ( $ batchId , $ jobId ) {
167
- $ data = Redis::get ( "batch: $ batchId " );
167
+ $ data = Redis::connection ( config ( ' queue.batching.redis_connection ' , ' default ' ))-> get ( "batch: $ batchId " );
168
168
if ( $ data === false ) {
169
169
Log::error ( "Batch not found for incrementFailedJobs: " . $ batchId );
170
170
return new UpdatedBatchJobCounts ( 0 , 0 );
171
171
}
172
172
$ batchData = json_decode ( $ data , true );
173
173
$ batchData ['failed_jobs ' ]++;
174
174
$ batchData ['failed_job_ids ' ][] = $ jobId ;
175
- Redis::set ( "batch: $ batchId " , json_encode ( $ batchData ) );
175
+ Redis::connection ( config ( ' queue.batching.redis_connection ' , ' default ' ))-> set ( "batch: $ batchId " , json_encode ( $ batchData ) );
176
176
return new UpdatedBatchJobCounts ( $ batchData ['pending_jobs ' ], $ batchData ['failed_jobs ' ] );
177
177
}, 100 , 200 );
178
178
}
179
179
180
180
public function decrementPendingJobs ( string $ batchId , string $ jobId )
181
181
{
182
182
return $ this ->executeWithLock ( "lock:batch: $ batchId " , function () use ( $ batchId , $ jobId ) {
183
- $ data = Redis::get ( "batch: $ batchId " );
183
+ $ data = Redis::connection ( config ( ' queue.batching.redis_connection ' , ' default ' ))-> get ( "batch: $ batchId " );
184
184
if ( $ data === false ) {
185
185
Log::error ( "Batch not found for decrementPendingJobs: " . $ batchId );
186
186
return new UpdatedBatchJobCounts ( 0 , 0 );
@@ -195,7 +195,7 @@ public function decrementPendingJobs( string $batchId, string $jobId )
195
195
Log::warning ("Attempted to decrement pending_jobs below 0 for batch: " . $ batchId );
196
196
}
197
197
198
- Redis::set ( "batch: $ batchId " , json_encode ( $ batchData ) );
198
+ Redis::connection ( config ( ' queue.batching.redis_connection ' , ' default ' ))-> set ( "batch: $ batchId " , json_encode ( $ batchData ) );
199
199
return new UpdatedBatchJobCounts ( $ batchData ['pending_jobs ' ], $ batchData ['failed_jobs ' ] );
200
200
}, 100 , 200 );
201
201
}
@@ -204,7 +204,7 @@ public function decrementPendingJobs( string $batchId, string $jobId )
204
204
public function markAsFinished ( string $ batchId )
205
205
{
206
206
return $ this ->executeWithLock ( "lock:batch: $ batchId " , function () use ( $ batchId ) {
207
- $ data = Redis::get ( "batch: $ batchId " );
207
+ $ data = Redis::connection ( config ( ' queue.batching.redis_connection ' , ' default ' ))-> get ( "batch: $ batchId " );
208
208
209
209
if ( $ data === false ) {
210
210
Log::debug ( "Batch not found for markAsFinished: " . $ batchId );
@@ -214,7 +214,7 @@ public function markAsFinished( string $batchId )
214
214
$ batchData = json_decode ( $ data , true );
215
215
// Convert finished_at to a Unix timestamp before storing
216
216
$ batchData ['finished_at ' ] = CarbonImmutable::now ()->getTimestamp ();
217
- Redis::set ( "batch: $ batchId " , json_encode ( $ batchData ) );
217
+ Redis::connection ( config ( ' queue.batching.redis_connection ' , ' default ' ))-> set ( "batch: $ batchId " , json_encode ( $ batchData ) );
218
218
219
219
Log::debug ( "Batch marked as finished: " . $ batchId . " with finished_at: " . $ batchData ['finished_at ' ] );
220
220
}, 100 , 200 );
@@ -223,13 +223,13 @@ public function markAsFinished( string $batchId )
223
223
224
224
public function delete ( string $ batchId )
225
225
{
226
- if ( !Redis::exists ( "batch: $ batchId " ) ) {
226
+ if ( !Redis::connection ( config ( ' queue.batching.redis_connection ' , ' default ' ))-> exists ( "batch: $ batchId " ) ) {
227
227
// Handle the case where the batch does not exist
228
228
return ;
229
229
}
230
230
231
- Redis::del ( "batch: $ batchId " );
232
- Redis::lrem ( 'batches_list ' , 0 , $ batchId );
231
+ Redis::connection ( config ( ' queue.batching.redis_connection ' , ' default ' ))-> del ( "batch: $ batchId " );
232
+ Redis::connection ( config ( ' queue.batching.redis_connection ' , ' default ' ))-> lrem ( 'batches_list ' , 0 , $ batchId );
233
233
}
234
234
235
235
protected function serialize ( $ value )
@@ -242,34 +242,34 @@ protected function unserialize( $serialized )
242
242
return unserialize ( $ serialized );
243
243
}
244
244
245
- protected function toBatch ( $ data ): Batch
245
+ protected function toBatch ( $ batch ): Batch
246
246
{
247
247
return $ this ->factory ->make (
248
248
$ this ,
249
- $ data ['id ' ],
250
- $ data ['name ' ],
251
- (int )$ data ['total_jobs ' ],
252
- (int )$ data ['pending_jobs ' ],
253
- (int )$ data ['failed_jobs ' ],
254
- $ data ['failed_job_ids ' ],
255
- $ this ->unserialize ( $ data ['options ' ] ),
256
- CarbonImmutable::createFromTimestamp ( $ data ['created_at ' ] ),
257
- isset ( $ data ['cancelled_at ' ] ) ? CarbonImmutable::createFromTimestamp ( $ data ['cancelled_at ' ] ) : null ,
258
- isset ( $ data ['finished_at ' ] ) ? CarbonImmutable::createFromTimestamp ( $ data ['finished_at ' ] ) : null
249
+ $ batch ['id ' ],
250
+ $ batch ['name ' ],
251
+ (int )$ batch ['total_jobs ' ],
252
+ (int )$ batch ['pending_jobs ' ],
253
+ (int )$ batch ['failed_jobs ' ],
254
+ $ batch ['failed_job_ids ' ],
255
+ $ this ->unserialize ( $ batch ['options ' ] ),
256
+ CarbonImmutable::createFromTimestamp ( $ batch ['created_at ' ] ),
257
+ isset ( $ batch ['cancelled_at ' ] ) ? CarbonImmutable::createFromTimestamp ( $ batch ['cancelled_at ' ] ) : null ,
258
+ isset ( $ batch ['finished_at ' ] ) ? CarbonImmutable::createFromTimestamp ( $ batch ['finished_at ' ] ) : null
259
259
);
260
260
}
261
261
262
262
public function cancel ( string $ batchId )
263
263
{
264
264
$ this ->executeWithLock ( "lock:batch: $ batchId " , function () use ( $ batchId ) {
265
- $ data = Redis::get ( "batch: $ batchId " );
265
+ $ data = Redis::connection ( config ( ' queue.batching.redis_connection ' , ' default ' ))-> get ( "batch: $ batchId " );
266
266
if ( $ data === false ) {
267
267
return ;
268
268
}
269
269
$ batchData = json_decode ( $ data , true );
270
270
// Convert cancelled_at to a Unix timestamp before storing
271
271
$ batchData ['cancelled_at ' ] = CarbonImmutable::now ()->getTimestamp ();
272
- Redis::set ( "batch: $ batchId " , json_encode ( $ batchData ) );
272
+ Redis::connection ( config ( ' queue.batching.redis_connection ' , ' default ' ))-> set ( "batch: $ batchId " , json_encode ( $ batchData ) );
273
273
}, 100 , 200 ); // Retry 100 times with 200 milliseconds between retries
274
274
}
275
275
@@ -295,14 +295,14 @@ public function pruneCancelled( DateTimeInterface $before )
295
295
296
296
protected function pruneBatches ( DateTimeInterface $ before , $ isFinished = null , $ isCancelled = false )
297
297
{
298
- $ batchIds = Redis::lrange ( 'batches_list ' , 0 , -1 );
298
+ $ batchIds = Redis::connection ( config ( ' queue.batching.redis_connection ' , ' default ' ))-> lrange ( 'batches_list ' , 0 , -1 );
299
299
$ totalDeleted = 0 ;
300
300
301
301
foreach ( $ batchIds as $ batchId ) {
302
- $ data = Redis::get ( "batch: $ batchId " );
302
+ $ data = Redis::connection ( config ( ' queue.batching.redis_connection ' , ' default ' ))-> get ( "batch: $ batchId " );
303
303
304
304
if ( $ data === false ) {
305
- Redis::lrem ( 'batches_list ' , 0 , $ batchId );
305
+ Redis::connection ( config ( ' queue.batching.redis_connection ' , ' default ' ))-> lrem ( 'batches_list ' , 0 , $ batchId );
306
306
continue ;
307
307
}
308
308
@@ -323,8 +323,8 @@ protected function pruneBatches( DateTimeInterface $before, $isFinished = null,
323
323
}
324
324
325
325
if ( $ shouldBeDeleted ) {
326
- Redis::del ( "batch: $ batchId " );
327
- Redis::lrem ( 'batches_list ' , 0 , $ batchId );
326
+ Redis::connection ( config ( ' queue.batching.redis_connection ' , ' default ' ))-> del ( "batch: $ batchId " );
327
+ Redis::connection ( config ( ' queue.batching.redis_connection ' , ' default ' ))-> lrem ( 'batches_list ' , 0 , $ batchId );
328
328
$ totalDeleted ++;
329
329
}
330
330
}
0 commit comments