From efbe608048c3fcdedde9b1651c152e10a4668b5c Mon Sep 17 00:00:00 2001 From: Claude Date: Wed, 25 Nov 2026 11:15:50 +0000 Subject: [PATCH 1/2] Add cache:start and cache:stop The API can now start and stop a Laravel Valkey cache (POST /caches/{cache}/start and /stop). Both commands resolve the cache like cache:get and output the updated cache with --json. Stopping cuts environments off from the cache, so cache:stop confirms and needs --force when non-interactive. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01T1Sq4Y2hkEm7aPHvpU9eP2 --- README.md | 4 + app/Client/README.md | 2 + .../Resources/Caches/StartCacheRequest.php | 29 ++++++ .../Resources/Caches/StopCacheRequest.php | 29 ++++++ app/Client/Resources/CachesResource.php | 18 ++++ app/Commands/CacheStart.php | 35 ++++++++ app/Commands/CacheStop.php | 39 ++++++++ tests/Feature/CacheStartStopTest.php | 88 +++++++++++++++++++ 8 files changed, 244 insertions(+) create mode 100644 app/Client/Resources/Caches/StartCacheRequest.php create mode 100644 app/Client/Resources/Caches/StopCacheRequest.php create mode 100644 app/Commands/CacheStart.php create mode 100644 app/Commands/CacheStop.php create mode 100644 tests/Feature/CacheStartStopTest.php diff --git a/README.md b/README.md index 7fa5fafa..7d6796a9 100644 --- a/README.md +++ b/README.md @@ -262,9 +262,13 @@ op read "op://vault/stripe/api-key" | cloud secret:create --name=STRIPE_KEY -n | `cloud cache:create` | Create a cache | | `cloud cache:update` | Update a cache | | `cloud cache:delete` | Delete a cache | +| `cloud cache:start` | Start a stopped cache | +| `cloud cache:stop` | Stop a cache | | `cloud cache:types` | List available cache types | | `cloud cache:metrics` | View cache metrics | +Environments cannot connect to a stopped cache until it is started again, so `cache:stop` asks for confirmation (or `--force`). + ### Object storage (buckets) | Command | Description | diff --git a/app/Client/README.md b/app/Client/README.md index 919d5670..a699df61 100644 --- a/app/Client/README.md +++ b/app/Client/README.md @@ -138,6 +138,8 @@ $data = $response->json(); - `CreateCacheRequest` - Create a new cache - `UpdateCacheRequest` - Update a cache - `DeleteCacheRequest` - Delete a cache +- `StartCacheRequest` - Start a stopped cache +- `StopCacheRequest` - Stop a cache - `ListCacheTypesRequest` - List available cache types #### WebSocket Clusters diff --git a/app/Client/Resources/Caches/StartCacheRequest.php b/app/Client/Resources/Caches/StartCacheRequest.php new file mode 100644 index 00000000..85e94ee1 --- /dev/null +++ b/app/Client/Resources/Caches/StartCacheRequest.php @@ -0,0 +1,29 @@ +cacheId}/start"; + } + + public function createDtoFromResponse(Response $response): Cache + { + return Cache::createFromResponse($response->json()); + } +} diff --git a/app/Client/Resources/Caches/StopCacheRequest.php b/app/Client/Resources/Caches/StopCacheRequest.php new file mode 100644 index 00000000..8dc7f840 --- /dev/null +++ b/app/Client/Resources/Caches/StopCacheRequest.php @@ -0,0 +1,29 @@ +cacheId}/stop"; + } + + public function createDtoFromResponse(Response $response): Cache + { + return Cache::createFromResponse($response->json()); + } +} diff --git a/app/Client/Resources/CachesResource.php b/app/Client/Resources/CachesResource.php index 9d0aa328..60ad8490 100644 --- a/app/Client/Resources/CachesResource.php +++ b/app/Client/Resources/CachesResource.php @@ -10,6 +10,8 @@ use App\Client\Resources\Caches\GetCacheRequest; use App\Client\Resources\Caches\ListCachesRequest; use App\Client\Resources\Caches\ListCacheTypesRequest; +use App\Client\Resources\Caches\StartCacheRequest; +use App\Client\Resources\Caches\StopCacheRequest; use App\Client\Resources\Caches\UpdateCacheRequest; use App\Dto\Cache; use App\Dto\CacheMetrics; @@ -57,6 +59,22 @@ public function update(UpdateCacheRequestData $data): Cache return $request->createDtoFromResponse($response); } + public function start(string $cacheId): Cache + { + $request = new StartCacheRequest($cacheId); + $response = $this->send($request); + + return $request->createDtoFromResponse($response); + } + + public function stop(string $cacheId): Cache + { + $request = new StopCacheRequest($cacheId); + $response = $this->send($request); + + return $request->createDtoFromResponse($response); + } + public function delete(string $cacheId): void { $this->send(new DeleteCacheRequest($cacheId)); diff --git a/app/Commands/CacheStart.php b/app/Commands/CacheStart.php new file mode 100644 index 00000000..9f42b655 --- /dev/null +++ b/app/Commands/CacheStart.php @@ -0,0 +1,35 @@ +ensureClient(); + + intro('Starting Cache'); + + $cache = $this->resolvers()->cache()->from($this->argument('cache')); + + $started = spin( + fn () => $this->client->caches()->start($cache->id), + 'Starting cache...', + ); + + $this->outputJsonIfWanted($started); + + success("Cache '{$started->name}' started."); + } +} diff --git a/app/Commands/CacheStop.php b/app/Commands/CacheStop.php new file mode 100644 index 00000000..ce69d420 --- /dev/null +++ b/app/Commands/CacheStop.php @@ -0,0 +1,39 @@ +ensureClient(); + + intro('Stopping Cache'); + + $cache = $this->resolvers()->cache()->from($this->argument('cache')); + + $this->confirmDestructive("Stop cache '{$cache->name}'? Environments cannot connect to it until it is started again."); + + $stopped = spin( + fn () => $this->client->caches()->stop($cache->id), + 'Stopping cache...', + ); + + $this->outputJsonIfWanted($stopped); + + success("Cache '{$stopped->name}' stopped."); + } +} diff --git a/tests/Feature/CacheStartStopTest.php b/tests/Feature/CacheStartStopTest.php new file mode 100644 index 00000000..39abeb63 --- /dev/null +++ b/tests/Feature/CacheStartStopTest.php @@ -0,0 +1,88 @@ +shouldReceive('apiTokens')->andReturn(collect(['test-api-token'])); + $this->app->instance(ConfigRepository::class, $config); +}); + +afterEach(function () { + MockClient::destroyGlobal(); +}); + +function setupCacheStartStopMocks(): void +{ + MockClient::global([ + GetCacheRequest::class => MockResponse::make(['data' => cacheResponse(['attributes' => ['name' => 'sessions']])], 200), + StartCacheRequest::class => MockResponse::make(['data' => cacheResponse(['attributes' => ['name' => 'sessions', 'status' => 'available']])], 200), + StopCacheRequest::class => MockResponse::make(['data' => cacheResponse(['attributes' => ['name' => 'sessions', 'status' => 'stopped']])], 200), + ]); +} + +it('starts a cache', function () { + setupCacheStartStopMocks(); + + $this->artisan('cache:start', ['cache' => 'cache-1', '--no-interaction' => true]) + ->assertSuccessful(); + + MockClient::global()->assertSent(fn ($request) => $request instanceof StartCacheRequest + && $request->getMethod()->value === 'POST' + && $request->resolveEndpoint() === '/caches/cache-1/start'); + MockClient::global()->assertNotSent(StopCacheRequest::class); +}); + +it('outputs the started cache as json', function () { + setupCacheStartStopMocks(); + + Artisan::call('cache:start', ['cache' => 'cache-1', '--json' => true]); + + $output = json_decode(Artisan::output(), true); + + expect($output)->toMatchArray([ + 'id' => 'cache-1', + 'name' => 'sessions', + 'status' => 'available', + ]); +}); + +it('stops a cache with --force', function () { + setupCacheStartStopMocks(); + + $this->artisan('cache:stop', ['cache' => 'cache-1', '--force' => true, '--no-interaction' => true]) + ->assertSuccessful(); + + MockClient::global()->assertSent(fn ($request) => $request instanceof StopCacheRequest + && $request->getMethod()->value === 'POST' + && $request->resolveEndpoint() === '/caches/cache-1/stop'); +}); + +it('outputs the stopped cache as json', function () { + setupCacheStartStopMocks(); + + Artisan::call('cache:stop', ['cache' => 'cache-1', '--force' => true, '--json' => true]); + + $output = json_decode(Artisan::output(), true); + + expect($output)->toMatchArray([ + 'id' => 'cache-1', + 'name' => 'sessions', + 'status' => 'stopped', + ]); +}); + +it('requires --force to stop a cache non-interactively', function () { + setupCacheStartStopMocks(); + + $this->artisan('cache:stop', ['cache' => 'cache-1', '--no-interaction' => true]) + ->assertFailed(); + + MockClient::global()->assertNotSent(StopCacheRequest::class); +}); From 1b13b456a6571bcbcc7ae55a82c89204cd7a13e4 Mon Sep 17 00:00:00 2001 From: Claude Date: Wed, 25 Nov 2026 11:19:47 +0000 Subject: [PATCH 2/2] Keep the cache:stop confirmation to one short line A long label overflows the confirm box on narrower terminals. The README already explains what stopping does. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01T1Sq4Y2hkEm7aPHvpU9eP2 --- app/Commands/CacheStop.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/Commands/CacheStop.php b/app/Commands/CacheStop.php index ce69d420..6a48ef62 100644 --- a/app/Commands/CacheStop.php +++ b/app/Commands/CacheStop.php @@ -25,7 +25,7 @@ public function handle() $cache = $this->resolvers()->cache()->from($this->argument('cache')); - $this->confirmDestructive("Stop cache '{$cache->name}'? Environments cannot connect to it until it is started again."); + $this->confirmDestructive("Stop cache '{$cache->name}'?"); $stopped = spin( fn () => $this->client->caches()->stop($cache->id),