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..4322f9aa 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( @@ -231,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,4 +245,40 @@ 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, + // 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/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..d0106bb4 100644 --- a/tests/Feature/ManagedQueueCreateTest.php +++ b/tests/Feature/ManagedQueueCreateTest.php @@ -202,3 +202,136 @@ 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, + ]); +}); + +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'); +}); 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'); });