Skip to content

Commit 1aa19f0

Browse files
committed
Do not delete task if task status is 20
1 parent 2282462 commit 1aa19f0

File tree

2 files changed

+23
-3
lines changed

2 files changed

+23
-3
lines changed

src/CloudTasksJob.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,11 +103,31 @@ public function timeoutAt(): ?int
103103

104104
public function delete(): void
105105
{
106+
// Laravel automatically calls delete() after a job is processed successfully. However, this is
107+
// not what we want to happen in Cloud Tasks because Cloud Tasks will also delete the task upon
108+
// a 200 OK status, which means a task is deleted twice, possibly resulting in errors. So if the
109+
// task was processed successfully (no errors or failures) then we will not delete the task
110+
// manually and will let Cloud Tasks do it.
111+
$successful =
112+
// If the task has failed, we should be able to delete it permanently
113+
$this->hasFailed() === false
114+
// If the task has errored, it should be released, which in process deletes the errored task
115+
&& $this->hasError() === false;
116+
117+
if ($successful) {
118+
return;
119+
}
120+
106121
parent::delete();
107122

108123
$this->cloudTasksQueue->delete($this);
109124
}
110125

126+
public function hasError(): bool
127+
{
128+
return data_get($this->job, 'internal.errored') === true;
129+
}
130+
111131
public function release($delay = 0)
112132
{
113133
parent::release();

tests/QueueTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ public function jobs_can_be_released()
242242

243243
// Assert
244244
Event::assertNotDispatched($this->getJobReleasedAfterExceptionEvent());
245-
CloudTasksApi::assertDeletedTaskCount(1);
245+
CloudTasksApi::assertDeletedTaskCount(0); // it returned 200 OK so we dont delete it, but Google does
246246
$releasedJob = null;
247247
Event::assertDispatched(JobReleased::class, function (JobReleased $event) use (&$releasedJob) {
248248
$releasedJob = $event->job;
@@ -257,7 +257,7 @@ public function jobs_can_be_released()
257257

258258
$this->runFromPayload($releasedJob->getRawBody());
259259

260-
CloudTasksApi::assertDeletedTaskCount(2);
260+
CloudTasksApi::assertDeletedTaskCount(0);
261261
CloudTasksApi::assertTaskCreated(function (Task $task) {
262262
$body = $task->getHttpRequest()->getBody();
263263
$decoded = json_decode($body, true);
@@ -476,6 +476,6 @@ public function test_ignoring_jobs_with_deleted_models()
476476

477477
// Act
478478
Log::assertLogged('UserJob:John');
479-
CloudTasksApi::assertTaskDeleted($job->task->getName());
479+
CloudTasksApi::assertTaskNotDeleted($job->task->getName());
480480
}
481481
}

0 commit comments

Comments
 (0)