Skip to content
This repository was archived by the owner on Nov 30, 2026. It is now read-only.
Closed
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
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 |
Expand Down
2 changes: 2 additions & 0 deletions app/Client/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
29 changes: 29 additions & 0 deletions app/Client/Resources/Caches/StartCacheRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace App\Client\Resources\Caches;

use App\Dto\Cache;
use Saloon\Enums\Method;
use Saloon\Http\Request;
use Saloon\Http\Response;

class StartCacheRequest extends Request
{
protected Method $method = Method::POST;

public function __construct(
protected string $cacheId,
) {
//
}

public function resolveEndpoint(): string
{
return "/caches/{$this->cacheId}/start";
}

public function createDtoFromResponse(Response $response): Cache
{
return Cache::createFromResponse($response->json());
}
}
29 changes: 29 additions & 0 deletions app/Client/Resources/Caches/StopCacheRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace App\Client\Resources\Caches;

use App\Dto\Cache;
use Saloon\Enums\Method;
use Saloon\Http\Request;
use Saloon\Http\Response;

class StopCacheRequest extends Request
{
protected Method $method = Method::POST;

public function __construct(
protected string $cacheId,
) {
//
}

public function resolveEndpoint(): string
{
return "/caches/{$this->cacheId}/stop";
}

public function createDtoFromResponse(Response $response): Cache
{
return Cache::createFromResponse($response->json());
}
}
18 changes: 18 additions & 0 deletions app/Client/Resources/CachesResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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));
Expand Down
35 changes: 35 additions & 0 deletions app/Commands/CacheStart.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace App\Commands;

use App\Dto\Cache;

use function Laravel\Prompts\intro;
use function Laravel\Prompts\spin;

class CacheStart extends BaseCommand
{
protected ?string $jsonDataClass = Cache::class;

protected $signature = 'cache:start {cache? : The cache ID or name}';

protected $description = 'Start a stopped cache';

public function handle()
{
$this->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.");
}
}
39 changes: 39 additions & 0 deletions app/Commands/CacheStop.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace App\Commands;

use App\Dto\Cache;

use function Laravel\Prompts\intro;
use function Laravel\Prompts\spin;

class CacheStop extends BaseCommand
{
protected ?string $jsonDataClass = Cache::class;

protected $signature = 'cache:stop
{cache? : The cache ID or name}
{--force : Skip confirmation}';

protected $description = 'Stop a cache';

public function handle()
{
$this->ensureClient();

intro('Stopping Cache');

$cache = $this->resolvers()->cache()->from($this->argument('cache'));

$this->confirmDestructive("Stop cache '{$cache->name}'?");

$stopped = spin(
fn () => $this->client->caches()->stop($cache->id),
'Stopping cache...',
);

$this->outputJsonIfWanted($stopped);

success("Cache '{$stopped->name}' stopped.");
}
}
88 changes: 88 additions & 0 deletions tests/Feature/CacheStartStopTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
<?php

use App\Client\Resources\Caches\GetCacheRequest;
use App\Client\Resources\Caches\StartCacheRequest;
use App\Client\Resources\Caches\StopCacheRequest;
use App\ConfigRepository;
use Illuminate\Support\Facades\Artisan;
use Saloon\Http\Faking\MockClient;
use Saloon\Http\Faking\MockResponse;

beforeEach(function () {
$config = Mockery::mock(ConfigRepository::class);
$config->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);
});