feat(fleet): remove host — DELETE /api/fleet/hosts/:id + Fleet card action
Self-service host removal. DELETE /api/fleet/hosts/:id (server.manage, tenant-guarded): refuses while the host is 'connected' (409 — a live agent re-registers on its next heartbeat, stop it first), deletes the host's game_instances explicitly (FK is SET NULL, would otherwise orphan them; instance_stats cascade), and clears the legacy server_connections row if it was the license's last host. Fleet view: offline host cards get a Remove button with inline confirm + toast. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import { Controller, Get } from '@nestjs/common';
|
||||
import { Controller, Get, Delete, Param } from '@nestjs/common';
|
||||
import { ApiTags, ApiBearerAuth, ApiOperation } from '@nestjs/swagger';
|
||||
import { FleetService } from './fleet.service';
|
||||
import { CurrentTenant } from '../../common/decorators/current-tenant.decorator';
|
||||
@@ -16,4 +16,11 @@ export class FleetController {
|
||||
async getFleet(@CurrentTenant() licenseId: string) {
|
||||
return this.fleetService.getFleet(licenseId);
|
||||
}
|
||||
|
||||
@Delete('hosts/:id')
|
||||
@RequirePermission('server.manage')
|
||||
@ApiOperation({ summary: 'Remove a host and its instances (host must be offline)' })
|
||||
async deleteHost(@CurrentTenant() licenseId: string, @Param('id') id: string) {
|
||||
return this.fleetService.deleteHost(licenseId, id);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,9 +4,10 @@ import { FleetController } from './fleet.controller';
|
||||
import { FleetService } from './fleet.service';
|
||||
import { AgentHost } from '../../entities/agent-host.entity';
|
||||
import { GameInstance } from '../../entities/game-instance.entity';
|
||||
import { ServerConnection } from '../../entities/server-connection.entity';
|
||||
|
||||
@Module({
|
||||
imports: [TypeOrmModule.forFeature([AgentHost, GameInstance])],
|
||||
imports: [TypeOrmModule.forFeature([AgentHost, GameInstance, ServerConnection])],
|
||||
controllers: [FleetController],
|
||||
providers: [FleetService],
|
||||
exports: [FleetService],
|
||||
|
||||
@@ -1,8 +1,9 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { Injectable, NotFoundException, ConflictException } from '@nestjs/common';
|
||||
import { InjectRepository } from '@nestjs/typeorm';
|
||||
import { Repository } from 'typeorm';
|
||||
import { AgentHost } from '../../entities/agent-host.entity';
|
||||
import { GameInstance } from '../../entities/game-instance.entity';
|
||||
import { ServerConnection } from '../../entities/server-connection.entity';
|
||||
|
||||
export interface FleetInstanceDto {
|
||||
id: string;
|
||||
@@ -49,8 +50,43 @@ export class FleetService {
|
||||
private readonly hostRepo: Repository<AgentHost>,
|
||||
@InjectRepository(GameInstance)
|
||||
private readonly instanceRepo: Repository<GameInstance>,
|
||||
@InjectRepository(ServerConnection)
|
||||
private readonly connectionRepo: Repository<ServerConnection>,
|
||||
) {}
|
||||
|
||||
/**
|
||||
* Remove a host and its game instances from the fleet.
|
||||
*
|
||||
* Refuses while the host is `connected` — a live agent re-registers on its
|
||||
* next heartbeat, so the operator must stop the agent first. Deletes the
|
||||
* host's instances explicitly (the FK is SET NULL, which would otherwise
|
||||
* orphan them); instance_stats cascade. If this was the license's last host,
|
||||
* the legacy single-server connection row is cleared too so the old
|
||||
* Dashboard doesn't show a stale server.
|
||||
*/
|
||||
async deleteHost(
|
||||
licenseId: string,
|
||||
hostId: string,
|
||||
): Promise<{ deleted: true; instances_removed: number }> {
|
||||
const host = await this.hostRepo.findOne({ where: { id: hostId, license_id: licenseId } });
|
||||
if (!host) throw new NotFoundException('Host not found');
|
||||
if (host.status === 'connected') {
|
||||
throw new ConflictException(
|
||||
'Host is online — stop the agent first, or it will re-register on its next heartbeat',
|
||||
);
|
||||
}
|
||||
|
||||
const del = await this.instanceRepo.delete({ license_id: licenseId, host_id: hostId });
|
||||
await this.hostRepo.delete({ id: hostId, license_id: licenseId });
|
||||
|
||||
const remaining = await this.hostRepo.count({ where: { license_id: licenseId } });
|
||||
if (remaining === 0) {
|
||||
await this.connectionRepo.delete({ license_id: licenseId });
|
||||
}
|
||||
|
||||
return { deleted: true, instances_removed: del.affected ?? 0 };
|
||||
}
|
||||
|
||||
async getFleet(licenseId: string): Promise<FleetResponseDto> {
|
||||
const [hosts, instances] = await Promise.all([
|
||||
this.hostRepo.find({
|
||||
|
||||
Reference in New Issue
Block a user