File tree Expand file tree Collapse file tree 2 files changed +23
-3
lines changed Expand file tree Collapse file tree 2 files changed +23
-3
lines changed Original file line number Diff line number Diff line change @@ -103,11 +103,31 @@ public function timeoutAt(): ?int
103
103
104
104
public function delete (): void
105
105
{
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
+
106
121
parent ::delete ();
107
122
108
123
$ this ->cloudTasksQueue ->delete ($ this );
109
124
}
110
125
126
+ public function hasError (): bool
127
+ {
128
+ return data_get ($ this ->job , 'internal.errored ' ) === true ;
129
+ }
130
+
111
131
public function release ($ delay = 0 )
112
132
{
113
133
parent ::release ();
Original file line number Diff line number Diff line change @@ -242,7 +242,7 @@ public function jobs_can_be_released()
242
242
243
243
// Assert
244
244
Event::assertNotDispatched ($ this ->getJobReleasedAfterExceptionEvent ());
245
- CloudTasksApi::assertDeletedTaskCount (1 );
245
+ CloudTasksApi::assertDeletedTaskCount (0 ); // it returned 200 OK so we dont delete it, but Google does
246
246
$ releasedJob = null ;
247
247
Event::assertDispatched (JobReleased::class, function (JobReleased $ event ) use (&$ releasedJob ) {
248
248
$ releasedJob = $ event ->job ;
@@ -257,7 +257,7 @@ public function jobs_can_be_released()
257
257
258
258
$ this ->runFromPayload ($ releasedJob ->getRawBody ());
259
259
260
- CloudTasksApi::assertDeletedTaskCount (2 );
260
+ CloudTasksApi::assertDeletedTaskCount (0 );
261
261
CloudTasksApi::assertTaskCreated (function (Task $ task ) {
262
262
$ body = $ task ->getHttpRequest ()->getBody ();
263
263
$ decoded = json_decode ($ body , true );
@@ -476,6 +476,6 @@ public function test_ignoring_jobs_with_deleted_models()
476
476
477
477
// Act
478
478
Log::assertLogged ('UserJob:John ' );
479
- CloudTasksApi::assertTaskDeleted ($ job ->task ->getName ());
479
+ CloudTasksApi::assertTaskNotDeleted ($ job ->task ->getName ());
480
480
}
481
481
}
You can’t perform that action at this time.
0 commit comments