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 @@ -316,6 +316,8 @@ Without an environment argument, `resend:attach` offers only environments withou
| `cloud websocket-cluster:create` | Create a WebSocket cluster |
| `cloud websocket-cluster:update` | Update a WebSocket cluster |
| `cloud websocket-cluster:delete` | Delete a WebSocket cluster |
| `cloud websocket-cluster:start` | Start a stopped cluster |
| `cloud websocket-cluster:stop` | Stop a WebSocket cluster |
| `cloud websocket-cluster:metrics` | View cluster metrics |
| `cloud websocket-cluster:types` | List cluster types and tiers |
| `cloud websocket-application:list` | List WebSocket applications |
Expand All @@ -325,6 +327,8 @@ Without an environment argument, `resend:attach` offers only environments withou
| `cloud websocket-application:delete` | Delete a WebSocket application |
| `cloud websocket-application:metrics` | View application metrics |

A stopped WebSocket cluster's applications stop accepting connections until it is started again, so `websocket-cluster:stop` asks for confirmation (or `--force`).

### Background processes

| 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 @@ -147,6 +147,8 @@ $data = $response->json();
- `CreateWebSocketClusterRequest` - Create a new cluster
- `UpdateWebSocketClusterRequest` - Update a cluster
- `DeleteWebSocketClusterRequest` - Delete a cluster
- `StartWebSocketClusterRequest` - Start a stopped cluster
- `StopWebSocketClusterRequest` - Stop a cluster
- `ListWebSocketClusterTypesRequest` - List cluster types with their regions and max connection tiers

#### WebSocket Applications
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace App\Client\Resources\WebSocketClusters;

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

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

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

public function resolveEndpoint(): string
{
return "/websocket-servers/{$this->clusterId}/start";
}

public function createDtoFromResponse(Response $response): WebsocketCluster
{
return WebsocketCluster::createFromResponse($response->json());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace App\Client\Resources\WebSocketClusters;

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

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

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

public function resolveEndpoint(): string
{
return "/websocket-servers/{$this->clusterId}/stop";
}

public function createDtoFromResponse(Response $response): WebsocketCluster
{
return WebsocketCluster::createFromResponse($response->json());
}
}
18 changes: 18 additions & 0 deletions app/Client/Resources/WebSocketClustersResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
use App\Client\Resources\WebSocketClusters\GetWebSocketClusterRequest;
use App\Client\Resources\WebSocketClusters\ListWebSocketClustersRequest;
use App\Client\Resources\WebSocketClusters\ListWebSocketClusterTypesRequest;
use App\Client\Resources\WebSocketClusters\StartWebSocketClusterRequest;
use App\Client\Resources\WebSocketClusters\StopWebSocketClusterRequest;
use App\Client\Resources\WebSocketClusters\UpdateWebSocketClusterRequest;
use App\Dto\WebsocketCluster;
use App\Dto\WebsocketClusterType;
Expand Down Expand Up @@ -57,6 +59,22 @@ public function update(UpdateWebSocketClusterRequestData $data): WebsocketCluste
return $request->createDtoFromResponse($response);
}

public function start(string $clusterId): WebsocketCluster
{
$request = new StartWebSocketClusterRequest($clusterId);
$response = $this->send($request);

return $request->createDtoFromResponse($response);
}

public function stop(string $clusterId): WebsocketCluster
{
$request = new StopWebSocketClusterRequest($clusterId);
$response = $this->send($request);

return $request->createDtoFromResponse($response);
}

public function delete(string $clusterId): void
{
$this->send(new DeleteWebSocketClusterRequest($clusterId));
Expand Down
37 changes: 37 additions & 0 deletions app/Commands/WebsocketClusterStart.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace App\Commands;

use App\Dto\WebsocketCluster;

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

class WebsocketClusterStart extends BaseCommand
{
protected ?string $jsonDataClass = WebsocketCluster::class;

protected $signature = 'websocket-cluster:start {cluster? : The cluster ID or name}';

protected $description = 'Start a stopped WebSocket cluster';

protected $aliases = ['ws-cluster:start'];

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

intro('Starting WebSocket Cluster');

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

$started = spin(
fn () => $this->client->websocketClusters()->start($cluster->id),
'Starting WebSocket cluster...',
);

$this->outputJsonIfWanted($started);

success("WebSocket cluster '{$started->name}' started.");
}
}
41 changes: 41 additions & 0 deletions app/Commands/WebsocketClusterStop.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace App\Commands;

use App\Dto\WebsocketCluster;

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

class WebsocketClusterStop extends BaseCommand
{
protected ?string $jsonDataClass = WebsocketCluster::class;

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

protected $description = 'Stop a WebSocket cluster';

protected $aliases = ['ws-cluster:stop'];

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

intro('Stopping WebSocket Cluster');

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

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

$stopped = spin(
fn () => $this->client->websocketClusters()->stop($cluster->id),
'Stopping WebSocket cluster...',
);

$this->outputJsonIfWanted($stopped);

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

use App\Client\Resources\WebSocketClusters\GetWebSocketClusterRequest;
use App\Client\Resources\WebSocketClusters\StartWebSocketClusterRequest;
use App\Client\Resources\WebSocketClusters\StopWebSocketClusterRequest;
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 setupWebsocketClusterStartStopMocks(): void
{
$cluster = fn (string $status) => ['data' => websocketClusterResponse([
'id' => 'ws-1',
'attributes' => ['name' => 'storefront-ws', 'status' => $status],
])];

MockClient::global([
GetWebSocketClusterRequest::class => MockResponse::make($cluster('available'), 200),
StartWebSocketClusterRequest::class => MockResponse::make($cluster('available'), 200),
StopWebSocketClusterRequest::class => MockResponse::make($cluster('stopped'), 200),
]);
}

it('starts a websocket cluster', function () {
setupWebsocketClusterStartStopMocks();

$this->artisan('websocket-cluster:start', ['cluster' => 'ws-1', '--no-interaction' => true])
->assertSuccessful();

MockClient::global()->assertSent(fn ($request) => $request instanceof StartWebSocketClusterRequest
&& $request->getMethod()->value === 'POST'
&& $request->resolveEndpoint() === '/websocket-servers/ws-1/start');
MockClient::global()->assertNotSent(StopWebSocketClusterRequest::class);
});

it('outputs the started websocket cluster as json', function () {
setupWebsocketClusterStartStopMocks();

Artisan::call('websocket-cluster:start', ['cluster' => 'ws-1', '--json' => true]);

$output = json_decode(Artisan::output(), true);

expect($output)->toMatchArray([
'id' => 'ws-1',
'name' => 'storefront-ws',
'status' => 'available',
]);
});

it('stops a websocket cluster with --force', function () {
setupWebsocketClusterStartStopMocks();

$this->artisan('websocket-cluster:stop', ['cluster' => 'ws-1', '--force' => true, '--no-interaction' => true])
->assertSuccessful();

MockClient::global()->assertSent(fn ($request) => $request instanceof StopWebSocketClusterRequest
&& $request->getMethod()->value === 'POST'
&& $request->resolveEndpoint() === '/websocket-servers/ws-1/stop');
});

it('outputs the stopped websocket cluster as json', function () {
setupWebsocketClusterStartStopMocks();

Artisan::call('websocket-cluster:stop', ['cluster' => 'ws-1', '--force' => true, '--json' => true]);

$output = json_decode(Artisan::output(), true);

expect($output)->toMatchArray([
'id' => 'ws-1',
'name' => 'storefront-ws',
'status' => 'stopped',
]);
});

it('requires --force to stop a websocket cluster non-interactively', function () {
setupWebsocketClusterStartStopMocks();

$this->artisan('websocket-cluster:stop', ['cluster' => 'ws-1', '--no-interaction' => true])
->assertFailed();

MockClient::global()->assertNotSent(StopWebSocketClusterRequest::class);
});