Skip to content

Commit 2d754b8

Browse files
committed
feat: return QueuePushResult from push() instead of bool
1 parent 8c41b17 commit 2d754b8

File tree

12 files changed

+194
-40
lines changed

12 files changed

+194
-40
lines changed

docs/basic-usage.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,12 @@ service('queue')->push('emails', 'email', ['message' => 'Email message goes here
160160

161161
We will be pushing `email` job to the `emails` queue.
162162

163+
As a result of calling the `push()` method, you will receive a `QueuePushResult` object, which you can inspect if needed. It provides the following information:
164+
165+
- `getStatus()`: Indicates whether the job was successfully added to the queue.
166+
- `getJobId()`: Returns the ID of the job that was added to the queue.
167+
- `getError()`: Returns any error that occurred if the job was not added.
168+
163169
### Sending chained jobs to the queue
164170

165171
Sending chained jobs is also simple and lets you specify the particular order of the job execution.
@@ -172,9 +178,11 @@ service('queue')->chain(function($chain) {
172178
});
173179
```
174180

175-
In the example above, we will send jobs to the `reports` and `emails` queue. First, we will generate a report for given user with the `generate-report` job, after this, we will send an email with `email` job.
181+
In the example above, we will send jobs to the `reports` and `emails` queues. First, we will generate a report for given user with the `generate-report` job, after this, we will send an email with `email` job.
176182
The `email` job will be executed only if the `generate-report` job was successful.
177183

184+
As with the `push()` method, calling the `chain()` method also returns a `QueuePushResult` object.
185+
178186
### Consuming the queue
179187

180188
Since we sent our sample job to queue `emails`, then we need to run the worker with the appropriate queue:

src/Handlers/BaseHandler.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
use CodeIgniter\Queue\Models\QueueJobFailedModel;
2323
use CodeIgniter\Queue\Payloads\ChainBuilder;
2424
use CodeIgniter\Queue\Payloads\PayloadMetadata;
25+
use CodeIgniter\Queue\QueuePushResult;
2526
use CodeIgniter\Queue\Traits\HasQueueValidation;
2627
use ReflectionException;
2728
use Throwable;
@@ -39,7 +40,7 @@ abstract class BaseHandler
3940

4041
abstract public function name(): string;
4142

42-
abstract public function push(string $queue, string $job, array $data, ?PayloadMetadata $metadata = null): ?string;
43+
abstract public function push(string $queue, string $job, array $data, ?PayloadMetadata $metadata = null): QueuePushResult;
4344

4445
abstract public function pop(string $queue, array $priorities): ?QueueJob;
4546

@@ -153,7 +154,7 @@ public function setPriority(string $priority): static
153154
*
154155
* @param Closure $callback Chain definition callback
155156
*/
156-
public function chain(Closure $callback): ?string
157+
public function chain(Closure $callback): QueuePushResult
157158
{
158159
$chainBuilder = new ChainBuilder($this);
159160
$callback($chainBuilder);

src/Handlers/DatabaseHandler.php

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
use CodeIgniter\Queue\Models\QueueJobModel;
2222
use CodeIgniter\Queue\Payloads\Payload;
2323
use CodeIgniter\Queue\Payloads\PayloadMetadata;
24+
use CodeIgniter\Queue\QueuePushResult;
2425
use ReflectionException;
2526
use Throwable;
2627

@@ -44,10 +45,8 @@ public function name(): string
4445

4546
/**
4647
* Add job to the queue.
47-
*
48-
* @throws ReflectionException
4948
*/
50-
public function push(string $queue, string $job, array $data, ?PayloadMetadata $metadata = null): ?string
49+
public function push(string $queue, string $job, array $data, ?PayloadMetadata $metadata = null): QueuePushResult
5150
{
5251
$this->validateJobAndPriority($queue, $job);
5352

@@ -62,9 +61,17 @@ public function push(string $queue, string $job, array $data, ?PayloadMetadata $
6261

6362
$this->priority = $this->delay = null;
6463

65-
$jobId = $this->jobModel->insert($queueJob);
64+
try {
65+
$jobId = $this->jobModel->insert($queueJob);
66+
} catch (Throwable $e) {
67+
return QueuePushResult::failure($e->getMessage());
68+
}
69+
70+
if ($jobId === 0) {
71+
return QueuePushResult::failure('Failed to insert job into the database.');
72+
}
6673

67-
return $jobId !== 0 ? (string) $jobId : null;
74+
return QueuePushResult::success($jobId);
6875
}
6976

7077
/**

src/Handlers/PredisHandler.php

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
use CodeIgniter\Queue\Interfaces\QueueInterface;
2323
use CodeIgniter\Queue\Payloads\Payload;
2424
use CodeIgniter\Queue\Payloads\PayloadMetadata;
25+
use CodeIgniter\Queue\QueuePushResult;
2526
use Exception;
2627
use Predis\Client;
2728
use Throwable;
@@ -59,13 +60,13 @@ public function name(): string
5960
/**
6061
* Add job to the queue.
6162
*/
62-
public function push(string $queue, string $job, array $data, ?PayloadMetadata $metadata = null): ?string
63+
public function push(string $queue, string $job, array $data, ?PayloadMetadata $metadata = null): QueuePushResult
6364
{
6465
$this->validateJobAndPriority($queue, $job);
6566

6667
helper('text');
6768

68-
$jobId = random_string('numeric', 16);
69+
$jobId = (int) random_string('numeric', 16);
6970
$availableAt = Time::now()->addSeconds($this->delay ?? 0);
7071

7172
$queueJob = new QueueJob([
@@ -78,11 +79,19 @@ public function push(string $queue, string $job, array $data, ?PayloadMetadata $
7879
'available_at' => $availableAt,
7980
]);
8081

81-
$result = $this->predis->zadd("queues:{$queue}:{$this->priority}", [json_encode($queueJob) => $availableAt->timestamp]);
82+
try {
83+
$result = $this->predis->zadd("queues:{$queue}:{$this->priority}", [json_encode($queueJob) => $availableAt->timestamp]);
84+
} catch (Throwable $e) {
85+
return QueuePushResult::failure('Unexpected Redis error: ' . $e->getMessage());
86+
} finally {
87+
$this->priority = $this->delay = null;
88+
}
8289

8390
$this->priority = $this->delay = null;
8491

85-
return $result > 0 ? $jobId : null;
92+
return $result > 0
93+
? QueuePushResult::success($jobId)
94+
: QueuePushResult::failure('Job already exists in the queue.');
8695
}
8796

8897
/**

src/Handlers/RedisHandler.php

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
use CodeIgniter\Queue\Interfaces\QueueInterface;
2323
use CodeIgniter\Queue\Payloads\Payload;
2424
use CodeIgniter\Queue\Payloads\PayloadMetadata;
25+
use CodeIgniter\Queue\QueuePushResult;
2526
use Redis;
2627
use RedisException;
2728
use Throwable;
@@ -76,14 +77,14 @@ public function name(): string
7677
*
7778
* @throws RedisException
7879
*/
79-
public function push(string $queue, string $job, array $data, ?PayloadMetadata $metadata = null): ?string
80+
public function push(string $queue, string $job, array $data, ?PayloadMetadata $metadata = null): QueuePushResult
8081
{
8182
$this->validateJobAndPriority($queue, $job);
8283

8384
helper('text');
8485

8586
$availableAt = Time::now()->addSeconds($this->delay ?? 0);
86-
$jobId = random_string('numeric', 16);
87+
$jobId = (int) random_string('numeric', 16);
8788

8889
$queueJob = new QueueJob([
8990
'id' => $jobId,
@@ -95,11 +96,21 @@ public function push(string $queue, string $job, array $data, ?PayloadMetadata $
9596
'available_at' => $availableAt,
9697
]);
9798

98-
$result = (int) $this->redis->zAdd("queues:{$queue}:{$this->priority}", $availableAt->timestamp, json_encode($queueJob));
99+
try {
100+
$result = $this->redis->zAdd("queues:{$queue}:{$this->priority}", $availableAt->timestamp, json_encode($queueJob));
101+
} catch (Throwable $e) {
102+
return QueuePushResult::failure('Unexpected Redis error: ' . $e->getMessage());
103+
} finally {
104+
$this->priority = $this->delay = null;
105+
}
99106

100-
$this->priority = $this->delay = null;
107+
if ($result === false) {
108+
return QueuePushResult::failure('Failed to add job to Redis.');
109+
}
101110

102-
return $result > 0 ? $jobId : null;
111+
return (int) $result > 0
112+
? QueuePushResult::success($jobId)
113+
: QueuePushResult::failure('Job already exists in the queue.');
103114
}
104115

105116
/**

src/Payloads/ChainBuilder.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
namespace CodeIgniter\Queue\Payloads;
1515

1616
use CodeIgniter\Queue\Handlers\BaseHandler;
17+
use CodeIgniter\Queue\QueuePushResult;
1718

1819
class ChainBuilder
1920
{
@@ -44,10 +45,10 @@ public function push(string $queue, string $jobName, array $data = []): ChainEle
4445
/**
4546
* Dispatch the chain of jobs
4647
*/
47-
public function dispatch(): ?string
48+
public function dispatch(): QueuePushResult
4849
{
4950
if ($this->payloads->count() === 0) {
50-
return null;
51+
return QueuePushResult::failure('No jobs to dispatch.');
5152
}
5253

5354
$current = $this->payloads->shift();

src/QueuePushResult.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* This file is part of CodeIgniter Queue.
7+
*
8+
* (c) CodeIgniter Foundation <[email protected]>
9+
*
10+
* For the full copyright and license information, please view
11+
* the LICENSE file that was distributed with this source code.
12+
*/
13+
14+
namespace CodeIgniter\Queue;
15+
16+
class QueuePushResult
17+
{
18+
public function __construct(
19+
protected readonly bool $success,
20+
protected readonly ?int $jobId = null,
21+
protected readonly ?string $error = null,
22+
) {
23+
}
24+
25+
public static function success(int $jobId): self
26+
{
27+
return new self(true, $jobId);
28+
}
29+
30+
public static function failure(?string $error = null): self
31+
{
32+
return new self(false, null, $error);
33+
}
34+
35+
public function getStatus(): bool
36+
{
37+
return $this->success;
38+
}
39+
40+
public function getJobId(): ?int
41+
{
42+
return $this->jobId;
43+
}
44+
45+
public function getError(): ?string
46+
{
47+
return $this->error;
48+
}
49+
}

tests/DatabaseHandlerTest.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ public function testPush(): void
8585
$handler = new DatabaseHandler($this->config);
8686
$result = $handler->push('queue', 'success', ['key' => 'value']);
8787

88-
$this->assertNotNull($result);
88+
$this->assertTrue($result->getStatus());
8989
$this->seeInDatabase('queue_jobs', [
9090
'queue' => 'queue',
9191
'payload' => json_encode(['job' => 'success', 'data' => ['key' => 'value'], 'metadata' => []]),
@@ -103,7 +103,7 @@ public function testPushWithPriority(): void
103103
$handler = new DatabaseHandler($this->config);
104104
$result = $handler->setPriority('high')->push('queue', 'success', ['key' => 'value']);
105105

106-
$this->assertNotNull($result);
106+
$this->assertTrue($result->getStatus());
107107
$this->seeInDatabase('queue_jobs', [
108108
'queue' => 'queue',
109109
'payload' => json_encode(['job' => 'success', 'data' => ['key' => 'value'], 'metadata' => []]),
@@ -122,7 +122,7 @@ public function testPushAndPopWithPriority(): void
122122
$handler = new DatabaseHandler($this->config);
123123
$result = $handler->push('queue', 'success', ['key1' => 'value1']);
124124

125-
$this->assertNotNull($result);
125+
$this->assertTrue($result->getStatus());
126126
$this->seeInDatabase('queue_jobs', [
127127
'queue' => 'queue',
128128
'payload' => json_encode(['job' => 'success', 'data' => ['key1' => 'value1'], 'metadata' => []]),
@@ -132,7 +132,7 @@ public function testPushAndPopWithPriority(): void
132132

133133
$result = $handler->setPriority('high')->push('queue', 'success', ['key2' => 'value2']);
134134

135-
$this->assertNotNull($result);
135+
$this->assertTrue($result->getStatus());
136136
$this->seeInDatabase('queue_jobs', [
137137
'queue' => 'queue',
138138
'payload' => json_encode(['job' => 'success', 'data' => ['key2' => 'value2'], 'metadata' => []]),
@@ -161,7 +161,7 @@ public function testPushWithDelay(): void
161161
$handler = new DatabaseHandler($this->config);
162162
$result = $handler->setDelay(MINUTE)->push('queue-delay', 'success', ['key' => 'value']);
163163

164-
$this->assertNotNull($result);
164+
$this->assertTrue($result->getStatus());
165165

166166
$availableAt = 1703859376;
167167

@@ -188,7 +188,7 @@ public function testChain(): void
188188
->push('queue', 'success', ['key2' => 'value2']);
189189
});
190190

191-
$this->assertNotNull($result);
191+
$this->assertTrue($result->getStatus());
192192
$this->seeInDatabase('queue_jobs', [
193193
'queue' => 'queue',
194194
'payload' => json_encode([
@@ -221,7 +221,7 @@ public function testChainWithPriorityAndDelay(): void
221221
->setDelay(120);
222222
});
223223

224-
$this->assertNotNull($result);
224+
$this->assertTrue($result->getStatus());
225225
$this->seeInDatabase('queue_jobs', [
226226
'queue' => 'queue',
227227
'payload' => json_encode([

tests/Payloads/ChainBuilderTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public function testChainWithSingleJob(): void
6363
$chain->push('queue', 'success', ['key' => 'value']);
6464
});
6565

66-
$this->assertNotNull($result);
66+
$this->assertTrue($result->getStatus());
6767
$this->seeInDatabase('queue_jobs', [
6868
'queue' => 'queue',
6969
'payload' => json_encode([
@@ -84,7 +84,7 @@ public function testEmptyChain(): void
8484
// No jobs added
8585
});
8686

87-
$this->assertNull($result);
87+
$this->assertFalse($result->getStatus());
8888
$this->seeInDatabase('queue_jobs', []);
8989
}
9090

@@ -99,7 +99,7 @@ public function testMultipleDifferentQueues(): void
9999
->push('queue2', 'success', ['key2' => 'value2']);
100100
});
101101

102-
$this->assertNotNull($result);
102+
$this->assertTrue($result->getStatus());
103103
$this->seeInDatabase('queue_jobs', [
104104
'queue' => 'queue1',
105105
'payload' => json_encode([
@@ -132,7 +132,7 @@ public function testChainWithManyJobs(): void
132132
->push('queue', 'success', ['key3' => 'value3']);
133133
});
134134

135-
$this->assertNotNull($result);
135+
$this->assertTrue($result->getStatus());
136136
$this->seeInDatabase('queue_jobs', [
137137
'queue' => 'queue',
138138
'payload' => json_encode([

0 commit comments

Comments
 (0)