From 83c59408dc28eb2955e63f9a57f360f3bd614790 Mon Sep 17 00:00:00 2001 From: Ivan Ramello Date: Wed, 25 Nov 2026 13:16:08 -0300 Subject: [PATCH 1/2] Send managed queue timeouts only when given managed-queue:create sent a 60 second visibility timeout and a 90 second shutdown timeout on every run, from its prompt defaults or its non-interactive fallbacks, and ship's managed queue hard-coded the same values. Laravel Cloud applies custom queue timeouts only for organizations with the advanced managed queues entitlement, and ignores them for every other organization. The command now sends each timeout only when it is passed as an option or answered at the prompt. An empty answer, like a missing option, lets Laravel Cloud choose. ship's queue leaves both to Laravel Cloud. --- README.md | 2 +- app/Commands/ManagedQueueCreate.php | 64 ++++++++++++-------- app/Concerns/ProvisionsQueueWorker.php | 4 +- tests/Feature/ManagedQueueCreateTest.php | 74 ++++++++++++++++++++++++ tests/Feature/ShipQueueTest.php | 2 + 5 files changed, 118 insertions(+), 28 deletions(-) diff --git a/README.md b/README.md index b904fa8e..7fa5fafa 100644 --- a/README.md +++ b/README.md @@ -370,7 +370,7 @@ Managed queues are a special kind of instance. Pass the instance ID directly or | `cloud managed-queue:retry-failed-job` | Retry a failed job on a managed queue | | `cloud managed-queue:delete-failed-job` | Delete failed jobs from a managed queue | -`managed-queue:create` accepts `--name`, `--size`, `--max-workers`, `--visibility-timeout`, `--polling-interval` and `--shutdown-timeout` so it can run non-interactively. `pause`, `purge` and `delete-failed-job` accept `--force` to skip the confirmation prompt. `delete-failed-job` takes one or more job IDs, or `--failed-before` to delete every job that failed before a date. +`managed-queue:create` accepts `--name`, `--size`, `--max-workers`, `--visibility-timeout`, `--polling-interval` and `--shutdown-timeout` so it can run non-interactively. The two timeouts are sent only when given: Laravel Cloud chooses them otherwise, and applies custom values only for organizations with the advanced managed queues entitlement. `pause`, `purge` and `delete-failed-job` accept `--force` to skip the confirmation prompt. `delete-failed-job` takes one or more job IDs, or `--failed-before` to delete every job that failed before a date. ### Usage diff --git a/app/Commands/ManagedQueueCreate.php b/app/Commands/ManagedQueueCreate.php index 9878409f..80e52d2f 100644 --- a/app/Commands/ManagedQueueCreate.php +++ b/app/Commands/ManagedQueueCreate.php @@ -25,9 +25,9 @@ class ManagedQueueCreate extends BaseCommand {--name= : Queue name} {--size= : Instance size} {--max-workers= : Maximum number of workers} - {--visibility-timeout= : Visibility timeout in seconds (0-43200)} + {--visibility-timeout= : Visibility timeout in seconds (0-43200). Needs the advanced managed queues entitlement; left out, Laravel Cloud chooses it} {--polling-interval= : Polling interval in seconds (0-60), previous-generation sizes only} - {--shutdown-timeout= : Shutdown timeout in seconds} + {--shutdown-timeout= : Shutdown timeout in seconds. Needs the advanced managed queues entitlement; left out, Laravel Cloud chooses it} {--fifo : Create a FIFO queue; Laravel Cloud appends .fifo to the name}'; protected $description = 'Create a new managed queue'; @@ -118,20 +118,13 @@ protected function createManagedQueue(string $environmentId, bool $fifo) 'max-workers', ); - $this->form()->prompt( + $this->promptForQueueTimeout( 'visibility_timeout', - fn ($resolver) => $resolver - ->fromInput( - fn ($value) => (int) number( - label: 'Visibility timeout (seconds)', - hint: 'How long an in-flight job is hidden from other workers (0-43200)', - default: $value ?? '60', - min: 0, - max: 43200, - ), - ) - ->nonInteractively(fn () => 60), 'visibility-timeout', + label: 'Visibility timeout (seconds)', + hint: 'How long an in-flight job is hidden from other workers (0-43200). Leave empty to let Laravel Cloud choose; a custom value needs the advanced managed queues entitlement.', + min: 0, + max: 43200, ); $size = $this->form()->get('size'); @@ -159,19 +152,12 @@ protected function createManagedQueue(string $environmentId, bool $fifo) ); } - $this->form()->prompt( + $this->promptForQueueTimeout( 'shutdown_timeout', - fn ($resolver) => $resolver - ->fromInput( - fn ($value) => (int) number( - label: 'Shutdown timeout (seconds)', - hint: 'Grace period for workers to finish their current job before terminating', - default: $value ?? '90', - min: 1, - ), - ) - ->nonInteractively(fn () => 90), 'shutdown-timeout', + label: 'Shutdown timeout (seconds)', + hint: 'Grace period for workers to finish their current job before terminating. Leave empty to let Laravel Cloud choose; a custom value needs the advanced managed queues entitlement.', + min: 1, ); $this->form()->prompt( @@ -259,4 +245,32 @@ protected function isCurrentGeneration(?string $size): bool { return str_starts_with((string) $size, 'mq.'); } + + /** + * Laravel Cloud applies a custom queue timeout only for organizations with the advanced managed + * queues entitlement, and chooses its own otherwise. So a timeout is sent only when one was given, + * never as a default: not for a missing option, and not for an empty answer. + */ + protected function promptForQueueTimeout(string $key, string $option, string $label, string $hint, int $min, ?int $max = null): void + { + if (! $this->isInteractive() && $this->option($option) === null) { + return; + } + + $this->form()->prompt( + $key, + fn ($resolver) => $resolver->fromInput(function ($value) use ($label, $hint, $min, $max) { + $answer = number( + label: $label, + default: (string) ($value ?? ''), + hint: $hint, + min: $min, + max: $max, + ); + + return $answer === '' ? null : (int) $answer; + }), + $option, + ); + } } diff --git a/app/Concerns/ProvisionsQueueWorker.php b/app/Concerns/ProvisionsQueueWorker.php index 3ab1f8ec..629e9282 100644 --- a/app/Concerns/ProvisionsQueueWorker.php +++ b/app/Concerns/ProvisionsQueueWorker.php @@ -198,9 +198,9 @@ protected function createManagedQueue(Environment $environment): void scalingType: InstanceScalingType::CUSTOM, minReplicas: 1, maxReplicas: 3, - visibilityTimeout: 60, + // No visibility or shutdown timeout: Laravel Cloud chooses them, and applies custom ones + // only for organizations with the advanced managed queues entitlement. pollingInterval: str_starts_with($size, 'mq.') ? null : 5, - shutdownTimeout: 90, backgroundProcesses: [ [ 'type' => 'worker', diff --git a/tests/Feature/ManagedQueueCreateTest.php b/tests/Feature/ManagedQueueCreateTest.php index 48cbff52..9ca5f92a 100644 --- a/tests/Feature/ManagedQueueCreateTest.php +++ b/tests/Feature/ManagedQueueCreateTest.php @@ -202,3 +202,77 @@ function setupManagedQueueCreateMocks(): Closure expect($sentBody()['fifo'])->toBeTrue(); }); + +it('sends no queue timeouts unless given', function () { + $sentBody = setupManagedQueueCreateMocks(); + + $this->artisan('managed-queue:create', [ + 'environment' => 'env-1', + '--name' => 'emails', + '--size' => 'mq.pro.1gb', + '--no-interaction' => true, + ])->assertSuccessful(); + + expect($sentBody()) + ->not->toHaveKey('visibility_timeout') + ->not->toHaveKey('shutdown_timeout'); +}); + +it('sends the queue timeouts given as options', function () { + $sentBody = setupManagedQueueCreateMocks(); + + $this->artisan('managed-queue:create', [ + 'environment' => 'env-1', + '--name' => 'emails', + '--size' => 'mq.pro.1gb', + '--visibility-timeout' => '240', + '--shutdown-timeout' => '75', + '--no-interaction' => true, + ])->assertSuccessful(); + + expect($sentBody())->toMatchArray([ + 'visibility_timeout' => 240, + 'shutdown_timeout' => 75, + ]); +}); + +it('sends no queue timeouts for empty answers when interactive', function () { + $sentBody = setupManagedQueueCreateMocks(); + + Artisan::registerCommand(new InteractiveManagedQueueCreateCommand); + + // Not FIFO, empty visibility and shutdown timeouts, then the backoff, tries and timeout defaults. + Prompt::fake([Key::ENTER, Key::ENTER, Key::ENTER, Key::ENTER, Key::ENTER, Key::ENTER]); + + $this->artisan('test:interactive-managed-queue-create', [ + 'environment' => 'env-1', + '--name' => 'orders', + '--size' => 'mq.pro.1gb', + '--max-workers' => 3, + ])->assertSuccessful(); + + expect($sentBody()) + ->not->toHaveKey('visibility_timeout') + ->not->toHaveKey('shutdown_timeout'); +}); + +it('sends the queue timeouts answered when interactive', function () { + $sentBody = setupManagedQueueCreateMocks(); + + Artisan::registerCommand(new InteractiveManagedQueueCreateCommand); + + // Not FIFO, a 240 second visibility and 75 second shutdown timeout, then the job defaults. + Prompt::fake([Key::ENTER, '2', '4', '0', Key::ENTER, '7', '5', Key::ENTER, Key::ENTER, Key::ENTER, Key::ENTER]); + + $this->artisan('test:interactive-managed-queue-create', [ + 'environment' => 'env-1', + '--name' => 'orders', + '--size' => 'mq.pro.1gb', + '--max-workers' => 3, + ])->assertSuccessful(); + + expect($sentBody())->toMatchArray([ + 'visibility_timeout' => 240, + 'shutdown_timeout' => 75, + ]); +}); diff --git a/tests/Feature/ShipQueueTest.php b/tests/Feature/ShipQueueTest.php index 91a45eca..65de6873 100644 --- a/tests/Feature/ShipQueueTest.php +++ b/tests/Feature/ShipQueueTest.php @@ -170,5 +170,7 @@ function requireComposerPackages(string $repo, array $packages): void 'name' => 'default', ]) ->and($sent->body)->not->toHaveKey('polling_interval') + ->and($sent->body)->not->toHaveKey('visibility_timeout') + ->and($sent->body)->not->toHaveKey('shutdown_timeout') ->and($sent->body['background_processes'][0]['config']['connection'])->toBe('cloud'); }); From 9652012c7af3aa70351de09e8b87b044bb03f23a Mon Sep 17 00:00:00 2001 From: Ivan Ramello Date: Wed, 25 Nov 2026 13:35:13 -0300 Subject: [PATCH 2/2] Keep empty queue timeout answers when retrying after a validation error An empty answer was stored as null, which the form reads as unanswered, so when the API rejected another field the CLI asked for both timeouts again. The answer now stays empty in the form and is turned into no timeout only when the request is built, so a retry asks only for the fields with errors. --- app/Commands/ManagedQueueCreate.php | 34 ++++++++------ tests/Feature/ManagedQueueCreateTest.php | 59 ++++++++++++++++++++++++ 2 files changed, 80 insertions(+), 13 deletions(-) diff --git a/app/Commands/ManagedQueueCreate.php b/app/Commands/ManagedQueueCreate.php index 80e52d2f..4322f9aa 100644 --- a/app/Commands/ManagedQueueCreate.php +++ b/app/Commands/ManagedQueueCreate.php @@ -217,9 +217,9 @@ protected function createManagedQueue(string $environmentId, bool $fifo) scalingType: InstanceScalingType::CUSTOM, minReplicas: 1, maxReplicas: $this->form()->integer('max_replicas'), - visibilityTimeout: $this->form()->integer('visibility_timeout'), + visibilityTimeout: $this->queueTimeout('visibility_timeout'), pollingInterval: $this->isCurrentGeneration($size) ? null : $this->form()->integer('polling_interval'), - shutdownTimeout: $this->form()->integer('shutdown_timeout'), + shutdownTimeout: $this->queueTimeout('shutdown_timeout'), backgroundProcesses: [ [ 'type' => 'worker', @@ -259,18 +259,26 @@ protected function promptForQueueTimeout(string $key, string $option, string $la $this->form()->prompt( $key, - fn ($resolver) => $resolver->fromInput(function ($value) use ($label, $hint, $min, $max) { - $answer = number( - label: $label, - default: (string) ($value ?? ''), - hint: $hint, - min: $min, - max: $max, - ); - - return $answer === '' ? null : (int) $answer; - }), + // An empty answer stays '' rather than null: the form asks again for null fields when the + // API rejects another one, and the user already chose to leave this one to Laravel Cloud. + fn ($resolver) => $resolver->fromInput(fn ($value) => number( + label: $label, + default: (string) ($value ?? ''), + hint: $hint, + min: $min, + max: $max, + )), $option, ); } + + /** + * The timeout to send: none when it was left out or answered empty. + */ + protected function queueTimeout(string $key): ?int + { + $value = $this->form()->get($key); + + return $value === null || $value === '' ? null : (int) $value; + } } diff --git a/tests/Feature/ManagedQueueCreateTest.php b/tests/Feature/ManagedQueueCreateTest.php index 9ca5f92a..d0106bb4 100644 --- a/tests/Feature/ManagedQueueCreateTest.php +++ b/tests/Feature/ManagedQueueCreateTest.php @@ -276,3 +276,62 @@ function setupManagedQueueCreateMocks(): Closure 'shutdown_timeout' => 75, ]); }); + +it('does not ask again for queue timeouts left empty when the API rejects another field', function () { + $bodies = []; + + MockClient::global([ + GetOrganizationRequest::class => MockResponse::make(organizationResponse(), 200), + GetEnvironmentRequest::class => MockResponse::make(['data' => createEnvironmentResponse()], 200), + ListInstanceSizesRequest::class => MockResponse::make([ + 'data' => ['managed_queue' => [managedQueueSize('mq.pro.1gb')]], + ], 200), + CreateInstanceRequest::class => function (PendingRequest $request) use (&$bodies) { + $bodies[] = $request->body()->all(); + + return count($bodies) === 1 + ? MockResponse::make(['message' => 'Invalid.', 'errors' => ['name' => ['The name has already been taken.']]], 422) + : MockResponse::make(['data' => [ + 'id' => 'instance-1', + 'type' => 'instances', + 'attributes' => ['name' => 'orders', 'type' => 'managed_queue', 'size' => 'mq.pro.1gb', 'scaling_type' => 'custom', 'min_replicas' => 1, 'max_replicas' => 3, 'uses_scheduler' => false], + ]], 201); + }, + ]); + + Artisan::registerCommand(new InteractiveManagedQueueCreateCommand); + + Prompt::fake([ + // Not FIFO, empty visibility and shutdown timeouts, then the backoff, tries and timeout defaults. + Key::ENTER, Key::ENTER, Key::ENTER, Key::ENTER, Key::ENTER, Key::ENTER, + // Asked for the name again, and kept. Nothing else: asking for a timeout again reads a key too many. + Key::ENTER, + ]); + + $this->artisan('test:interactive-managed-queue-create', [ + 'environment' => 'env-1', + '--name' => 'orders', + '--size' => 'mq.pro.1gb', + '--max-workers' => 3, + ])->assertSuccessful(); + + expect($bodies)->toHaveCount(2) + ->and($bodies[1])->not->toHaveKey('visibility_timeout') + ->and($bodies[1])->not->toHaveKey('shutdown_timeout'); +}); + +it('sends a zero visibility timeout and leaves out the shutdown timeout', function () { + $sentBody = setupManagedQueueCreateMocks(); + + $this->artisan('managed-queue:create', [ + 'environment' => 'env-1', + '--name' => 'emails', + '--size' => 'mq.pro.1gb', + '--visibility-timeout' => '0', + '--no-interaction' => true, + ])->assertSuccessful(); + + expect($sentBody()) + ->toMatchArray(['visibility_timeout' => 0]) + ->not->toHaveKey('shutdown_timeout'); +});