Skip to content
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

Add support for Google Pub/Sub driver #68

Merged
merged 4 commits into from
Jul 29, 2024
Merged
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
1 change: 0 additions & 1 deletion .styleci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ enabled:
- linebreak_after_opening_tag
- single_quote
- no_blank_lines_after_phpdoc
- unary_operator_spaces
- no_useless_else
- no_useless_return
- trailing_comma_in_multiline_array
Expand Down
2 changes: 2 additions & 0 deletions src/Queue/Driver.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ enum Driver: string
*/
case NSQ = 'nsq';

case PubSub = 'google_pub_sub';

/**
* @internal Used when the driver is not specified.
*/
Expand Down
51 changes: 51 additions & 0 deletions src/Queue/PubSubCreateInfo.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

declare(strict_types=1);

namespace Spiral\RoadRunner\Jobs\Queue;

final class PubSubCreateInfo extends CreateInfo
{
public const MAX_DELIVERY_ATTEMPTS_DEFAULT_VALUE = 10;

/**
* @param non-empty-string $name
* @param non-empty-string $projectId
* @param non-empty-string $topic
* @param positive-int $priority
* @param non-empty-string|null $deadLetterTopic
* @param positive-int $maxDeliveryAttempts
*/
public function __construct(
string $name,
public readonly string $projectId,
public readonly string $topic,
int $priority = self::PRIORITY_DEFAULT_VALUE,
public readonly ?string $deadLetterTopic = null,
public readonly int $maxDeliveryAttempts = self::MAX_DELIVERY_ATTEMPTS_DEFAULT_VALUE,
) {
parent::__construct(Driver::PubSub, $name, $priority);

\assert($this->projectId !== '', 'Precondition [projectId !== ""] failed');
\assert($this->topic !== '', 'Precondition [topic !== ""] failed');
\assert($this->maxDeliveryAttempts >= 5, 'Precondition [maxDeliveryAttempts >= 5] failed');
if ($this->deadLetterTopic !== null) {
\assert($this->deadLetterTopic !== '', 'Precondition [deadLetterTopic !== ""] failed');
}
}

public function toArray(): array
{
$result = \array_merge(parent::toArray(), [
'project_id' => $this->projectId,
'topic' => $this->topic,
]);

if ($this->deadLetterTopic !== null && $this->deadLetterTopic !== '') {
$result['dead_letter_topic'] = $this->deadLetterTopic;
$result['max_delivery_attempts'] = $this->maxDeliveryAttempts;
}

return $result;
}
}
92 changes: 92 additions & 0 deletions tests/Unit/Queue/PubSubCreateInfoTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<?php

declare(strict_types=1);

namespace Spiral\RoadRunner\Jobs\Tests\Unit\Queue;

use PHPUnit\Framework\TestCase;
use Spiral\RoadRunner\Jobs\Queue\Driver;
use Spiral\RoadRunner\Jobs\Queue\PubSubCreateInfo;

final class PubSubCreateInfoTest extends TestCase
{
public function testCreatePubSubCreateInfo(): void
{
$pubSubCreateInfo = new PubSubCreateInfo(
name: 'test_name',
projectId: 'test_project_id',
topic: 'test_topic',
priority: 3,
deadLetterTopic: 'test_dead_letter_topic',
maxDeliveryAttempts: 15,
);

$this->assertSame(Driver::PubSub, $pubSubCreateInfo->driver);
$this->assertSame('test_name', $pubSubCreateInfo->name);
$this->assertSame('test_project_id', $pubSubCreateInfo->projectId);
$this->assertSame('test_topic', $pubSubCreateInfo->topic);
$this->assertSame(3, $pubSubCreateInfo->priority);
$this->assertSame('test_dead_letter_topic', $pubSubCreateInfo->deadLetterTopic);
$this->assertSame(15, $pubSubCreateInfo->maxDeliveryAttempts);
}

public function testCreatePubSubCreateInfoOnlyRequiredData(): void
{
$pubSubCreateInfo = new PubSubCreateInfo(
name: 'test_name',
projectId: 'test_project_id',
topic: 'test_topic',
);

$this->assertSame(Driver::PubSub, $pubSubCreateInfo->driver);
$this->assertSame('test_name', $pubSubCreateInfo->name);
$this->assertSame('test_project_id', $pubSubCreateInfo->projectId);
$this->assertSame('test_topic', $pubSubCreateInfo->topic);
$this->assertSame(10, $pubSubCreateInfo->priority);
$this->assertNull($pubSubCreateInfo->deadLetterTopic);
$this->assertSame(10, $pubSubCreateInfo->maxDeliveryAttempts);
}

public function testToArray(): void
{
$pubSubCreateInfo = new PubSubCreateInfo(
name: 'test_name',
projectId: 'test_project_id',
topic: 'test_topic',
priority: 3,
deadLetterTopic: 'test_dead_letter_topic',
maxDeliveryAttempts: 15,
);

$expectedArray = [
'name' => 'test_name',
'driver' => Driver::PubSub->value,
'priority' => 3,
'project_id' => 'test_project_id',
'topic' => 'test_topic',
'dead_letter_topic' => 'test_dead_letter_topic',
'max_delivery_attempts' => 15,
];

$this->assertSame($expectedArray, $pubSubCreateInfo->toArray());
}

public function testToArrayOnlyRequiredData(): void
{
$pubSubCreateInfo = new PubSubCreateInfo(
name: 'test_name',
projectId: 'test_project_id',
topic: 'test_topic',
);

$expectedArray = [
'name' => 'test_name',
'driver' => Driver::PubSub->value,
'priority' => 10,
'project_id' => 'test_project_id',
'topic' => 'test_topic',
];

$this->assertSame($expectedArray, $pubSubCreateInfo->toArray());
}
}
Loading