Skip to content

feat: Queue::push() to return job ID instead of boolean #62

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/Handlers/BaseHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ abstract class BaseHandler

abstract public function name(): string;

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

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

Expand Down Expand Up @@ -153,7 +153,7 @@ public function setPriority(string $priority): static
*
* @param Closure $callback Chain definition callback
*/
public function chain(Closure $callback): bool
public function chain(Closure $callback): ?string
{
$chainBuilder = new ChainBuilder($this);
$callback($chainBuilder);
Expand Down
6 changes: 4 additions & 2 deletions src/Handlers/DatabaseHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public function name(): string
*
* @throws ReflectionException
*/
public function push(string $queue, string $job, array $data, ?PayloadMetadata $metadata = null): bool
public function push(string $queue, string $job, array $data, ?PayloadMetadata $metadata = null): ?string
{
$this->validateJobAndPriority($queue, $job);

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

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

return $this->jobModel->insert($queueJob, false);
$jobId = $this->jobModel->insert($queueJob);

return $jobId !== 0 ? (string) $jobId : null;
}

/**
Expand Down
7 changes: 4 additions & 3 deletions src/Handlers/PredisHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,16 +59,17 @@ public function name(): string
/**
* Add job to the queue.
*/
public function push(string $queue, string $job, array $data, ?PayloadMetadata $metadata = null): bool
public function push(string $queue, string $job, array $data, ?PayloadMetadata $metadata = null): ?string
{
$this->validateJobAndPriority($queue, $job);

helper('text');

$jobId = random_string('numeric', 16);
$availableAt = Time::now()->addSeconds($this->delay ?? 0);

$queueJob = new QueueJob([
'id' => random_string('numeric', 16),
'id' => $jobId,
'queue' => $queue,
'payload' => new Payload($job, $data, $metadata),
'priority' => $this->priority,
Expand All @@ -81,7 +82,7 @@ public function push(string $queue, string $job, array $data, ?PayloadMetadata $

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

return $result > 0;
return $result > 0 ? $jobId : null;
}

/**
Expand Down
7 changes: 4 additions & 3 deletions src/Handlers/RedisHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,16 +76,17 @@ public function name(): string
*
* @throws RedisException
*/
public function push(string $queue, string $job, array $data, ?PayloadMetadata $metadata = null): bool
public function push(string $queue, string $job, array $data, ?PayloadMetadata $metadata = null): ?string
{
$this->validateJobAndPriority($queue, $job);

helper('text');

$availableAt = Time::now()->addSeconds($this->delay ?? 0);
$jobId = random_string('numeric', 16);

$queueJob = new QueueJob([
'id' => random_string('numeric', 16),
'id' => $jobId,
'queue' => $queue,
'payload' => new Payload($job, $data, $metadata),
'priority' => $this->priority,
Expand All @@ -98,7 +99,7 @@ public function push(string $queue, string $job, array $data, ?PayloadMetadata $

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

return $result > 0;
return $result > 0 ? $jobId : null;
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/Payloads/ChainBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,10 @@ public function push(string $queue, string $jobName, array $data = []): ChainEle
/**
* Dispatch the chain of jobs
*/
public function dispatch(): bool
public function dispatch(): ?string
{
if ($this->payloads->count() === 0) {
return true;
return null;
}

$current = $this->payloads->shift();
Expand Down
14 changes: 7 additions & 7 deletions tests/DatabaseHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public function testPush(): void
$handler = new DatabaseHandler($this->config);
$result = $handler->push('queue', 'success', ['key' => 'value']);

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

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

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

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

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

$this->assertTrue($result);
$this->assertNotNull($result);

$availableAt = 1703859376;

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

$this->assertTrue($result);
$this->assertNotNull($result);
$this->seeInDatabase('queue_jobs', [
'queue' => 'queue',
'payload' => json_encode([
Expand Down Expand Up @@ -221,7 +221,7 @@ public function testChainWithPriorityAndDelay(): void
->setDelay(120);
});

$this->assertTrue($result);
$this->assertNotNull($result);
$this->seeInDatabase('queue_jobs', [
'queue' => 'queue',
'payload' => json_encode([
Expand Down
8 changes: 4 additions & 4 deletions tests/Payloads/ChainBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public function testChainWithSingleJob(): void
$chain->push('queue', 'success', ['key' => 'value']);
});

$this->assertTrue($result);
$this->assertNotNull($result);
$this->seeInDatabase('queue_jobs', [
'queue' => 'queue',
'payload' => json_encode([
Expand All @@ -84,7 +84,7 @@ public function testEmptyChain(): void
// No jobs added
});

$this->assertTrue($result);
$this->assertNull($result);
$this->seeInDatabase('queue_jobs', []);
}

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

$this->assertTrue($result);
$this->assertNotNull($result);
$this->seeInDatabase('queue_jobs', [
'queue' => 'queue1',
'payload' => json_encode([
Expand Down Expand Up @@ -132,7 +132,7 @@ public function testChainWithManyJobs(): void
->push('queue', 'success', ['key3' => 'value3']);
});

$this->assertTrue($result);
$this->assertNotNull($result);
$this->seeInDatabase('queue_jobs', [
'queue' => 'queue',
'payload' => json_encode([
Expand Down
10 changes: 5 additions & 5 deletions tests/PredisHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public function testPush(): void
$handler = new PredisHandler($this->config);
$result = $handler->push('queue', 'success', ['key' => 'value']);

$this->assertTrue($result);
$this->assertNotNull($result);

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

$this->assertTrue($result);
$this->assertNotNull($result);

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

$this->assertTrue($result);
$this->assertNotNull($result);

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

$this->assertTrue($result);
$this->assertNotNull($result);

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

$this->assertTrue($result);
$this->assertNotNull($result);

$predis = self::getPrivateProperty($handler, 'predis');
// Should be in high priority queue
Expand Down
4 changes: 2 additions & 2 deletions tests/PushAndPopWithDelayTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,11 @@ public function testPushAndPopWithDelay(string $name, string $class): void
$handler = new $class($this->config);
$result = $handler->setDelay(MINUTE)->push('queue-delay', 'success', ['key1' => 'value1']);

$this->assertTrue($result);
$this->assertNotNull($result);

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

$this->assertTrue($result);
$this->assertNotNull($result);

if ($name === 'database') {
$this->seeInDatabase('queue_jobs', [
Expand Down
10 changes: 5 additions & 5 deletions tests/RedisHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public function testPush(): void
$handler = new RedisHandler($this->config);
$result = $handler->push('queue', 'success', ['key' => 'value']);

$this->assertTrue($result);
$this->assertNotNull($result);

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

$this->assertTrue($result);
$this->assertNotNull($result);

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

$this->assertTrue($result);
$this->assertNotNull($result);

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

$this->assertTrue($result);
$this->assertNotNull($result);

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

$this->assertTrue($result);
$this->assertNotNull($result);

$redis = self::getPrivateProperty($handler, 'redis');
// Should be in high priority queue
Expand Down
Loading