@@ -143,11 +143,19 @@ protected function pushToCloudTasks($queue, $payload, $delay = 0)
143143 // we will add it manually here if it's not present yet.
144144 [$ payload , $ uuid ] = $ this ->withUuid ($ payload );
145145
146+ // Since 3.x tasks are released back onto the queue after an exception has
147+ // been thrown. This means we lose the native [X-CloudTasks-TaskRetryCount] header
148+ // value and need to manually set and update the number of times a task has been attempted.
149+ $ payload = $ this ->withAttempts ($ payload );
150+
146151 $ httpRequest ->setBody ($ payload );
147152
148153 $ task = $ this ->createTask ();
149154 $ task ->setHttpRequest ($ httpRequest );
150155
156+ // The deadline for requests sent to the app. If the app does not respond by
157+ // this deadline then the request is cancelled and the attempt is marked as
158+ // a failure. Cloud Tasks will retry the task according to the RetryConfig.
151159 if (!empty ($ this ->config ['dispatch_deadline ' ])) {
152160 $ task ->setDispatchDeadline (new Duration (['seconds ' => $ this ->config ['dispatch_deadline ' ]]));
153161 }
@@ -182,6 +190,20 @@ private function withUuid(string $payload): array
182190 ];
183191 }
184192
193+ private function withAttempts (string $ payload ): string
194+ {
195+ /** @var array $decoded */
196+ $ decoded = json_decode ($ payload , true );
197+
198+ if (!isset ($ decoded ['internal ' ]['attempts ' ])) {
199+ $ decoded ['internal ' ]['attempts ' ] = 0 ;
200+ } else {
201+ $ decoded ['internal ' ]['attempts ' ]++;
202+ }
203+
204+ return json_encode ($ decoded );
205+ }
206+
185207 /**
186208 * Pop the next job off of the queue.
187209 *
0 commit comments