77use Google \ApiCore \ApiException ;
88use Google \Cloud \Tasks \V2 \Client \CloudTasksClient ;
99use Illuminate \Container \Container ;
10- use Illuminate \Contracts \ Encryption \ Encrypter ;
10+ use Illuminate \Queue \ Worker ;
1111use Illuminate \Queue \WorkerOptions ;
12- use Illuminate \Support \Str ;
13- use Illuminate \Validation \ValidationException ;
14- use Safe \Exceptions \JsonException ;
12+ use Throwable ;
1513
1614use function Safe \json_decode ;
1715
@@ -39,72 +37,15 @@ public function __construct(CloudTasksClient $client)
3937
4038 public function handle (?string $ task = null ): void
4139 {
42- $ task = $ this -> captureTask ( $ task );
40+ $ task = json_decode (( string ) $ task ?: request ()-> getContent (), assoc: true );
4341
44- $ this ->loadQueueConnectionConfiguration ($ task );
45-
46- $ this ->setQueue ();
42+ $ this ->config = config ('queue.connections. ' .$ task ['internal ' ]['connection ' ]);
4743
4844 $ this ->guard ();
4945
5046 $ this ->handleTask ($ task );
5147 }
5248
53- /**
54- * @param string|array|null $task
55- *
56- * @throws JsonException
57- */
58- private function captureTask ($ task ): array
59- {
60- $ task = $ task ?: (string ) (request ()->getContent ());
61-
62- try {
63- $ array = json_decode ($ task , true );
64- } catch (JsonException $ e ) {
65- $ array = [];
66- }
67-
68- $ validator = validator ([
69- 'json ' => $ task ,
70- 'task ' => $ array ,
71- ], [
72- 'json ' => 'required|json ' ,
73- 'task ' => 'required|array ' ,
74- 'task.data ' => 'required|array ' ,
75- ]);
76-
77- try {
78- $ validator ->validate ();
79- } catch (ValidationException $ e ) {
80- if (config ('app.debug ' )) {
81- throw $ e ;
82- } else {
83- abort (404 );
84- }
85- }
86-
87- return json_decode ($ task , true );
88- }
89-
90- private function loadQueueConnectionConfiguration (array $ task ): void
91- {
92- $ command = self ::getCommandProperties ($ task ['data ' ]['command ' ]);
93- $ connection = $ command ['connection ' ] ?? config ('queue.default ' );
94- $ baseConfig = config ('queue.connections. ' .$ connection );
95- $ config = (new CloudTasksConnector ())->connect ($ baseConfig )->config ;
96-
97- // The connection name from the config may not be the actual connection name
98- $ config ['connection ' ] = $ connection ;
99-
100- $ this ->config = $ config ;
101- }
102-
103- private function setQueue (): void
104- {
105- $ this ->queue = new CloudTasksQueue ($ this ->config , $ this ->client );
106- }
107-
10849 private function guard (): void
10950 {
11051 $ appEngine = ! empty ($ this ->config ['app_engine ' ]);
@@ -121,18 +62,25 @@ private function guard(): void
12162
12263 private function handleTask (array $ task ): void
12364 {
65+ $ queue = new CloudTasksQueue (
66+ config: $ this ->config ,
67+ client: $ this ->client ,
68+ );
69+
70+ $ queue ->setConnectionName ($ task ['internal ' ]['connection ' ]);
71+
12472 $ job = new CloudTasksJob (
125- Container::getInstance (),
126- $ this -> queue ,
127- $ task ,
128- $ this -> config ['connection ' ],
129- $ task ['internal ' ]['queue ' ],
73+ container: Container::getInstance (),
74+ cloudTasksQueue: $ queue ,
75+ job: $ task ,
76+ connectionName: $ task [ ' internal ' ] ['connection ' ],
77+ queue: $ task ['internal ' ]['queue ' ],
13078 );
13179
13280 try {
133- $ apiTask = CloudTasksApi::getTask ($ task ['internal ' ]['taskName ' ]);
134- } catch (ApiException $ e ) {
135- if (in_array ($ e ->getStatus (), ['NOT_FOUND ' , 'PRECONDITION_FAILED ' ])) {
81+ CloudTasksApi::getTask ($ task ['internal ' ]['taskName ' ]);
82+ } catch (Throwable $ e ) {
83+ if ($ e instanceof ApiException && in_array ($ e ->getStatus (), ['NOT_FOUND ' , 'PRECONDITION_FAILED ' ])) {
13684 abort (404 );
13785 }
13886
@@ -141,23 +89,11 @@ private function handleTask(array $task): void
14189
14290 $ job ->setAttempts ($ job ->attempts () + 1 );
14391
144- app ('queue.worker ' )->process ($ this ->config ['connection ' ], $ job , $ this ->getWorkerOptions ());
145- }
146-
147- public static function getCommandProperties (string $ command ): array
148- {
149- if (Str::startsWith ($ command , 'O: ' )) {
150- return (array ) unserialize ($ command , ['allowed_classes ' => false ]);
151- }
152-
153- if (app ()->bound (Encrypter::class)) {
154- return (array ) unserialize (
155- app (Encrypter::class)->decrypt ($ command ),
156- ['allowed_classes ' => ['Illuminate\Support\Carbon ' ]]
157- );
158- }
159-
160- return [];
92+ tap (app ('queue.worker ' ), fn (Worker $ worker ) => $ worker ->process (
93+ connectionName: $ job ->getConnectionName (),
94+ job: $ job ,
95+ options: $ this ->getWorkerOptions ()
96+ ));
16197 }
16298
16399 public function getWorkerOptions (): WorkerOptions
0 commit comments