2
2
3
3
namespace PubSub \PubSubQueue ;
4
4
5
- use Illuminate \Queue \Queue ;
6
- use Illuminate \Support \Str ;
7
- use Google \Cloud \PubSub \Topic ;
8
5
use Google \Cloud \PubSub \Message ;
9
6
use Google \Cloud \PubSub \PubSubClient ;
10
- use PubSub \ PubSubQueue \ Jobs \ PubSubJob ;
7
+ use Google \ Cloud \ PubSub \ Topic ;
11
8
use Illuminate \Contracts \Queue \Queue as QueueContract ;
9
+ use Illuminate \Queue \Queue ;
10
+ use Illuminate \Support \Str ;
11
+ use PubSub \PubSubQueue \Jobs \PubSubJob ;
12
12
13
13
class PubSubQueue extends Queue implements QueueContract
14
14
{
@@ -31,6 +31,10 @@ class PubSubQueue extends Queue implements QueueContract
31
31
*/
32
32
protected $ config ;
33
33
34
+ /**
35
+ * Subscriber name
36
+ */
37
+ protected $ subscriber ;
34
38
35
39
/**
36
40
* Create a new GCP PubSub instance.
@@ -86,11 +90,9 @@ public function pushRaw($payload, $subscriber = null, array $options = [])
86
90
{
87
91
$ topic = $ this ->getTopic ($ subscriber , true );
88
92
89
- $ this ->subscribeToTopic ($ topic );
90
-
91
93
$ publish = ['data ' => $ payload ];
92
94
93
- if (! empty ($ options )) {
95
+ if (!empty ($ options )) {
94
96
$ publish ['attributes ' ] = $ options ;
95
97
}
96
98
@@ -128,25 +130,26 @@ public function later($delay, $job, $data = '', $subscriber = null)
128
130
*/
129
131
public function pop ($ subscriber = null )
130
132
{
133
+ $ this ->subscriber = $ subscriber ;
131
134
$ topic = $ this ->getTopic ($ this ->getQueue ($ subscriber ));
132
135
133
- if (! $ topic ->exists ()) {
136
+ if (!$ topic ->exists ()) {
134
137
return ;
135
138
}
136
-
137
- $ subscription = $ topic ->subscription ($ this ->getSubscriberName ($ subscriber ));
139
+ $ subscription = $ topic ->subscription ($ subscriber );
138
140
$ messages = $ subscription ->pull ([
139
141
'returnImmediately ' => true ,
140
142
'maxMessages ' => 1 ,
141
143
]);
142
144
143
- if (! empty ($ messages ) && count ($ messages ) > 0 ) {
145
+ if (!empty ($ messages ) && count ($ messages ) > 0 ) {
144
146
return new PubSubJob (
145
147
$ this ->container ,
146
148
$ this ,
147
149
$ messages [0 ],
148
150
$ this ->connectionName ,
149
- $ this ->getQueue ($ subscriber )
151
+ $ this ->getQueue ($ subscriber ),
152
+ $ subscriber
150
153
);
151
154
}
152
155
}
@@ -170,8 +173,6 @@ public function bulk($jobs, $data = '', $queue = null)
170
173
171
174
$ topic = $ this ->getTopic ($ this ->getQueue ($ queue ), true );
172
175
173
- $ this ->subscribeToTopic ($ topic );
174
-
175
176
return $ topic ->publishBatch ($ payloads );
176
177
}
177
178
@@ -183,7 +184,7 @@ public function bulk($jobs, $data = '', $queue = null)
183
184
*/
184
185
public function acknowledge (Message $ message , $ queue = null )
185
186
{
186
- $ subscription = $ this ->getTopic ($ this ->getQueue ($ queue ))->subscription ($ this ->getSubscriberName ( $ queue ) );
187
+ $ subscription = $ this ->getTopic ($ this ->getQueue ($ queue ))->subscription ($ this ->subscriber );
187
188
$ subscription ->acknowledge ($ message );
188
189
}
189
190
@@ -198,7 +199,7 @@ public function acknowledge(Message $message, $queue = null)
198
199
public function acknowledgeAndPublish (Message $ message , $ queue = null , $ options = [], $ delay = 0 )
199
200
{
200
201
$ topic = $ this ->getTopic ($ this ->getQueue ($ queue ));
201
- $ subscription = $ topic ->subscription ($ this -> getSubscriberName ( $ queue) );
202
+ $ subscription = $ topic ->subscription ($ queue );
202
203
203
204
$ subscription ->acknowledge ($ message );
204
205
@@ -257,7 +258,7 @@ public function getTopic($queue, $create = false)
257
258
$ queue = $ this ->getQueue ($ queue );
258
259
$ topic = $ this ->pubsub ->topic ($ queue );
259
260
260
- if (! $ topic ->exists () && $ create ) {
261
+ if (!$ topic ->exists () && $ create ) {
261
262
$ topic ->create ();
262
263
}
263
264
@@ -271,11 +272,11 @@ public function getTopic($queue, $create = false)
271
272
*
272
273
* @return \Google\Cloud\PubSub\Subscription
273
274
*/
274
- public function subscribeToTopic (Topic $ topic )
275
+ public function subscribeToTopic (Topic $ topic, $ subscriber = null )
275
276
{
276
- $ subscription = $ topic ->subscription ($ this -> getSubscriberName () );
277
- if (! $ subscription ->exists ()) {
278
- $ subscription = $ topic ->subscribe ($ this -> getSubscriberName () );
277
+ $ subscription = $ topic ->subscription ($ subscriber );
278
+ if (!$ subscription ->exists ()) {
279
+ $ subscription = $ topic ->subscribe ($ subscriber );
279
280
}
280
281
281
282
return $ subscription ;
@@ -290,10 +291,7 @@ public function subscribeToTopic(Topic $topic)
290
291
*/
291
292
public function getSubscriberName ($ queue = null )
292
293
{
293
- if ($ this ->config && $ this ->config ['subscribers ' ] && $ queue && isset ($ this ->config ['subscribers ' ][$ queue ])) {
294
- return $ this ->config ['subscribers ' ][$ queue ];
295
- }
296
- return 'subscriber ' ;
294
+ return $ queue ;
297
295
}
298
296
299
297
/**
@@ -314,7 +312,10 @@ public function getPubSub()
314
312
*/
315
313
public function getQueue ($ queue )
316
314
{
317
- return $ queue ?: $ this ->default ;
315
+ if ($ this ->config && $ this ->config ['subscribers ' ] && $ queue && isset ($ this ->config ['subscribers ' ][$ queue ])) {
316
+ return $ this ->config ['subscribers ' ][$ queue ];
317
+ }
318
+ return $ this ->default ;
318
319
}
319
320
320
321
/**
0 commit comments