Skip to content

Commit 4f8ca10

Browse files
committed
feat: return jobID after push instead of the bool value
1 parent 39f0a8b commit 4f8ca10

File tree

10 files changed

+41
-35
lines changed

10 files changed

+41
-35
lines changed

src/Handlers/BaseHandler.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ abstract class BaseHandler
3939

4040
abstract public function name(): string;
4141

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

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

@@ -153,7 +153,7 @@ public function setPriority(string $priority): static
153153
*
154154
* @param Closure $callback Chain definition callback
155155
*/
156-
public function chain(Closure $callback): bool
156+
public function chain(Closure $callback): ?string
157157
{
158158
$chainBuilder = new ChainBuilder($this);
159159
$callback($chainBuilder);

src/Handlers/DatabaseHandler.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public function name(): string
4747
*
4848
* @throws ReflectionException
4949
*/
50-
public function push(string $queue, string $job, array $data, ?PayloadMetadata $metadata = null): bool
50+
public function push(string $queue, string $job, array $data, ?PayloadMetadata $metadata = null): ?string
5151
{
5252
$this->validateJobAndPriority($queue, $job);
5353

@@ -62,7 +62,9 @@ public function push(string $queue, string $job, array $data, ?PayloadMetadata $
6262

6363
$this->priority = $this->delay = null;
6464

65-
return $this->jobModel->insert($queueJob, false);
65+
$jobId = $this->jobModel->insert($queueJob);
66+
67+
return $jobId !== 0 ? (string) $jobId : null;
6668
}
6769

6870
/**

src/Handlers/PredisHandler.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,16 +59,18 @@ public function name(): string
5959
/**
6060
* Add job to the queue.
6161
*/
62-
public function push(string $queue, string $job, array $data, ?PayloadMetadata $metadata = null): bool
62+
public function push(string $queue, string $job, array $data, ?PayloadMetadata $metadata = null): ?string
6363
{
6464
$this->validateJobAndPriority($queue, $job);
6565

6666
helper('text');
6767

6868
$availableAt = Time::now()->addSeconds($this->delay ?? 0);
6969

70+
$jobId = random_string('numeric', 16);
71+
7072
$queueJob = new QueueJob([
71-
'id' => random_string('numeric', 16),
73+
'id' => $jobId,
7274
'queue' => $queue,
7375
'payload' => new Payload($job, $data, $metadata),
7476
'priority' => $this->priority,
@@ -81,7 +83,7 @@ public function push(string $queue, string $job, array $data, ?PayloadMetadata $
8183

8284
$this->priority = $this->delay = null;
8385

84-
return $result > 0;
86+
return $result > 0 ? $jobId : null;
8587
}
8688

8789
/**

src/Handlers/RedisHandler.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,16 +76,18 @@ public function name(): string
7676
*
7777
* @throws RedisException
7878
*/
79-
public function push(string $queue, string $job, array $data, ?PayloadMetadata $metadata = null): bool
79+
public function push(string $queue, string $job, array $data, ?PayloadMetadata $metadata = null): ?string
8080
{
8181
$this->validateJobAndPriority($queue, $job);
8282

8383
helper('text');
8484

8585
$availableAt = Time::now()->addSeconds($this->delay ?? 0);
8686

87+
$jobId = random_string('numeric', 16);
88+
8789
$queueJob = new QueueJob([
88-
'id' => random_string('numeric', 16),
90+
'id' => $jobId,
8991
'queue' => $queue,
9092
'payload' => new Payload($job, $data, $metadata),
9193
'priority' => $this->priority,
@@ -98,7 +100,7 @@ public function push(string $queue, string $job, array $data, ?PayloadMetadata $
98100

99101
$this->priority = $this->delay = null;
100102

101-
return $result > 0;
103+
return $result > 0 ? $jobId : null;
102104
}
103105

104106
/**

src/Payloads/ChainBuilder.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,10 @@ public function push(string $queue, string $jobName, array $data = []): ChainEle
4444
/**
4545
* Dispatch the chain of jobs
4646
*/
47-
public function dispatch(): bool
47+
public function dispatch(): ?string
4848
{
4949
if ($this->payloads->count() === 0) {
50-
return true;
50+
return null;
5151
}
5252

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

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->assertTrue($result);
88+
$this->assertNotNull($result);
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->assertTrue($result);
106+
$this->assertNotNull($result);
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->assertTrue($result);
125+
$this->assertNotNull($result);
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->assertTrue($result);
135+
$this->assertNotNull($result);
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->assertTrue($result);
164+
$this->assertNotNull($result);
165165

166166
$availableAt = 1703859376;
167167

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

191-
$this->assertTrue($result);
191+
$this->assertNotNull($result);
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->assertTrue($result);
224+
$this->assertNotNull($result);
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->assertTrue($result);
66+
$this->assertNotNull($result);
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->assertTrue($result);
87+
$this->assertNull($result);
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->assertTrue($result);
102+
$this->assertNotNull($result);
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->assertTrue($result);
135+
$this->assertNotNull($result);
136136
$this->seeInDatabase('queue_jobs', [
137137
'queue' => 'queue',
138138
'payload' => json_encode([

tests/PredisHandlerTest.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public function testPush(): void
7272
$handler = new PredisHandler($this->config);
7373
$result = $handler->push('queue', 'success', ['key' => 'value']);
7474

75-
$this->assertTrue($result);
75+
$this->assertNotNull($result);
7676

7777
$predis = self::getPrivateProperty($handler, 'predis');
7878
$this->assertSame(1, $predis->zcard('queues:queue:low'));
@@ -92,7 +92,7 @@ public function testPushWithPriority(): void
9292
$handler = new PredisHandler($this->config);
9393
$result = $handler->setPriority('high')->push('queue', 'success', ['key' => 'value']);
9494

95-
$this->assertTrue($result);
95+
$this->assertNotNull($result);
9696

9797
$predis = self::getPrivateProperty($handler, 'predis');
9898
$this->assertSame(1, $predis->zcard('queues:queue:high'));
@@ -114,7 +114,7 @@ public function testPushWithDelay(): void
114114
$handler = new PredisHandler($this->config);
115115
$result = $handler->setDelay(MINUTE)->push('queue-delay', 'success', ['key' => 'value']);
116116

117-
$this->assertTrue($result);
117+
$this->assertNotNull($result);
118118

119119
$predis = self::getPrivateProperty($handler, 'predis');
120120
$this->assertSame(1, $predis->zcard('queues:queue-delay:default'));
@@ -140,7 +140,7 @@ public function testChain(): void
140140
->push('queue', 'success', ['key2' => 'value2']);
141141
});
142142

143-
$this->assertTrue($result);
143+
$this->assertNotNull($result);
144144

145145
$predis = self::getPrivateProperty($handler, 'predis');
146146
$this->assertSame(1, $predis->zcard('queues:queue:low'));
@@ -180,7 +180,7 @@ public function testChainWithPriorityAndDelay(): void
180180
->setDelay(120);
181181
});
182182

183-
$this->assertTrue($result);
183+
$this->assertNotNull($result);
184184

185185
$predis = self::getPrivateProperty($handler, 'predis');
186186
// Should be in high priority queue

tests/PushAndPopWithDelayTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,11 @@ public function testPushAndPopWithDelay(string $name, string $class): void
6464
$handler = new $class($this->config);
6565
$result = $handler->setDelay(MINUTE)->push('queue-delay', 'success', ['key1' => 'value1']);
6666

67-
$this->assertTrue($result);
67+
$this->assertNotNull($result);
6868

6969
$result = $handler->push('queue-delay', 'success', ['key2' => 'value2']);
7070

71-
$this->assertTrue($result);
71+
$this->assertNotNull($result);
7272

7373
if ($name === 'database') {
7474
$this->seeInDatabase('queue_jobs', [

tests/RedisHandlerTest.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public function testPush(): void
6969
$handler = new RedisHandler($this->config);
7070
$result = $handler->push('queue', 'success', ['key' => 'value']);
7171

72-
$this->assertTrue($result);
72+
$this->assertNotNull($result);
7373

7474
$redis = self::getPrivateProperty($handler, 'redis');
7575
$this->assertSame(1, $redis->zCard('queues:queue:low'));
@@ -86,7 +86,7 @@ public function testPushWithPriority(): void
8686
$handler = new RedisHandler($this->config);
8787
$result = $handler->setPriority('high')->push('queue', 'success', ['key' => 'value']);
8888

89-
$this->assertTrue($result);
89+
$this->assertNotNull($result);
9090

9191
$redis = self::getPrivateProperty($handler, 'redis');
9292
$this->assertSame(1, $redis->zCard('queues:queue:high'));
@@ -108,7 +108,7 @@ public function testPushWithDelay(): void
108108
$handler = new RedisHandler($this->config);
109109
$result = $handler->setDelay(MINUTE)->push('queue-delay', 'success', ['key' => 'value']);
110110

111-
$this->assertTrue($result);
111+
$this->assertNotNull($result);
112112

113113
$redis = self::getPrivateProperty($handler, 'redis');
114114
$this->assertSame(1, $redis->zCard('queues:queue-delay:default'));
@@ -134,7 +134,7 @@ public function testChain(): void
134134
->push('queue', 'success', ['key2' => 'value2']);
135135
});
136136

137-
$this->assertTrue($result);
137+
$this->assertNotNull($result);
138138

139139
$redis = self::getPrivateProperty($handler, 'redis');
140140
$this->assertSame(1, $redis->zCard('queues:queue:low'));
@@ -174,7 +174,7 @@ public function testChainWithPriorityAndDelay(): void
174174
->setDelay(120);
175175
});
176176

177-
$this->assertTrue($result);
177+
$this->assertNotNull($result);
178178

179179
$redis = self::getPrivateProperty($handler, 'redis');
180180
// Should be in high priority queue

0 commit comments

Comments
 (0)