Skip to content
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/Traits/Timers.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use function React\Promise\resolve;
use Workflow\Serializers\Serializer;
use Workflow\Timer;
use Workflow\V2\Support\BackendCapabilities;

trait Timers
{
Expand Down Expand Up @@ -84,7 +85,7 @@ public static function timer(int|string|CarbonInterval $seconds): PromiseInterfa
$connection = self::connection() ?? config('queue.default');
$driver = config('queue.connections.' . $connection . '.driver');

if ($driver === 'sqs') {
if (BackendCapabilities::isSqsBackedDriver($driver)) {
$maxDelay = self::$context->now->copy()->addSeconds(900);
if ($timer->stop_at->greaterThan($maxDelay)) {
$delay = $maxDelay;
Expand Down
16 changes: 15 additions & 1 deletion src/V2/Support/BackendCapabilities.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,20 @@ final class BackendCapabilities
'workflow-serializer-base64',
];

/**
* Queue drivers backed by Amazon SQS, which rejects message delays over
* 900 seconds. Laravel Cloud's managed queues use the `cloud` driver,
* which wraps SQS.
*
* @var list<string>
*/
private const SQS_BACKED_DRIVERS = ['sqs', 'cloud'];

public static function isSqsBackedDriver(mixed $driver): bool
{
return in_array($driver, self::SQS_BACKED_DRIVERS, true);
}

/**
* @return array<string, mixed>
*/
Expand Down Expand Up @@ -172,7 +186,7 @@ private static function queue(?string $configuredConnection = null): array
'async_delivery' => $driver !== null && $driver !== 'sync',
'delayed_delivery' => $driver !== null && $driver !== 'sync',
'requires_worker' => ! $pollMode,
'max_delay_seconds' => $driver === 'sqs' ? 900 : null,
'max_delay_seconds' => self::isSqsBackedDriver($driver) ? 900 : null,
'after_commit_option' => $connection === null ? null : config(
sprintf('queue.connections.%s.after_commit', $connection)
),
Expand Down
2 changes: 1 addition & 1 deletion src/V2/Support/TimerTransportChunker.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public static function maxReleaseDelaySeconds(?string $connection = null): ?int
$snapshot = BackendCapabilities::snapshot(queueConnection: $connection);
$driver = $snapshot['queue']['driver'] ?? null;

if ($driver === 'sqs') {
if (BackendCapabilities::isSqsBackedDriver($driver)) {
return self::MAX_RELEASE_SECONDS_SQS;
}

Expand Down
52 changes: 52 additions & 0 deletions tests/Unit/Traits/TimersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -361,4 +361,56 @@ public function testTimerCapsDelayForSqsDriver(): void
return true;
});
}

public function testTimerCapsDelayForLaravelCloudDriver(): void
{
Bus::fake();

config([
'queue.default' => 'cloud',
]);
config([
'queue.connections.cloud' => [
'driver' => 'cloud',
'queue' => 'default',
],
]);

$workflow = WorkflowStub::load(WorkflowStub::make(TestWorkflow::class)->id());
$storedWorkflow = StoredWorkflow::findOrFail($workflow->id());
$storedWorkflow->class = TestChildWorkflow::class;
$storedWorkflow->update([
'arguments' => Serializer::serialize([]),
'status' => WorkflowPendingStatus::$name,
]);

$now = now();
WorkflowStub::setContext([
'storedWorkflow' => $storedWorkflow,
'index' => 0,
'now' => $now,
'replaying' => false,
]);

$storedWorkflow->timers()
->create([
'index' => 0,
'stop_at' => $now->copy()
->addHour(),
]);

WorkflowStub::timer(3600)
->then(static function ($value) use (&$result) {
$result = $value;
});

$this->assertNull($result);

Bus::assertDispatched(Timer::class, function ($job) use ($now) {
$delaySeconds = $job->delay->diffInSeconds($now);
$this->assertLessThanOrEqual(900, $delaySeconds);
$this->assertGreaterThanOrEqual(899, $delaySeconds);
return true;
});
}
}
18 changes: 18 additions & 0 deletions tests/Unit/V2/TimerTransportChunkerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,24 @@ public function testMaxDispatchDelayReturns900ForSqsDriver(): void
$this->assertSame(900, TimerTransportChunker::maxDispatchDelaySeconds());
}

public function testMaxDispatchDelayReturns900ForLaravelCloudDriver(): void
{
config()->set('queue.default', 'cloud');
config()
->set('queue.connections.cloud.driver', 'cloud');

$this->assertSame(900, TimerTransportChunker::maxDispatchDelaySeconds());
}

public function testMaxReleaseDelayReturns43200ForLaravelCloudDriver(): void
{
config()->set('queue.default', 'cloud');
config()
->set('queue.connections.cloud.driver', 'cloud');

$this->assertSame(43200, TimerTransportChunker::maxReleaseDelaySeconds());
}

public function testMaxReleaseDelayReturnsNullForNonSqsDriver(): void
{
config()->set('queue.default', 'redis');
Expand Down
Loading