fix(gateway): 배포 worktree 자동 정리 추가
Profile과 Gateway release의 현재 실행 및 rollback 경계를 보호하면서 오래된 commit worktree를 주기적으로 정리한다.
This commit is contained in:
@@ -44,7 +44,11 @@ import {
|
||||
writeProfileReleaseSource,
|
||||
type ProfileReleaseSource,
|
||||
} from './profileReleaseSource.js';
|
||||
import type { GitWorkspaceManager } from './workspaceManager.js';
|
||||
import {
|
||||
DEFAULT_MANAGED_WORKSPACE_KEEP_NEWEST,
|
||||
DEFAULT_MANAGED_WORKSPACE_RETENTION_MS,
|
||||
type GitWorkspaceManager,
|
||||
} from './workspaceManager.js';
|
||||
import type { AdminSeedUser } from './seedProfileDatabase.js';
|
||||
import { assertReleaseComponents, readReleaseManifest } from './releaseManifest.js';
|
||||
|
||||
@@ -73,6 +77,8 @@ export interface GatewayOrchestratorOptions {
|
||||
cancelGame?: typeof defaultCancelGame;
|
||||
}
|
||||
|
||||
const WORKSPACE_CLEANUP_INTERVAL_MS = 24 * 60 * 60 * 1_000;
|
||||
|
||||
export interface ProfileRuntimeState {
|
||||
frontendRunning: boolean;
|
||||
apiRunning: boolean;
|
||||
@@ -629,11 +635,13 @@ export class GatewayOrchestrator implements GatewayOrchestratorHandle {
|
||||
private scheduleTimer?: NodeJS.Timeout;
|
||||
private buildTimer?: NodeJS.Timeout;
|
||||
private adminActionTimer?: NodeJS.Timeout;
|
||||
private workspaceCleanupTimer?: NodeJS.Timeout;
|
||||
private reconcileInFlight = false;
|
||||
private scheduleInFlight = false;
|
||||
private buildInFlight = false;
|
||||
private adminActionInFlight = false;
|
||||
private operationInFlight = false;
|
||||
private workspaceCleanupInFlight = false;
|
||||
private activeOperationAbortSignal?: AbortSignal;
|
||||
private readonly resetInFlight = new Set<string>();
|
||||
private readonly operationLeaseOwner = randomUUID();
|
||||
@@ -714,7 +722,7 @@ export class GatewayOrchestrator implements GatewayOrchestratorHandle {
|
||||
start(): void {
|
||||
this.stopping = false;
|
||||
this.trackTask(this.reconcileNow());
|
||||
this.trackTask(this.runOperationsNow());
|
||||
this.trackTask(this.runOperationsNow().then(() => this.cleanupWorkspacesScheduled()));
|
||||
this.trackTask(this.runAdminActionsNow());
|
||||
this.reconcileTimer = setInterval(() => this.trackTask(this.reconcileNow()), this.reconcileIntervalMs);
|
||||
this.scheduleTimer = setInterval(() => this.trackTask(this.runScheduleNow()), this.scheduleIntervalMs);
|
||||
@@ -723,6 +731,10 @@ export class GatewayOrchestrator implements GatewayOrchestratorHandle {
|
||||
this.trackTask(this.runOperationsNow());
|
||||
this.trackTask(this.runAdminActionsNow());
|
||||
}, this.adminActionIntervalMs);
|
||||
this.workspaceCleanupTimer = setInterval(
|
||||
() => this.trackTask(this.cleanupWorkspacesScheduled()),
|
||||
WORKSPACE_CLEANUP_INTERVAL_MS
|
||||
);
|
||||
}
|
||||
|
||||
async stop(): Promise<void> {
|
||||
@@ -747,6 +759,9 @@ export class GatewayOrchestrator implements GatewayOrchestratorHandle {
|
||||
if (this.adminActionTimer) {
|
||||
clearInterval(this.adminActionTimer);
|
||||
}
|
||||
if (this.workspaceCleanupTimer) {
|
||||
clearInterval(this.workspaceCleanupTimer);
|
||||
}
|
||||
await Promise.allSettled([...this.inFlightTasks]);
|
||||
}
|
||||
|
||||
@@ -903,7 +918,7 @@ export class GatewayOrchestrator implements GatewayOrchestratorHandle {
|
||||
}
|
||||
|
||||
async runBuildQueueNow(): Promise<void> {
|
||||
if (this.stopping || this.buildInFlight) {
|
||||
if (this.stopping || this.buildInFlight || this.workspaceCleanupInFlight) {
|
||||
return;
|
||||
}
|
||||
this.buildInFlight = true;
|
||||
@@ -965,7 +980,7 @@ export class GatewayOrchestrator implements GatewayOrchestratorHandle {
|
||||
}
|
||||
|
||||
async runOperationsNow(): Promise<void> {
|
||||
if (this.stopping || this.operationInFlight || this.buildInFlight) {
|
||||
if (this.stopping || this.operationInFlight || this.buildInFlight || this.workspaceCleanupInFlight) {
|
||||
return;
|
||||
}
|
||||
this.operationInFlight = true;
|
||||
@@ -2160,83 +2175,56 @@ export class GatewayOrchestrator implements GatewayOrchestratorHandle {
|
||||
}
|
||||
|
||||
async cleanupStaleWorkspaces(): Promise<{ removed: string[]; skipped: string[] }> {
|
||||
const profiles = await this.repository.listProfiles();
|
||||
const cutoff = this.computeCutoffDate(6);
|
||||
const workspaceMap = new Map<string, { profileNames: string[]; lastUsedAt?: Date; hasActiveBuild: boolean }>();
|
||||
for (const profile of profiles) {
|
||||
const workspace = profile.buildWorkspace;
|
||||
if (!workspace) {
|
||||
continue;
|
||||
}
|
||||
const entry = workspaceMap.get(workspace) ?? {
|
||||
profileNames: [],
|
||||
lastUsedAt: undefined,
|
||||
hasActiveBuild: false,
|
||||
};
|
||||
entry.profileNames.push(profile.profileName);
|
||||
if (profile.buildLastUsedAt) {
|
||||
const usedAt = new Date(profile.buildLastUsedAt);
|
||||
if (!entry.lastUsedAt || usedAt > entry.lastUsedAt) {
|
||||
entry.lastUsedAt = usedAt;
|
||||
if (this.buildInFlight || this.operationInFlight || this.workspaceCleanupInFlight) {
|
||||
const managedWorkspaces = await this.workspaceManager.listManagedWorkspaces();
|
||||
return { removed: [], skipped: managedWorkspaces.map((workspace) => workspace.root) };
|
||||
}
|
||||
this.workspaceCleanupInFlight = true;
|
||||
try {
|
||||
const managedWorkspaces = await this.workspaceManager.listManagedWorkspaces();
|
||||
const profiles = await this.repository.listProfiles();
|
||||
const protectedWorkspaces = new Set<string>();
|
||||
for (const profile of profiles) {
|
||||
if (profile.buildWorkspace) {
|
||||
protectedWorkspaces.add(path.resolve(profile.buildWorkspace));
|
||||
}
|
||||
if (profile.buildCommitSha && (profile.buildStatus === 'RUNNING' || profile.buildStatus === 'QUEUED')) {
|
||||
protectedWorkspaces.add(
|
||||
path.resolve(this.workspaceManager.workspacePathForCommit(profile.buildCommitSha))
|
||||
);
|
||||
}
|
||||
}
|
||||
if (profile.buildStatus === 'RUNNING' || profile.buildStatus === 'QUEUED') {
|
||||
entry.hasActiveBuild = true;
|
||||
}
|
||||
workspaceMap.set(workspace, entry);
|
||||
}
|
||||
|
||||
const activeProcesses = (await this.processManager.list()).filter((process) =>
|
||||
isRuntimeProcessActive(process.status)
|
||||
);
|
||||
const referencedWorkspaces = new Set<string>();
|
||||
for (const [workspace, entry] of workspaceMap.entries()) {
|
||||
const profileProcessNames = new Set(
|
||||
entry.profileNames.flatMap((profileName) => [
|
||||
buildProcessName(profileName, 'frontend'),
|
||||
buildProcessName(profileName, 'api'),
|
||||
buildProcessName(profileName, 'daemon'),
|
||||
buildProcessName(profileName, 'auction'),
|
||||
buildProcessName(profileName, 'battle-sim'),
|
||||
buildProcessName(profileName, 'tournament'),
|
||||
])
|
||||
const activeProcesses = (await this.processManager.list()).filter((process) =>
|
||||
isRuntimeProcessActive(process.status)
|
||||
);
|
||||
if (
|
||||
activeProcesses.some(
|
||||
(process) =>
|
||||
profileProcessNames.has(process.name) ||
|
||||
isPathInside(process.cwd, workspace) ||
|
||||
isPathInside(process.script, workspace)
|
||||
)
|
||||
) {
|
||||
referencedWorkspaces.add(workspace);
|
||||
for (const workspace of managedWorkspaces) {
|
||||
if (
|
||||
activeProcesses.some(
|
||||
(process) =>
|
||||
isPathInside(process.cwd, workspace.root) || isPathInside(process.script, workspace.root)
|
||||
)
|
||||
) {
|
||||
protectedWorkspaces.add(workspace.root);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const removed: string[] = [];
|
||||
const skipped: string[] = [];
|
||||
for (const [workspace, entry] of workspaceMap.entries()) {
|
||||
if (!entry.lastUsedAt || entry.hasActiveBuild || referencedWorkspaces.has(workspace)) {
|
||||
skipped.push(workspace);
|
||||
continue;
|
||||
}
|
||||
if (entry.lastUsedAt > cutoff) {
|
||||
skipped.push(workspace);
|
||||
continue;
|
||||
}
|
||||
await this.workspaceManager.remove(workspace);
|
||||
await this.repository.clearWorkspaceUsage(entry.profileNames);
|
||||
removed.push(workspace);
|
||||
return await this.workspaceManager.cleanup({
|
||||
protectedPaths: [...protectedWorkspaces],
|
||||
retentionMs: DEFAULT_MANAGED_WORKSPACE_RETENTION_MS,
|
||||
keepNewest: DEFAULT_MANAGED_WORKSPACE_KEEP_NEWEST,
|
||||
});
|
||||
} finally {
|
||||
this.workspaceCleanupInFlight = false;
|
||||
}
|
||||
|
||||
return { removed, skipped };
|
||||
}
|
||||
|
||||
private computeCutoffDate(months: number): Date {
|
||||
const date = this.now();
|
||||
const cutoff = new Date(date);
|
||||
cutoff.setMonth(cutoff.getMonth() - months);
|
||||
return cutoff;
|
||||
private async cleanupWorkspacesScheduled(): Promise<void> {
|
||||
if (this.stopping || this.buildInFlight || this.operationInFlight || this.workspaceCleanupInFlight) return;
|
||||
const result = await this.cleanupStaleWorkspaces();
|
||||
if (result.removed.length > 0) {
|
||||
console.info(`[gateway-orchestrator] removed ${result.removed.length} stale profile worktrees`);
|
||||
}
|
||||
}
|
||||
|
||||
private async startProfile(profile: GatewayProfileRecord, assertLease?: () => Promise<void>): Promise<boolean> {
|
||||
|
||||
@@ -6,6 +6,7 @@ export interface WorkspaceManagerOptions {
|
||||
repoRoot: string;
|
||||
worktreeRoot: string;
|
||||
baseEnv?: Record<string, string>;
|
||||
now?: () => Date;
|
||||
}
|
||||
|
||||
export interface WorkspaceInfo {
|
||||
@@ -14,6 +15,26 @@ export interface WorkspaceInfo {
|
||||
needsInstall: boolean;
|
||||
}
|
||||
|
||||
export interface ManagedWorkspaceInfo {
|
||||
root: string;
|
||||
commitSha: string;
|
||||
lastUsedAt: Date;
|
||||
}
|
||||
|
||||
export interface ManagedWorkspaceCleanupOptions {
|
||||
protectedPaths?: readonly string[];
|
||||
retentionMs: number;
|
||||
keepNewest: number;
|
||||
}
|
||||
|
||||
export interface ManagedWorkspaceCleanupResult {
|
||||
removed: string[];
|
||||
skipped: string[];
|
||||
}
|
||||
|
||||
export const DEFAULT_MANAGED_WORKSPACE_RETENTION_MS = 24 * 60 * 60 * 1_000;
|
||||
export const DEFAULT_MANAGED_WORKSPACE_KEEP_NEWEST = 2;
|
||||
|
||||
const runGit = (args: string[], cwd: string, env?: Record<string, string>): Promise<{ ok: boolean; output: string }> =>
|
||||
new Promise((resolve) => {
|
||||
const child = spawn('git', args, {
|
||||
@@ -58,11 +79,13 @@ export class GitWorkspaceManager {
|
||||
private readonly repoRoot: string;
|
||||
private readonly worktreeRoot: string;
|
||||
private readonly baseEnv?: Record<string, string>;
|
||||
private readonly now: () => Date;
|
||||
|
||||
constructor(options: WorkspaceManagerOptions) {
|
||||
this.repoRoot = options.repoRoot;
|
||||
this.worktreeRoot = options.worktreeRoot;
|
||||
this.baseEnv = options.baseEnv;
|
||||
this.now = options.now ?? (() => new Date());
|
||||
}
|
||||
|
||||
async resolveCommit(sourceMode: 'BRANCH' | 'COMMIT', sourceRef: string): Promise<string> {
|
||||
@@ -124,6 +147,8 @@ export class GitWorkspaceManager {
|
||||
} else {
|
||||
await this.assertReusableWorkspace(workspacePath, commitSha);
|
||||
}
|
||||
const usedAt = this.now();
|
||||
fs.utimesSync(workspacePath, usedAt, usedAt);
|
||||
|
||||
return {
|
||||
root: workspacePath,
|
||||
@@ -138,13 +163,100 @@ export class GitWorkspaceManager {
|
||||
return false;
|
||||
}
|
||||
await this.assertRegisteredWorkspace(resolved);
|
||||
const status = await runGit(['status', '--porcelain'], resolved, this.baseEnv);
|
||||
if (!status.ok) {
|
||||
throw new Error(status.output || 'Failed to inspect managed workspace.');
|
||||
}
|
||||
if (status.output.trim()) {
|
||||
throw new Error('Managed workspace has uncommitted changes.');
|
||||
}
|
||||
const result = await runGit(['worktree', 'remove', '--force', resolved], this.repoRoot, this.baseEnv);
|
||||
if (!result.ok) {
|
||||
fs.rmSync(resolved, { recursive: true, force: true });
|
||||
throw new Error(result.output || 'Failed to remove git worktree.');
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
workspacePathForCommit(commitSha: string): string {
|
||||
if (!COMMIT_SHA_PATTERN.test(commitSha)) {
|
||||
throw new Error('Invalid commit SHA.');
|
||||
}
|
||||
return path.join(this.worktreeRoot, commitSha);
|
||||
}
|
||||
|
||||
async listManagedWorkspaces(): Promise<ManagedWorkspaceInfo[]> {
|
||||
const listed = await runGit(['worktree', 'list', '--porcelain'], this.repoRoot, this.baseEnv);
|
||||
if (!listed.ok) {
|
||||
throw new Error(listed.output || 'Failed to inspect git worktrees.');
|
||||
}
|
||||
const workspaces: ManagedWorkspaceInfo[] = [];
|
||||
for (const block of listed.output.split(/\n\n+/)) {
|
||||
const lines = block.split('\n');
|
||||
const worktreeLine = lines.find((line) => line.startsWith('worktree '));
|
||||
const headLine = lines.find((line) => line.startsWith('HEAD '));
|
||||
if (!worktreeLine || !headLine) continue;
|
||||
const workspacePath = path.resolve(worktreeLine.slice('worktree '.length));
|
||||
const commitSha = headLine.slice('HEAD '.length);
|
||||
try {
|
||||
this.assertManagedWorkspacePath(workspacePath);
|
||||
} catch {
|
||||
continue;
|
||||
}
|
||||
if (!COMMIT_SHA_PATTERN.test(commitSha) || !fs.existsSync(workspacePath)) continue;
|
||||
workspaces.push({
|
||||
root: workspacePath,
|
||||
commitSha,
|
||||
lastUsedAt: fs.statSync(workspacePath).mtime,
|
||||
});
|
||||
}
|
||||
return workspaces;
|
||||
}
|
||||
|
||||
async cleanup(options: ManagedWorkspaceCleanupOptions): Promise<ManagedWorkspaceCleanupResult> {
|
||||
if (!Number.isFinite(options.retentionMs) || options.retentionMs < 0) {
|
||||
throw new Error('Workspace retention must be a non-negative duration.');
|
||||
}
|
||||
if (!Number.isInteger(options.keepNewest) || options.keepNewest < 0) {
|
||||
throw new Error('Workspace keepNewest must be a non-negative integer.');
|
||||
}
|
||||
const protectedPaths = new Set((options.protectedPaths ?? []).map((item) => path.resolve(item)));
|
||||
const workspaces = await this.listManagedWorkspaces();
|
||||
const unprotectedNewest = [...workspaces]
|
||||
.filter((workspace) => !protectedPaths.has(workspace.root))
|
||||
.sort((left, right) => right.lastUsedAt.getTime() - left.lastUsedAt.getTime())
|
||||
.slice(0, options.keepNewest);
|
||||
const retainedNewestPaths = new Set(unprotectedNewest.map((workspace) => workspace.root));
|
||||
const cutoff = this.now().getTime() - options.retentionMs;
|
||||
const removed: string[] = [];
|
||||
const skipped: string[] = [];
|
||||
|
||||
for (const workspace of workspaces) {
|
||||
if (
|
||||
protectedPaths.has(workspace.root) ||
|
||||
retainedNewestPaths.has(workspace.root) ||
|
||||
workspace.lastUsedAt.getTime() > cutoff
|
||||
) {
|
||||
skipped.push(workspace.root);
|
||||
continue;
|
||||
}
|
||||
try {
|
||||
if (await this.remove(workspace.root)) {
|
||||
removed.push(workspace.root);
|
||||
} else {
|
||||
skipped.push(workspace.root);
|
||||
}
|
||||
} catch {
|
||||
skipped.push(workspace.root);
|
||||
}
|
||||
}
|
||||
|
||||
const pruned = await runGit(['worktree', 'prune', '--expire', 'now'], this.repoRoot, this.baseEnv);
|
||||
if (!pruned.ok) {
|
||||
throw new Error(pruned.output || 'Failed to prune git worktree metadata.');
|
||||
}
|
||||
return { removed, skipped };
|
||||
}
|
||||
|
||||
private assertManagedWorkspacePath(workspacePath: string): string {
|
||||
const resolved = path.resolve(workspacePath);
|
||||
const root = path.resolve(this.worktreeRoot);
|
||||
|
||||
Reference in New Issue
Block a user