1111use Google \Protobuf \Timestamp ;
1212use Illuminate \Contracts \Queue \Queue as QueueContract ;
1313use Illuminate \Queue \Queue as LaravelQueue ;
14- use Illuminate \Support \Facades \Log ;
1514use Illuminate \Support \Str ;
1615use Stackkit \LaravelGoogleCloudTasksQueue \Events \TaskCreated ;
1716use function Safe \json_decode ;
@@ -140,20 +139,22 @@ protected function pushToCloudTasks($queue, $payload, $delay = 0)
140139 $ httpRequest ->setUrl ($ this ->getHandler ());
141140 $ httpRequest ->setHttpMethod (HttpMethod::POST );
142141
142+ $ payload = json_decode ($ payload , true );
143+
143144 // Laravel 7+ jobs have a uuid, but Laravel 6 doesn't have it.
144145 // Since we are using and expecting the uuid in some places
145146 // we will add it manually here if it's not present yet.
146- [ $ payload, $ uuid , $ displayName ] = $ this ->extractPayload ($ payload );
147+ $ payload = $ this ->withUuid ($ payload );
147148
148149 // Since 3.x tasks are released back onto the queue after an exception has
149150 // been thrown. This means we lose the native [X-CloudTasks-TaskRetryCount] header
150151 // value and need to manually set and update the number of times a task has been attempted.
151152 $ payload = $ this ->withAttempts ($ payload );
152153
153- $ httpRequest ->setBody ($ payload );
154+ $ httpRequest ->setBody (json_encode ( $ payload) );
154155
155156 $ task = $ this ->createTask ();
156- $ task ->setName ($ this ->taskName ($ queue , $ uuid , $ displayName ));
157+ $ task ->setName ($ this ->taskName ($ queue , $ payload ));
157158 $ task ->setHttpRequest ($ httpRequest );
158159
159160 // The deadline for requests sent to the app. If the app does not respond by
@@ -174,51 +175,39 @@ protected function pushToCloudTasks($queue, $payload, $delay = 0)
174175
175176 $ createdTask = CloudTasksApi::createTask ($ queueName , $ task );
176177
177- event ((new TaskCreated )->queue ($ queue )->task ($ createdTask ));
178+ event ((new TaskCreated )->queue ($ queue )->task ($ task ));
178179
179- return $ uuid ;
180+ return $ payload [ ' uuid ' ] ;
180181 }
181182
182- private function taskName (string $ queueName , string $ uuid , string $ displayName ) {
183- $ config = $ this ->config ;
184- $ projectId = $ config ['project ' ];
185- $ location = $ config ['location ' ];
186- $ queueId = $ queueName ;
187- // projects/PROJECT_ID/locations/LOCATION_ID/queues/QUEUE_ID/tasks/TASK_ID
188- // projects/PROJECT_ID/locations/LOCATION_ID/queues/QUEUE_ID/tasks/TASK_ID
189- $ displayName = str_replace ("\\" , "- " , $ displayName );
190- $ taskName = sprintf ('projects/%s/locations/%s/queues/%s/tasks/%s-%s ' , $ projectId , $ location , $ queueId , $ uuid , $ displayName );
191- print ($ taskName );
192- print ($ taskName );
193- return $ taskName ;
194- }
195-
196- private function extractPayload (string $ payload ): array
183+ private function withUuid (array $ payload ): array
197184 {
198- /** @var array $decoded */
199- $ decoded = json_decode ($ payload , true );
200-
201- if (!isset ($ decoded ['uuid ' ])) {
202- $ decoded ['uuid ' ] = (string ) Str::uuid ();
185+ if (!isset ($ payload ['uuid ' ])) {
186+ $ payload ['uuid ' ] = (string ) Str::uuid ();
203187 }
204188
205- return [
206- json_encode ($ decoded ),
207- $ decoded ['uuid ' ],
208- $ decoded ['displayName ' ]
209- ];
189+ return $ payload ;
210190 }
211191
212- private function withAttempts (string $ payload ): string
192+ private function taskName (string $ queueName , array $ payload ): string
213193 {
214- /** @var array $decoded */
215- $ decoded = json_decode ($ payload , true );
194+ $ displayName = str_replace ("\\" , '- ' , $ payload ['displayName ' ]);
216195
217- if (!isset ($ decoded ['internal ' ]['attempts ' ])) {
218- $ decoded ['internal ' ]['attempts ' ] = 0 ;
196+ return CloudTasksClient::taskName (
197+ $ this ->config ['project ' ],
198+ $ this ->config ['location ' ],
199+ $ queueName ,
200+ $ displayName . '- ' . $ payload ['uuid ' ]
201+ );
202+ }
203+
204+ private function withAttempts (array $ payload ): array
205+ {
206+ if (!isset ($ payload ['internal ' ]['attempts ' ])) {
207+ $ payload ['internal ' ]['attempts ' ] = 0 ;
219208 }
220209
221- return json_encode ( $ decoded ) ;
210+ return $ payload ;
222211 }
223212
224213 /**
0 commit comments