Skip to content

Commit d79baea

Browse files
Add prefixes to queue names. (#46)
* Optional subscription and topic creation (#35) * ability to disable automatic topic creation and validation which cause high usage of "Administrative operations" calls and can lead to Quota limit exceptions * ability to disable automatic subscription which cause high usage of "Administrative operations" calls and can lead to Quota limit exceptions * style: styleci fix * Add ability to specify queue prefixes. * Fix formatting. * Update README to include queue_prefix config. Co-authored-by: Maksim Martianov <[email protected]>
1 parent 6bd66f7 commit d79baea

File tree

4 files changed

+62
-8
lines changed

4 files changed

+62
-8
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ You can check [Google Cloud PubSub client](http://googleapis.github.io/google-cl
2929
'pubsub' => [
3030
'driver' => 'pubsub',
3131
'queue' => env('PUBSUB_QUEUE', 'default'),
32+
'queue_prefix' => env('PUBSUB_QUEUE_PREFIX', ''),
3233
'project_id' => env('PUBSUB_PROJECT_ID', 'your-project-id'),
3334
'retries' => 3,
3435
'request_timeout' => 60,

src/Connectors/PubSubConnector.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,10 @@ public function connect(array $config)
2929
return new PubSubQueue(
3030
new PubSubClient($gcp_config),
3131
$config['queue'] ?? $this->default_queue,
32-
$config['subscriber'] ?? 'subscriber'
32+
$config['subscriber'] ?? 'subscriber',
33+
$config['create_topics'] ?? true,
34+
$config['create_subscriptions'] ?? true,
35+
$config['queue_prefix'] ?? ''
3336
);
3437
}
3538

src/PubSubQueue.php

Lines changed: 39 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,17 +33,41 @@ class PubSubQueue extends Queue implements QueueContract
3333
*/
3434
protected $subscriber;
3535

36+
/**
37+
* Create topics automatically.
38+
*
39+
* @var bool
40+
*/
41+
protected $topicAutoCreation;
42+
43+
/**
44+
* Create subscriptions automatically.
45+
*
46+
* @var bool
47+
*/
48+
protected $subscriptionAutoCreation;
49+
50+
/**
51+
* Prepend all queue names with this prefix.
52+
*
53+
* @var string
54+
*/
55+
protected $queuePrefix = '';
56+
3657
/**
3758
* Create a new GCP PubSub instance.
3859
*
3960
* @param \Google\Cloud\PubSub\PubSubClient $pubsub
4061
* @param string $default
4162
*/
42-
public function __construct(PubSubClient $pubsub, $default, $subscriber = 'subscriber')
63+
public function __construct(PubSubClient $pubsub, $default, $subscriber = 'subscriber', $topicAutoCreation = true, $subscriptionAutoCreation = true, $queuePrefix = '')
4364
{
4465
$this->pubsub = $pubsub;
4566
$this->default = $default;
4667
$this->subscriber = $subscriber;
68+
$this->topicAutoCreation = $topicAutoCreation;
69+
$this->subscriptionAutoCreation = $subscriptionAutoCreation;
70+
$this->queuePrefix = $queuePrefix;
4771
}
4872

4973
/**
@@ -85,7 +109,7 @@ public function push($job, $data = '', $queue = null)
85109
*/
86110
public function pushRaw($payload, $queue = null, array $options = [])
87111
{
88-
$topic = $this->getTopic($queue, true);
112+
$topic = $this->getTopic($queue, $this->topicAutoCreation);
89113

90114
$this->subscribeToTopic($topic);
91115

@@ -131,7 +155,7 @@ public function pop($queue = null)
131155
{
132156
$topic = $this->getTopic($this->getQueue($queue));
133157

134-
if (! $topic->exists()) {
158+
if ($this->topicAutoCreation && ! $topic->exists()) {
135159
return;
136160
}
137161

@@ -179,7 +203,7 @@ public function bulk($jobs, $data = '', $queue = null)
179203
$payloads[] = ['data' => base64_encode($payload)];
180204
}
181205

182-
$topic = $this->getTopic($this->getQueue($queue), true);
206+
$topic = $this->getTopic($this->getQueue($queue), $this->topicAutoCreation);
183207

184208
$this->subscribeToTopic($topic);
185209

@@ -274,7 +298,8 @@ public function getTopic($queue, $create = false)
274298
$queue = $this->getQueue($queue);
275299
$topic = $this->pubsub->topic($queue);
276300

277-
if (! $topic->exists() && $create) {
301+
// don't check topic if automatic creation is not required, to avoid additional administrator operations calls
302+
if ($create && ! $topic->exists()) {
278303
$topic->create();
279304
}
280305

@@ -292,7 +317,8 @@ public function subscribeToTopic(Topic $topic)
292317
{
293318
$subscription = $topic->subscription($this->getSubscriberName());
294319

295-
if (! $subscription->exists()) {
320+
// don't check subscription if automatic creation is not required, to avoid additional administrator operations calls
321+
if ($this->subscriptionAutoCreation && ! $subscription->exists()) {
296322
$subscription = $topic->subscribe($this->getSubscriberName());
297323
}
298324

@@ -329,7 +355,13 @@ public function getPubSub()
329355
*/
330356
public function getQueue($queue)
331357
{
332-
return $queue ?: $this->default;
358+
$queue = $queue ?: $this->default;
359+
360+
if (! $this->queuePrefix || Str::startsWith($queue, $this->queuePrefix)) {
361+
return $queue;
362+
}
363+
364+
return $this->queuePrefix.$queue;
333365
}
334366

335367
/**

tests/Unit/Connectors/PubSubConnectorTests.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,24 @@ public function testConnectReturnsPubSubQueueInstance()
2727
$this->assertEquals($queue->getSubscriberName(), 'test-subscriber');
2828
}
2929

30+
public function testQueuePrefixAdded()
31+
{
32+
$connector = new PubSubConnector();
33+
$config = $this->createFakeConfig() + ['queue_prefix' => 'prefix-'];
34+
$queue = $connector->connect($config);
35+
36+
$this->assertEquals('prefix-my-queue', $queue->getQueue('my-queue'));
37+
}
38+
39+
public function testNotQueuePrefixAddedMultipleTimes()
40+
{
41+
$connector = new PubSubConnector();
42+
$config = $this->createFakeConfig() + ['queue_prefix' => 'prefix-'];
43+
$queue = $connector->connect($config);
44+
45+
$this->assertEquals('prefix-default', $queue->getQueue($queue->getQueue('default')));
46+
}
47+
3048
private function createFakeConfig()
3149
{
3250
return [

0 commit comments

Comments
 (0)