-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
ExitHooks.ts
82 lines (69 loc) · 2.39 KB
/
ExitHooks.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import Debug from '@prisma/debug'
import os from 'os'
export type BeforeExitListener = () => Promise<void> | void
const debug = Debug('prisma:client:libraryEngine:exitHooks')
export class ExitHooks {
private nextOwnerId = 1
private ownerToIdMap = new WeakMap<object, number>()
private idToListenerMap = new Map<number, BeforeExitListener>()
private areHooksInstalled = false
install() {
if (this.areHooksInstalled) {
return
}
this.installExitEventHook('beforeExit')
this.installExitEventHook('exit')
this.installExitSignalHook('SIGINT')
this.installExitSignalHook('SIGUSR2')
this.installExitSignalHook('SIGTERM')
this.areHooksInstalled = true
}
setListener(owner: object, listener: BeforeExitListener | undefined) {
if (listener) {
let id = this.ownerToIdMap.get(owner)
if (!id) {
id = this.nextOwnerId++
this.ownerToIdMap.set(owner, id)
}
this.idToListenerMap.set(id, listener)
} else {
const id = this.ownerToIdMap.get(owner)
if (id !== undefined) {
this.ownerToIdMap.delete(owner)
this.idToListenerMap.delete(id)
}
}
}
getListener(owner: object): BeforeExitListener | undefined {
const id = this.ownerToIdMap.get(owner)
if (id === undefined) {
return undefined
}
return this.idToListenerMap.get(id)
}
private installExitEventHook(event: 'beforeExit' | 'exit') {
// Note: TypeScript isn't able to narrow type arguments on overloaded functions.
process.once(event as any, this.exitLikeHook)
}
private installExitSignalHook(signal: NodeJS.Signals) {
process.once(signal, async (signal) => {
await this.exitLikeHook(signal)
const isSomeoneStillListening = process.listenerCount(signal) > 0
// Only exit when there are no listeners left for this signal.
// If there is another listener, that other listener is responsible for exiting.
if (isSomeoneStillListening) {
return
}
// the usual way to exit with a signal is to add 128 to the signal number
const exitCode = os.constants.signals[signal] + 128
process.exit(exitCode)
})
}
private exitLikeHook = async (event: 'beforeExit' | 'exit' | NodeJS.Signals) => {
debug(`exit event received: ${event}`)
for (const listener of this.idToListenerMap.values()) {
await listener()
}
this.idToListenerMap.clear()
}
}