Skip to content

Commit 8f41ea1

Browse files
committed
Track job attempts internally
1 parent 8d6e8b2 commit 8f41ea1

File tree

3 files changed

+36
-1
lines changed

3 files changed

+36
-1
lines changed

src/CloudTasksQueue.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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
*

src/TaskHandler.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ private function handleTask(array $task): void
124124

125125
$this->loadQueueRetryConfig($job);
126126

127-
$job->setAttempts((int) request()->header('X-CloudTasks-TaskRetryCount'));
127+
$job->setAttempts($task['internal']['attempts']);
128128
$job->setMaxTries($this->retryConfig->getMaxAttempts());
129129

130130
// If the job is being attempted again we also check if a

tests/TestCase.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,8 @@ public function run(): void
149149
rescue(function (): void {
150150
app(TaskHandler::class)->handle($this->payload);
151151
});
152+
153+
$this->payload = $this->incrementAttempts($this->payload);
152154
}
153155

154156
public function runWithoutExceptionHandler(): void
@@ -158,6 +160,17 @@ public function runWithoutExceptionHandler(): void
158160

159161
app(TaskHandler::class)->handle($this->payload);
160162

163+
$this->payload = $this->incrementAttempts($this->payload);
164+
}
165+
166+
private function incrementAttempts(string $payload): string
167+
{
168+
$decoded = \Safe\json_decode($payload, true);
169+
170+
$decoded['internal']['attempts'] ??= 0;
171+
$decoded['internal']['attempts']++;
172+
173+
return json_encode($decoded);
161174
}
162175
};
163176
}

0 commit comments

Comments
 (0)