-
Notifications
You must be signed in to change notification settings - Fork 188
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
113 additions
and
156 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
export class ShutdownService { | ||
private static instance: ShutdownService; | ||
private delayedOperations: Map<NodeJS.Timeout, () => Promise<any>> = | ||
new Map(); | ||
private static readonly SHUTDOWN_TIMEOUT = 120000; // 120 seconds timeout | ||
|
||
|
||
public static getInstance(): ShutdownService { | ||
if (!ShutdownService.instance) { | ||
ShutdownService.instance = new ShutdownService(); | ||
} | ||
return ShutdownService.instance; | ||
} | ||
|
||
public addDelayedOperation( | ||
timeoutId: NodeJS.Timeout, | ||
operation: () => Promise<any> | ||
): void { | ||
this.delayedOperations.set(timeoutId, operation); | ||
} | ||
|
||
public static getTimeoutId( | ||
operation: () => Promise<any>, | ||
delayMs: number | ||
): NodeJS.Timeout { | ||
return setTimeout(() => { | ||
operation().catch((error) => { | ||
console.error("Error in delayed operation:", error); | ||
}); | ||
}, delayMs); | ||
} | ||
|
||
public async executeShutdown(): Promise<void> { | ||
console.log("Executing shutdown handlers..."); | ||
try { | ||
// Clear all timeouts and collect operations | ||
const operations = Array.from(this.delayedOperations.entries()); | ||
this.delayedOperations.clear(); | ||
|
||
for (const [timeoutId, operation] of operations) { | ||
clearTimeout(timeoutId); | ||
} | ||
|
||
// Execute delayed operations | ||
await Promise.all([ | ||
Promise.all( | ||
operations.map(([timeoutId, op]) => | ||
op() | ||
.catch((error) => { | ||
console.error("Error in delayed operation:", error); | ||
}) | ||
.finally(() => { | ||
this.removeDelayedOperation(timeoutId); | ||
}) | ||
) | ||
), | ||
new Promise((_, reject) => | ||
setTimeout( | ||
() => reject(new Error("Shutdown timed out")), | ||
ShutdownService.SHUTDOWN_TIMEOUT | ||
) | ||
), | ||
]); | ||
|
||
console.log("All shutdown handlers executed successfully."); | ||
} catch (error) { | ||
console.error("Error during shutdown:", error); | ||
console.log("Shutdown process completed with errors or timed out."); | ||
} | ||
} | ||
public removeDelayedOperation(timeoutId: NodeJS.Timeout): void { | ||
this.delayedOperations.delete(timeoutId); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
12 changes: 0 additions & 12 deletions
12
valhalla/jawn/src/managers/shutdown/ShutdownableManager.ts
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.