Skip to content

Commit 64595e9

Browse files
committed
Prevent already deleted tasks from being called again
1 parent 1aa19f0 commit 64595e9

File tree

7 files changed

+26
-21
lines changed

7 files changed

+26
-21
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22
composer.lock
33
.idea/
44
.phpunit.result.cache
5+
.phpunit.cache

src/CloudTasksApi.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* @method static Task createTask(string $queueName, Task $task)
1414
* @method static void deleteTask(string $taskName)
1515
* @method static Task getTask(string $taskName)
16-
* @method static int|null getRetryUntilTimestamp(string $taskName)
16+
* @method static int|null getRetryUntilTimestamp(Task $task)
1717
*/
1818
class CloudTasksApi extends Facade
1919
{

src/CloudTasksApiConcrete.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,8 @@ public function getTask(string $taskName): Task
5050
return $this->client->getTask($taskName);
5151
}
5252

53-
public function getRetryUntilTimestamp(string $taskName): ?int
53+
public function getRetryUntilTimestamp(Task $task): ?int
5454
{
55-
$task = $this->getTask($taskName);
56-
5755
$attempt = $task->getFirstAttempt();
5856

5957
if (!$attempt instanceof Attempt) {

src/CloudTasksApiContract.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,5 @@ public function getRetryConfig(string $queueName): RetryConfig;
1313
public function createTask(string $queueName, Task $task): Task;
1414
public function deleteTask(string $taskName): void;
1515
public function getTask(string $taskName): Task;
16-
public function getRetryUntilTimestamp(string $taskName): ?int;
16+
public function getRetryUntilTimestamp(Task $task): ?int;
1717
}

src/CloudTasksApiFake.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public function getTask(string $taskName): Task
4949
}
5050

5151

52-
public function getRetryUntilTimestamp(string $taskName): ?int
52+
public function getRetryUntilTimestamp(Task $task): ?int
5353
{
5454
return null;
5555
}

src/TaskHandler.php

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Stackkit\LaravelGoogleCloudTasksQueue;
44

5+
use Google\ApiCore\ApiException;
56
use Google\Cloud\Tasks\V2\CloudTasksClient;
67
use Google\Cloud\Tasks\V2\RetryConfig;
78
use Illuminate\Bus\Queueable;
@@ -122,6 +123,24 @@ private function handleTask(array $task): void
122123

123124
$this->loadQueueRetryConfig($job);
124125

126+
$taskName = request()->header('X-Cloudtasks-Taskname');
127+
$fullTaskName = $this->client->taskName(
128+
$this->config['project'],
129+
$this->config['location'],
130+
$job->getQueue() ?: $this->config['queue'],
131+
$taskName,
132+
);
133+
134+
try {
135+
$apiTask = CloudTasksApi::getTask($fullTaskName);
136+
} catch (ApiException $e) {
137+
if (in_array($e->getStatus(), ['NOT_FOUND', 'PRECONDITION_FAILED'])) {
138+
abort(404);
139+
}
140+
141+
throw $e;
142+
}
143+
125144
// If the task has a [X-CloudTasks-TaskRetryCount] header higher than 0, then
126145
// we know the job was created using an earlier version of the package. This
127146
// job does not have the attempts tracked internally yet.
@@ -138,20 +157,7 @@ private function handleTask(array $task): void
138157
// max retry duration has been set. If that duration
139158
// has passed, it should stop trying altogether.
140159
if ($job->attempts() > 0) {
141-
$taskName = request()->header('X-Cloudtasks-Taskname');
142-
143-
if (!is_string($taskName)) {
144-
throw new UnexpectedValueException('Expected task name to be a string.');
145-
}
146-
147-
$fullTaskName = $this->client->taskName(
148-
$this->config['project'],
149-
$this->config['location'],
150-
$job->getQueue() ?: $this->config['queue'],
151-
$taskName,
152-
);
153-
154-
$job->setRetryUntil(CloudTasksApi::getRetryUntilTimestamp($fullTaskName));
160+
$job->setRetryUntil(CloudTasksApi::getRetryUntilTimestamp($apiTask));
155161
}
156162

157163
$job->setAttempts($job->attempts() + 1);

tests/CloudTasksApiTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ public function test_get_retry_until_timestamp()
184184
// The queue max retry duration is 5 seconds. The max retry until timestamp is calculated from the
185185
// first attempt, so we expect it to be [timestamp first attempt] + 5 seconds.
186186
$expected = $createdTask->getFirstAttempt()->getDispatchTime()->getSeconds() + 5;
187-
$actual = CloudTasksApi::getRetryUntilTimestamp($createdTask->getName());
187+
$actual = CloudTasksApi::getRetryUntilTimestamp($createdTask);
188188
$this->assertSame($expected, $actual);
189189
}
190190
}

0 commit comments

Comments
 (0)