2
2
3
3
namespace Superbalist \PubSub \GoogleCloud ;
4
4
5
+ use Exception ;
5
6
use Google \Cloud \PubSub \Message ;
6
7
use Google \Cloud \PubSub \PubSubClient ;
7
8
use Google \Cloud \PubSub \Subscription ;
@@ -50,13 +51,21 @@ class GoogleCloudPubSubAdapter implements PubSubAdapterInterface
50
51
*/
51
52
protected $ returnImmediatelyPause ;
52
53
54
+ /**
55
+ * @var boolean
56
+ */
57
+ private $ alwaysThrowException ;
58
+
53
59
/**
54
60
* @param PubSubClient $client
55
61
* @param string $clientIdentifier
56
62
* @param bool $autoCreateTopics
57
63
* @param bool $autoCreateSubscriptions
58
64
* @param bool $backgroundBatching
59
65
* @param int $maxMessages
66
+ * @param bool $returnImmediately
67
+ * @param int $returnImmediatelyPause
68
+ * @param bool $alwaysThrowException
60
69
*/
61
70
public function __construct (
62
71
PubSubClient $ client ,
@@ -66,7 +75,8 @@ public function __construct(
66
75
$ backgroundBatching = false ,
67
76
$ maxMessages = 1000 ,
68
77
$ returnImmediately = false ,
69
- $ returnImmediatelyPause = 500000
78
+ $ returnImmediatelyPause = 500000 ,
79
+ $ alwaysThrowException = false
70
80
) {
71
81
$ this ->client = $ client ;
72
82
$ this ->clientIdentifier = $ clientIdentifier ;
@@ -76,6 +86,7 @@ public function __construct(
76
86
$ this ->maxMessages = $ maxMessages ;
77
87
$ this ->returnImmediately = $ returnImmediately ;
78
88
$ this ->returnImmediatelyPause = (int ) $ returnImmediatelyPause ;
89
+ $ this ->alwaysThrowException = $ alwaysThrowException ;
79
90
}
80
91
81
92
/**
@@ -228,11 +239,31 @@ public function setMaxMessages($maxMessages)
228
239
$ this ->maxMessages = $ maxMessages ;
229
240
}
230
241
242
+ /**
243
+ * Always throw exception from user callback function
244
+ * if set to true message will NOT be acked
245
+ *
246
+ * @param bool alwaysThrowException
247
+ */
248
+ public function setAlwaysThrowException ($ alwaysThrowException ) {
249
+ $ this ->alwaysThrowException = (bool ) $ alwaysThrowException ;
250
+ }
251
+
252
+ /**
253
+ * Return the always throw exception property
254
+ *
255
+ * @return bool
256
+ */
257
+ public function getAlwaysThrowException () {
258
+ return $ this ->alwaysThrowException ;
259
+ }
260
+
231
261
/**
232
262
* Subscribe a handler to a channel.
233
263
*
234
264
* @param string $channel
235
265
* @param callable $handler
266
+ * @throws Exception
236
267
*/
237
268
public function subscribe ($ channel , callable $ handler )
238
269
{
@@ -260,7 +291,13 @@ public function subscribe($channel, callable $handler)
260
291
if ($ payload === 'unsubscribe ' ) {
261
292
$ isSubscriptionLoopActive = false ;
262
293
} else {
263
- call_user_func ($ handler , $ payload );
294
+ try {
295
+ call_user_func ($ handler , $ payload );
296
+ } catch (Exception $ e ) {
297
+ if ($ this ->alwaysThrowException ) {
298
+ throw $ e ;
299
+ }
300
+ }
264
301
}
265
302
266
303
$ subscription ->acknowledge ($ message );
0 commit comments