22
33namespace RichanFongdasen \GCRWorker ;
44
5- use ErrorException ;
65use Google \Cloud \PubSub \Message ;
76use Illuminate \Container \Container ;
8- use Kainxspirits \PubSubQueue \Connectors \PubSubConnector ;
97use Kainxspirits \PubSubQueue \Jobs \PubSubJob ;
10- use Kainxspirits \ PubSubQueue \ PubSubQueue ;
8+ use RichanFongdasen \ GCRWorker \ Facade \ GcrQueue ;
119
1210class PubSubEvent
1311{
14- /**
15- * Pub/Sub connector instance.
16- *
17- * @var PubSubConnector
18- */
19- protected $ connector ;
20-
2112 /**
2213 * Laravel IOC Container instance.
2314 *
2415 * @var Container
2516 */
2617 protected $ container ;
2718
28- /**
29- * Pub/Sub queue instance.
30- *
31- * @var PubSubQueue
32- */
33- protected $ queue ;
34-
3519 /**
3620 * PubSubEvent constructor.
3721 *
38- * @param Container $container
39- * @param PubSubConnector $connector
40- *
41- * @throws ErrorException
22+ * @param Container $container
4223 */
43- public function __construct (Container $ container, PubSubConnector $ connector )
24+ public function __construct (Container $ container )
4425 {
4526 $ this ->container = $ container ;
46- $ this ->connector = $ connector ;
47- $ this ->queue = $ this ->getQueue ();
4827 }
4928
5029 /**
@@ -58,31 +37,13 @@ protected function createJob(Message $message): PubSubJob
5837 {
5938 return new PubSubJob (
6039 $ this ->container ,
61- $ this -> queue ,
40+ GcrQueue:: getPubSubQueue () ,
6241 $ message ,
6342 config ('queue.connections.pubsub.driver ' ),
6443 config ('queue.connections.pubsub.queue ' )
6544 );
6645 }
6746
68- /**
69- * Get the PubSubQueue instance.
70- *
71- * @throws ErrorException
72- *
73- * @return PubSubQueue
74- */
75- protected function getQueue (): PubSubQueue
76- {
77- $ queue = $ this ->connector ->connect (config ('queue.connections.pubsub ' ));
78-
79- if (!($ queue instanceof PubSubQueue)) {
80- throw new ErrorException ('Failed to retrieve PubSubQueue instance. ' );
81- }
82-
83- return $ queue ;
84- }
85-
8647 /**
8748 * Handle the given Pub/Sub event message.
8849 *
@@ -92,6 +53,8 @@ public function handle(Message $message): void
9253 {
9354 set_time_limit (config ('gcr-worker.max_execution_time ' ));
9455
56+ GcrQueue::acknowledge ($ message );
57+
9558 $ this ->createJob ($ message )->fire ();
9659 }
9760}
0 commit comments