11
11
use Google \Protobuf \Timestamp ;
12
12
use Illuminate \Contracts \Queue \Queue as QueueContract ;
13
13
use Illuminate \Queue \Queue as LaravelQueue ;
14
- use Illuminate \Support \Facades \Log ;
15
14
use Illuminate \Support \Str ;
16
15
use Stackkit \LaravelGoogleCloudTasksQueue \Events \TaskCreated ;
17
16
use function Safe \json_decode ;
@@ -140,20 +139,22 @@ protected function pushToCloudTasks($queue, $payload, $delay = 0)
140
139
$ httpRequest ->setUrl ($ this ->getHandler ());
141
140
$ httpRequest ->setHttpMethod (HttpMethod::POST );
142
141
142
+ $ payload = json_decode ($ payload , true );
143
+
143
144
// Laravel 7+ jobs have a uuid, but Laravel 6 doesn't have it.
144
145
// Since we are using and expecting the uuid in some places
145
146
// we will add it manually here if it's not present yet.
146
- [ $ payload, $ uuid , $ displayName ] = $ this ->extractPayload ($ payload );
147
+ $ payload = $ this ->withUuid ($ payload );
147
148
148
149
// Since 3.x tasks are released back onto the queue after an exception has
149
150
// been thrown. This means we lose the native [X-CloudTasks-TaskRetryCount] header
150
151
// value and need to manually set and update the number of times a task has been attempted.
151
152
$ payload = $ this ->withAttempts ($ payload );
152
153
153
- $ httpRequest ->setBody ($ payload );
154
+ $ httpRequest ->setBody (json_encode ( $ payload) );
154
155
155
156
$ task = $ this ->createTask ();
156
- $ task ->setName ($ this ->taskName ($ queue , $ uuid , $ displayName ));
157
+ $ task ->setName ($ this ->taskName ($ queue , $ payload ));
157
158
$ task ->setHttpRequest ($ httpRequest );
158
159
159
160
// 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)
174
175
175
176
$ createdTask = CloudTasksApi::createTask ($ queueName , $ task );
176
177
177
- event ((new TaskCreated )->queue ($ queue )->task ($ createdTask ));
178
+ event ((new TaskCreated )->queue ($ queue )->task ($ task ));
178
179
179
- return $ uuid ;
180
+ return $ payload [ ' uuid ' ] ;
180
181
}
181
182
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
197
184
{
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 ();
203
187
}
204
188
205
- return [
206
- json_encode ($ decoded ),
207
- $ decoded ['uuid ' ],
208
- $ decoded ['displayName ' ]
209
- ];
189
+ return $ payload ;
210
190
}
211
191
212
- private function withAttempts (string $ payload ): string
192
+ private function taskName (string $ queueName , array $ payload ): string
213
193
{
214
- /** @var array $decoded */
215
- $ decoded = json_decode ($ payload , true );
194
+ $ displayName = str_replace ("\\" , '- ' , $ payload ['displayName ' ]);
216
195
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 ;
219
208
}
220
209
221
- return json_encode ( $ decoded ) ;
210
+ return $ payload ;
222
211
}
223
212
224
213
/**
0 commit comments