Skip to content

Commit

Permalink
Merge pull request #106 from stackkit/bugfix/duplicate-job-name
Browse files Browse the repository at this point in the history
Append timestamp to task name to prevent duplicates
  • Loading branch information
marickvantuil authored May 27, 2023
2 parents 0aa376b + 40b2701 commit 4a3481f
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
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';
});
}
}

0 comments on commit 4a3481f

Please sign in to comment.