Skip to content
Open
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
29 changes: 28 additions & 1 deletion src/Jobs/RecordBarstoolJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Saloon\Barstool\Jobs;

use DateTimeInterface;
use Saloon\Barstool\Models\Barstool;
use Saloon\Barstool\Enums\RecordingType;
use Illuminate\Foundation\Queue\Queueable;
Expand Down Expand Up @@ -35,6 +36,22 @@ public function uniqueId(): string
return "{$this->uuid}-{$this->type->value}";
}

/**
* Keep a response or fatal recording alive until its request row exists.
*
* Releasing this job still counts as an attempt. With only three tries, the
* worker fails it before handle() on the next pickup and the recording is lost.
* The deadline is fixed when the job is queued, and it takes precedence over tries.
*/
public function retryUntil(): ?DateTimeInterface
{
if ($this->type === RecordingType::REQUEST) {
return null;
}

return now()->addMinutes(10);
}

public function handle(): void
{
if ($this->type === RecordingType::REQUEST) {
Expand All @@ -54,11 +71,21 @@ public function handle(): void
->first();

if ($barstool === null) {
$this->release(2);
$this->release($this->releaseDelay());

return;
}

$barstool->update($this->data);
}

/**
* Use the configured backoff, then keep using its last delay.
*/
private function releaseDelay(): int
{
$index = min($this->attempts() - 1, count($this->backoff) - 1);

return $this->backoff[max(0, $index)];
}
}
86 changes: 86 additions & 0 deletions tests/BarstoolTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Illuminate\Support\Facades\Schema;
use Illuminate\Support\Facades\Artisan;
use Saloon\Barstool\Enums\RecordingType;
use Illuminate\Database\Schema\Blueprint;
use Saloon\Http\Connectors\NullConnector;
use Saloon\Barstool\Jobs\RecordBarstoolJob;
use Saloon\Http\Response as SaloonResponse;
Expand Down Expand Up @@ -1260,3 +1261,88 @@

expect(BarstoolRecorder::calculateDuration($pendingRequest))->toBe(1);
});

it('records a queued response that the worker handles before the request row exists', function () {
config()->set('queue.default', 'database');
config()->set('queue.failed.driver', 'database-uuids');
config()->set('queue.failed.table', 'failed_jobs');

Schema::create('jobs', function (Blueprint $table) {
$table->id();
$table->string('queue')->index();
$table->longText('payload');
$table->unsignedTinyInteger('attempts');
$table->unsignedInteger('reserved_at')->nullable();
$table->unsignedInteger('available_at');
$table->unsignedInteger('created_at');
});

Schema::create('failed_jobs', function (Blueprint $table) {
$table->id();
$table->string('uuid')->unique();
$table->text('connection');
$table->text('queue');
$table->longText('payload');
$table->longText('exception');
$table->timestamp('failed_at')->useCurrent();
});

$this->freezeTime();

$uuid = '11111111-1111-4111-8111-111111111111';

RecordBarstoolJob::dispatch(
RecordingType::RESPONSE,
[
'url' => 'https://tests.saloon.dev/api/user',
'response_headers' => [],
'response_body' => '{"data":"ok"}',
'response_status' => 200,
'successful' => true,
'duration' => 120,
],
$uuid,
);

foreach (range(1, 4) as $attempt) {
Artisan::call('queue:work', [
'--once' => true,
'--sleep' => 0,
]);

if ($attempt < 4) {
$this->travel(31)->seconds();
}
}

RecordBarstoolJob::dispatch(
RecordingType::REQUEST,
[
'connector_class' => NullConnector::class,
'request_class' => SoloUserRequest::class,
'method' => 'GET',
'url' => 'https://tests.saloon.dev/api/user',
'successful' => false,
],
$uuid,
);

Artisan::call('queue:work', [
'--once' => true,
'--sleep' => 0,
]);

$this->travel(31)->seconds();

Artisan::call('queue:work', [
'--once' => true,
'--sleep' => 0,
]);

$recording = Barstool::query()->where('uuid', $uuid)->first();

expect($recording)->not->toBeNull()
->and($recording->response_status)->toBe(200)
->and($recording->successful)->toBeTrue()
->and($recording->response_body)->toBe('{"data":"ok"}');
});
Loading