mirror of
https://github.com/pockethost/pockethost.git
synced 2025-07-03 11:22:29 +00:00
enh: cleanupManager async cleanups and singleton shutdown
This commit is contained in:
parent
f5f19d3f78
commit
4f43f9fa54
@ -1,20 +1,49 @@
|
||||
import { map } from '@s-libs/micro-dash'
|
||||
import { reduce, values } from '@s-libs/micro-dash'
|
||||
import { nanoid } from 'nanoid'
|
||||
import { logger } from './Logger'
|
||||
|
||||
export type CleanupFunc = () => any
|
||||
export const createCleanupManager = () => {
|
||||
export type CleanupFunc = () => Promise<void> | void
|
||||
type CleanupRec = {
|
||||
cleanup: CleanupFunc
|
||||
priority: number
|
||||
}
|
||||
export const CLEANUP_DEFAULT_PRIORITY = 10
|
||||
|
||||
export const createCleanupManager = (slug?: string) => {
|
||||
const _slug = slug || nanoid()
|
||||
const { error, warn, dbg } = logger().create(`cleanupManager:${_slug}`)
|
||||
let i = 0
|
||||
const cleanups: any = {}
|
||||
const add = (cb: CleanupFunc) => {
|
||||
const cleanups: { [_: number]: CleanupRec } = {}
|
||||
const add = (cb: CleanupFunc, priority = CLEANUP_DEFAULT_PRIORITY) => {
|
||||
const idx = i++
|
||||
const cleanup = async () => {
|
||||
await cb()
|
||||
delete cleanups[idx]
|
||||
}
|
||||
cleanups[idx] = cleanup
|
||||
cleanups[idx] = { cleanup, priority }
|
||||
return cleanup
|
||||
}
|
||||
|
||||
const shutdown = () => Promise.all(map(cleanups, (v) => v()))
|
||||
let _shutdownP: Promise<void> | undefined = undefined
|
||||
const shutdown: () => Promise<void> = () => {
|
||||
if (_shutdownP) return _shutdownP
|
||||
const _cleanupFuncs = values(cleanups)
|
||||
.sort((a, b) => a.priority - b.priority)
|
||||
.map((v) => v.cleanup)
|
||||
_shutdownP = reduce(
|
||||
_cleanupFuncs,
|
||||
(c, v) => {
|
||||
return c.then(() => v())
|
||||
},
|
||||
Promise.resolve()
|
||||
).catch((e) => {
|
||||
error(
|
||||
`Cleanup functions are failing. This should never happen, check all cleanup functions to make sure they are trapping their exceptions.`
|
||||
)
|
||||
throw e
|
||||
})
|
||||
return _shutdownP
|
||||
}
|
||||
|
||||
return { add, shutdown }
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user