Skip to content

Append timestamp to task name to prevent duplicates #106

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/CloudTasksQueue.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Google\Protobuf\Timestamp;
use Illuminate\Contracts\Queue\Queue as QueueContract;
use Illuminate\Queue\Queue as LaravelQueue;
use Illuminate\Support\Carbon;
use Illuminate\Support\Str;
use Stackkit\LaravelGoogleCloudTasksQueue\Events\TaskCreated;
use function Safe\json_decode;
Expand Down Expand Up @@ -197,7 +198,7 @@ private function taskName(string $queueName, array $payload): string
$this->config['project'],
$this->config['location'],
$queueName,
$displayName . '-' . $payload['uuid']
$displayName . '-' . $payload['uuid'] . '-' . Carbon::now()->getTimestamp(),
);
}

Expand Down
31 changes: 31 additions & 0 deletions tests/TaskHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@

use Firebase\JWT\ExpiredException;
use Google\Cloud\Tasks\V2\RetryConfig;
use Google\Cloud\Tasks\V2\Task;
use Google\Protobuf\Duration;
use Illuminate\Queue\Events\JobProcessed;
use Illuminate\Queue\Events\JobProcessing;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Event;
use Illuminate\Support\Facades\Log;
use Stackkit\LaravelGoogleCloudTasksQueue\CloudTasksApi;
Expand Down Expand Up @@ -475,4 +477,33 @@ public function attempts_are_copied_from_x_header()
return $event->job->attempts() === 7;
});
}

/**
* @test
*/
public function retried_jobs_get_a_new_name()
{
// Arrange
OpenIdVerificator::fake();
Event::fake($this->getJobReleasedAfterExceptionEvent());
CloudTasksApi::fake();

// Act & Assert
Carbon::setTestNow(Carbon::createFromTimestamp(1685035628));
$job = $this->dispatch(new FailingJob());
Carbon::setTestNow(Carbon::createFromTimestamp(1685035629));

$job->run();

// Assert
CloudTasksApi::assertCreatedTaskCount(2);
CloudTasksApi::assertTaskCreated(function (Task $task): bool {
[$timestamp] = array_reverse(explode('-', $task->getName()));
return $timestamp === '1685035628';
});
CloudTasksApi::assertTaskCreated(function (Task $task): bool {
[$timestamp] = array_reverse(explode('-', $task->getName()));
return $timestamp === '1685035629';
});
}
}