Skip to content

Commit 6db8d35

Browse files
committed
Add support for optional configuration option redis_connection to define what redis connection to use to store batch related data in. Defaults to 'default' if not set.
1 parent d7d36b0 commit 6db8d35

File tree

2 files changed

+40
-39
lines changed

2 files changed

+40
-39
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ Set the `database` key under the `batching` section to `'redis'`. Without this a
2828
~~~php
2929
'batching' => [
3030
'database' => 'redis', // Change this from 'mysql' to 'redis'
31+
'redis_connection' => 'default', // here you can define what redis connection to store batch related data in. Defaults to 'default' if not set.
3132
'table' => 'job_batches',
3233
],
3334
~~~

src/Repositories/RedisBatchRepository.php

+39-39
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,14 @@ public function __construct( BatchFactory $factory )
2929

3030
public function get($limit = 50, $before = null): array
3131
{
32-
if (!Redis::exists('batches_list')) {
32+
if (!Redis::connection(config('queue.batching.redis_connection', 'default'))->exists('batches_list')) {
3333
return [];
3434
}
3535

36-
$totalBatches = Redis::llen('batches_list');
36+
$totalBatches = Redis::connection(config('queue.batching.redis_connection', 'default'))->llen('batches_list');
3737

3838
// 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;
4040

4141
// Determine the range to fetch
4242
if ($beforeIndex !== false && $beforeIndex !== null) {
@@ -48,10 +48,10 @@ public function get($limit = 50, $before = null): array
4848
}
4949

5050
// 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);
5252

5353
// 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) {
5555
foreach ($batchIds as $batchId) {
5656
$pipe->get("batch:$batchId");
5757
}
@@ -71,7 +71,7 @@ public function get($limit = 50, $before = null): array
7171

7272
public function find( string $batchId )
7373
{
74-
$data = Redis::get( "batch:$batchId" );
74+
$data = Redis::connection(config('queue.batching.redis_connection', 'default'))->get( "batch:$batchId" );
7575

7676
if ( $data === false ) {
7777
// Return null or handle the case where the batch does not exist
@@ -99,31 +99,31 @@ public function store( PendingBatch $batch )
9999
'finished_at' => null,
100100
];
101101

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
104104

105105
return $this->find( $id );
106106
}
107107

108108
public function incrementTotalJobs( string $batchId, int $amount )
109109
{
110110
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" );
112112
if ( $data === false ) {
113113
Log::error( "Batch not found for incrementTotalJobs: " . $batchId );
114114
return new UpdatedBatchJobCounts( 0, 0 );
115115
}
116116
$batchData = json_decode( $data, true );
117117
$batchData['total_jobs'] += $amount;
118118
$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 ) );
120120
return new UpdatedBatchJobCounts( $batchData['pending_jobs'], $batchData['failed_jobs'] );
121121
}, 100, 200 );
122122
}
123123

124124
protected function acquireLock( string $key ): bool
125125
{
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' );
127127
return (bool)$isAcquired;
128128
}
129129

@@ -158,29 +158,29 @@ protected function executeWithLock( string $lockKey, Closure $callback, $retryCo
158158

159159
protected function releaseLock( string $key )
160160
{
161-
Redis::del( $key );
161+
Redis::connection(config('queue.batching.redis_connection', 'default'))->del( $key );
162162
}
163163

164164
public function incrementFailedJobs( string $batchId, string $jobId )
165165
{
166166
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" );
168168
if ( $data === false ) {
169169
Log::error( "Batch not found for incrementFailedJobs: " . $batchId );
170170
return new UpdatedBatchJobCounts( 0, 0 );
171171
}
172172
$batchData = json_decode( $data, true );
173173
$batchData['failed_jobs']++;
174174
$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 ) );
176176
return new UpdatedBatchJobCounts( $batchData['pending_jobs'], $batchData['failed_jobs'] );
177177
}, 100, 200 );
178178
}
179179

180180
public function decrementPendingJobs( string $batchId, string $jobId )
181181
{
182182
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" );
184184
if ( $data === false ) {
185185
Log::error( "Batch not found for decrementPendingJobs: " . $batchId );
186186
return new UpdatedBatchJobCounts( 0, 0 );
@@ -195,7 +195,7 @@ public function decrementPendingJobs( string $batchId, string $jobId )
195195
Log::warning("Attempted to decrement pending_jobs below 0 for batch: " . $batchId);
196196
}
197197

198-
Redis::set( "batch:$batchId", json_encode( $batchData ) );
198+
Redis::connection(config('queue.batching.redis_connection', 'default'))->set( "batch:$batchId", json_encode( $batchData ) );
199199
return new UpdatedBatchJobCounts( $batchData['pending_jobs'], $batchData['failed_jobs'] );
200200
}, 100, 200 );
201201
}
@@ -204,7 +204,7 @@ public function decrementPendingJobs( string $batchId, string $jobId )
204204
public function markAsFinished( string $batchId )
205205
{
206206
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" );
208208

209209
if ( $data === false ) {
210210
Log::debug( "Batch not found for markAsFinished: " . $batchId );
@@ -214,7 +214,7 @@ public function markAsFinished( string $batchId )
214214
$batchData = json_decode( $data, true );
215215
// Convert finished_at to a Unix timestamp before storing
216216
$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 ) );
218218

219219
Log::debug( "Batch marked as finished: " . $batchId . " with finished_at: " . $batchData['finished_at'] );
220220
}, 100, 200 );
@@ -223,13 +223,13 @@ public function markAsFinished( string $batchId )
223223

224224
public function delete( string $batchId )
225225
{
226-
if ( !Redis::exists( "batch:$batchId" ) ) {
226+
if ( !Redis::connection(config('queue.batching.redis_connection', 'default'))->exists( "batch:$batchId" ) ) {
227227
// Handle the case where the batch does not exist
228228
return;
229229
}
230230

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 );
233233
}
234234

235235
protected function serialize( $value )
@@ -242,34 +242,34 @@ protected function unserialize( $serialized )
242242
return unserialize( $serialized );
243243
}
244244

245-
protected function toBatch( $data ): Batch
245+
protected function toBatch( $batch ): Batch
246246
{
247247
return $this->factory->make(
248248
$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
259259
);
260260
}
261261

262262
public function cancel( string $batchId )
263263
{
264264
$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" );
266266
if ( $data === false ) {
267267
return;
268268
}
269269
$batchData = json_decode( $data, true );
270270
// Convert cancelled_at to a Unix timestamp before storing
271271
$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 ) );
273273
}, 100, 200 ); // Retry 100 times with 200 milliseconds between retries
274274
}
275275

@@ -295,14 +295,14 @@ public function pruneCancelled( DateTimeInterface $before )
295295

296296
protected function pruneBatches( DateTimeInterface $before, $isFinished = null, $isCancelled = false )
297297
{
298-
$batchIds = Redis::lrange( 'batches_list', 0, -1 );
298+
$batchIds = Redis::connection(config('queue.batching.redis_connection', 'default'))->lrange( 'batches_list', 0, -1 );
299299
$totalDeleted = 0;
300300

301301
foreach ( $batchIds as $batchId ) {
302-
$data = Redis::get( "batch:$batchId" );
302+
$data = Redis::connection(config('queue.batching.redis_connection', 'default'))->get( "batch:$batchId" );
303303

304304
if ( $data === false ) {
305-
Redis::lrem( 'batches_list', 0, $batchId );
305+
Redis::connection(config('queue.batching.redis_connection', 'default'))->lrem( 'batches_list', 0, $batchId );
306306
continue;
307307
}
308308

@@ -323,8 +323,8 @@ protected function pruneBatches( DateTimeInterface $before, $isFinished = null,
323323
}
324324

325325
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 );
328328
$totalDeleted++;
329329
}
330330
}

0 commit comments

Comments
 (0)