Skip to content
Merged
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
34 changes: 34 additions & 0 deletions packages/backend/src/clusters/clusters.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import { Observable, timer, exhaustMap, map, from } from 'rxjs';
import { AuthGuard } from '../auth/guards/jwt.guard.js';

import { ClustersService } from './clusters.service.js';
import { ClusterBulkActionResultZodDto } from './dto/cluster-bulk-action-result.dto.js';
import { GetAggregateStatsZodDto } from './dto/get-aggregate-stats.dto.js';
import { GetClusterLogsQueryZodDto, GetClusterLogsZodDto } from './dto/get-cluster-logs.dto.js';
import { GetClusterStatsZodDto } from './dto/get-cluster-stats.dto.js';
Expand Down Expand Up @@ -69,6 +70,39 @@ export class ClustersController {
);
}

@Post('start')
@HttpCode(HttpStatus.OK)
@ApiOkResponse({
type: ClusterBulkActionResultZodDto,
description:
'Start every cluster in parallel. Already-running clusters are no-ops. The response lists clusters that succeeded and those that failed, with the failure reason.',
})
async startAll() {
return await this.clustersService.startAll();
}

@Post('stop')
@HttpCode(HttpStatus.OK)
@ApiOkResponse({
type: ClusterBulkActionResultZodDto,
description:
'Stop every cluster in parallel. Already-stopped clusters are no-ops. The response lists clusters that succeeded and those that failed, with the failure reason.',
})
async stopAll() {
return await this.clustersService.stopAll();
}

@Post('restart')
@HttpCode(HttpStatus.OK)
@ApiOkResponse({
type: ClusterBulkActionResultZodDto,
description:
'Restart every cluster in parallel (stop + start). Stopped clusters are simply started. The response lists clusters that succeeded and those that failed, with the failure reason.',
})
async restartAll() {
return await this.clustersService.restartAll();
}

@Sse('stats/stream')
@ApiOkResponse({
description:
Expand Down
37 changes: 37 additions & 0 deletions packages/backend/src/clusters/clusters.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { BadRequestException, Injectable, NotFoundException } from '@nestjs/comm
import { DockerService } from '../docker/docker.service.js';
import { PrismaService } from '../prisma/prisma.service.js';

import { ClusterBulkActionResultDto } from './dto/cluster-bulk-action-result.dto.js';
import { GetAggregateStatsDto } from './dto/get-aggregate-stats.dto.js';

@Injectable()
Expand Down Expand Up @@ -163,6 +164,42 @@ export class ClustersService {
return await this.dockerService.getContainerStats(resource.containerId);
}

async startAll(): Promise<ClusterBulkActionResultDto> {
return await this.runBulkAction((id) => this.start(id));
}

async stopAll(): Promise<ClusterBulkActionResultDto> {
return await this.runBulkAction((id) => this.stop(id));
}

async restartAll(): Promise<ClusterBulkActionResultDto> {
return await this.runBulkAction((id) => this.restart(id));
}

private async runBulkAction(action: (id: number) => Promise<unknown>): Promise<ClusterBulkActionResultDto> {
const clusters = await this.prismaService.cluster.findMany({
select: { id: true },
orderBy: { id: 'asc' },
});

const settled = await Promise.allSettled(clusters.map((c) => action(c.id)));

const succeeded: number[] = [];
const failed: Array<{ id: number; reason: string }> = [];

settled.forEach((result, index) => {
const id = clusters[index].id;
if (result.status === 'fulfilled') {
succeeded.push(id);
} else {
const reason = result.reason instanceof Error ? result.reason.message : String(result.reason);
failed.push({ id, reason });
}
});

return { succeeded, failed };
}

async aggregateStats(): Promise<GetAggregateStatsDto> {
const clusters = await this.prismaService.cluster.findMany({
where: { status: 'RUNNING', containerId: { not: null } },
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { createZodDto } from 'nestjs-zod';
import { z } from 'zod';

export const ClusterBulkActionResultSchema = z.object({
succeeded: z.array(z.number().int().nonnegative()).meta({
description: 'IDs of clusters where the action completed successfully.',
}),
failed: z
.array(
z.object({
id: z.number().int().nonnegative(),
reason: z.string(),
}),
)
.meta({
description: 'Clusters where the action failed, along with the failure reason.',
}),
});

export class ClusterBulkActionResultZodDto extends createZodDto(ClusterBulkActionResultSchema) {}

export type ClusterBulkActionResultDto = z.infer<typeof ClusterBulkActionResultSchema>;
Loading