Skip to content

Commit

Permalink
Merge pull request #58 from stackkit/master
Browse files Browse the repository at this point in the history
Bugfix: task retries
  • Loading branch information
marickvantuil authored Jun 24, 2022
2 parents 19c7267 + cef2e32 commit 33638b2
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 13 deletions.
7 changes: 0 additions & 7 deletions src/CloudTasksJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,4 @@ public function delete(): void

$this->cloudTasksQueue->delete($this);
}

public function fire(): void
{
$this->attempts++;

parent::fire();
}
}
13 changes: 11 additions & 2 deletions src/TaskHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ private function handleTask(array $task): void

$this->loadQueueRetryConfig($job);

$job->setAttempts((int) request()->header('X-CloudTasks-TaskExecutionCount'));
$job->setAttempts((int) request()->header('X-CloudTasks-TaskRetryCount'));
$job->setMaxTries($this->retryConfig->getMaxAttempts());

// If the job is being attempted again we also check if a
Expand All @@ -108,9 +108,18 @@ private function handleTask(array $task): void
throw new UnexpectedValueException('Expected task name to be a string.');
}

$job->setRetryUntil(CloudTasksApi::getRetryUntilTimestamp($taskName));
$fullTaskName = $this->client->taskName(
$this->config['project'],
$this->config['location'],
$job->getQueue() ?: $this->config['queue'],
$taskName,
);

$job->setRetryUntil(CloudTasksApi::getRetryUntilTimestamp($fullTaskName));
}

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

app('queue.worker')->process($this->config['connection'], $job, new WorkerOptions());
}

Expand Down
8 changes: 4 additions & 4 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,16 +143,16 @@ public function run(): void
app(TaskHandler::class)->handle($this->payload);
});

$taskExecutionCount = request()->header('X-CloudTasks-TaskExecutionCount', 0);
request()->headers->set('X-CloudTasks-TaskExecutionCount', $taskExecutionCount + 1);
$taskRetryCount = request()->header('X-CloudTasks-TaskRetryCount', 0);
request()->headers->set('X-CloudTasks-TaskRetryCount', $taskRetryCount + 1);
}

public function runWithoutExceptionHandler(): void
{
app(TaskHandler::class)->handle($this->payload);

$taskExecutionCount = request()->header('X-CloudTasks-TaskExecutionCount', 0);
request()->headers->set('X-CloudTasks-TaskExecutionCount', $taskExecutionCount + 1);
$taskRetryCount = request()->header('X-CloudTasks-TaskRetryCount', 0);
request()->headers->set('X-CloudTasks-TaskRetryCount', $taskRetryCount + 1);
}
};
}
Expand Down

0 comments on commit 33638b2

Please sign in to comment.