Skip to content

Commit 593a9a7

Browse files
author
Lasim
committed
feat(satellite): implement server removal handling for active and dormant states
1 parent be24451 commit 593a9a7

File tree

2 files changed

+85
-1
lines changed

2 files changed

+85
-1
lines changed

services/satellite/src/process/manager.ts

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -446,6 +446,63 @@ export class ProcessManager extends EventEmitter {
446446
return this.runtimeState.getAllDormantProcessNames();
447447
}
448448

449+
/**
450+
* Remove a server completely (handles both active and dormant states)
451+
* This is the method to call when a server is being uninstalled
452+
* Returns info about what was removed
453+
*/
454+
async removeServerCompletely(
455+
installationName: string,
456+
timeout: number = 10000
457+
): Promise<{ active: boolean; dormant: boolean }> {
458+
this.logger.info({ operation: 'remove_server_completely_start',
459+
installation_name: installationName
460+
}, `Removing server completely: ${installationName}`);
461+
462+
const result = { active: false, dormant: false };
463+
464+
// Check if active process exists and terminate it
465+
const processInfo = this.getProcessByName(installationName);
466+
if (processInfo) {
467+
this.logger.info({
468+
operation: 'remove_server_terminating_active',
469+
installation_name: installationName,
470+
process_id: processInfo.id,
471+
status: processInfo.status
472+
}, `Terminating active process: ${installationName}`);
473+
474+
await this.terminateProcess(processInfo, timeout);
475+
result.active = true;
476+
}
477+
478+
// Check if dormant config exists and remove it
479+
if (this.runtimeState) {
480+
const dormantConfig = this.runtimeState.getDormantConfig(installationName);
481+
if (dormantConfig) {
482+
this.logger.info({
483+
operation: 'remove_server_clearing_dormant',
484+
installation_name: installationName,
485+
team_id: dormantConfig.team_id
486+
}, `Clearing dormant config: ${installationName}`);
487+
488+
this.runtimeState.removeDormantConfig(installationName);
489+
result.dormant = true;
490+
}
491+
}
492+
493+
// Clean up restart attempts tracking
494+
this.restartAttempts.delete(installationName);
495+
496+
this.logger.info({
497+
operation: 'remove_server_completely_success',
498+
installation_name: installationName,
499+
removed_active: result.active,
500+
removed_dormant: result.dormant
501+
}, `Server removed completely: ${installationName} (active: ${result.active}, dormant: ${result.dormant})`);
502+
503+
return result;
504+
}
505+
449506
/**
450507
* Get or respawn a process if it's dormant
451508
* This method checks active processes first, then dormant configs, and respawns if needed

services/satellite/src/process/runtime-state.ts

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ export class RuntimeState extends EventEmitter {
7474

7575
/**
7676
* Remove a process from runtime state
77+
* Also cleans up dormant config if process was in dormant state
7778
*/
7879
removeProcess(processId: string): RuntimeProcessInfo | null {
7980
const processInfo = this.processes.get(processId);
@@ -93,6 +94,9 @@ export class RuntimeState extends EventEmitter {
9394
}
9495
}
9596

97+
// BUGFIX: Clean up dormant config if exists
98+
this.dormantProcessConfigs.delete(processInfo.installationName);
99+
96100
this.emit('processRemoved', processInfo);
97101
return processInfo;
98102
}
@@ -331,12 +335,35 @@ export class RuntimeState extends EventEmitter {
331335
}
332336

333337
/**
334-
* Remove dormant process config (after respawning)
338+
* Remove dormant process config (after respawning or uninstall)
335339
*/
336340
removeDormantConfig(installationName: string): void {
337341
this.dormantProcessConfigs.delete(installationName);
338342
}
339343

344+
/**
345+
* Remove a server completely by installation name (handles both active and dormant)
346+
* This is the method to call when a server is being uninstalled
347+
*/
348+
removeServerByName(installationName: string): { active: boolean; dormant: boolean } {
349+
const result = { active: false, dormant: false };
350+
351+
// Check if active process exists
352+
const processId = this.processesByName.get(installationName);
353+
if (processId) {
354+
this.removeProcess(processId);
355+
result.active = true;
356+
}
357+
358+
// Check if dormant config exists
359+
if (this.dormantProcessConfigs.has(installationName)) {
360+
this.dormantProcessConfigs.delete(installationName);
361+
result.dormant = true;
362+
}
363+
364+
return result;
365+
}
366+
340367
/**
341368
* Get count of dormant processes
342369
*/

0 commit comments

Comments
 (0)