Skip to content

Commit

Permalink
feat: stable types node and exit hooks (#18899)
Browse files Browse the repository at this point in the history
* feat: stabilise @types/node version

* feat: type-safe ExitHooks.ts

* chore: update pnpm-lock.yaml

* feat(engine-core): split event and signal exit hook into two separate hooks

* chore: re-create pnpm-lock.yaml
  • Loading branch information
jkomyno committed Apr 28, 2023
1 parent 7773098 commit ccf3df0
Show file tree
Hide file tree
Showing 19 changed files with 97 additions and 296 deletions.
2 changes: 1 addition & 1 deletion packages/debug/package.json
Expand Up @@ -15,7 +15,7 @@
"bugs": "https://github.com/prisma/prisma/issues",
"devDependencies": {
"@types/jest": "29.4.0",
"@types/node": "12.20.55",
"@types/node": "14.18.42",
"esbuild": "0.15.13",
"jest": "29.4.3",
"jest-junit": "15.0.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/engine-core/package.json
Expand Up @@ -17,7 +17,7 @@
"@swc/core": "1.3.32",
"@swc/jest": "0.2.24",
"@types/jest": "29.4.0",
"@types/node": "16.18.23",
"@types/node": "14.18.42",
"esbuild": "0.15.13",
"jest": "29.4.3",
"jest-junit": "15.0.0",
Expand Down
47 changes: 31 additions & 16 deletions packages/engine-core/src/library/ExitHooks.ts
@@ -1,4 +1,5 @@
import Debug from '@prisma/debug'
import os from 'os'

export type BeforeExitListener = () => Promise<void> | void

Expand All @@ -15,11 +16,11 @@ export class ExitHooks {
return
}

this.installHook('beforeExit')
this.installHook('exit')
this.installHook('SIGINT', true)
this.installHook('SIGUSR2', true)
this.installHook('SIGTERM', true)
this.installExitEventHook('beforeExit')
this.installExitEventHook('exit')
this.installExitSignalHook('SIGINT')
this.installExitSignalHook('SIGUSR2')
this.installExitSignalHook('SIGTERM')
this.areHooksInstalled = true
}

Expand Down Expand Up @@ -48,20 +49,34 @@ export class ExitHooks {
return this.idToListenerMap.get(id)
}

private installHook(event: string, shouldExit = false) {
process.once(event, async (code) => {
debug(`exit event received: ${event}`)
for (const listener of this.idToListenerMap.values()) {
await listener()
}
private installExitEventHook(event: 'beforeExit' | 'exit') {
// Note: TypeScript isn't able to narrow type arguments on overloaded functions.
process.once(event as any, this.exitLikeHook)
}

this.idToListenerMap.clear()
private installExitSignalHook(signal: NodeJS.Signals) {
process.once(signal, async (signal) => {
await this.exitLikeHook(signal)
const isSomeoneStillListening = process.listenerCount(signal) > 0

// only exit, if only we are listening
// if there is another listener, that other listener is responsible
if (shouldExit && process.listenerCount(event) === 0) {
process.exit(code)
// 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()
}
}
2 changes: 1 addition & 1 deletion packages/engines/package.json
Expand Up @@ -14,7 +14,7 @@
"@swc/core": "1.3.32",
"@swc/jest": "0.2.24",
"@types/jest": "29.4.0",
"@types/node": "16.18.23",
"@types/node": "14.18.42",
"execa": "5.1.1",
"jest": "29.4.3",
"typescript": "4.9.5"
Expand Down
2 changes: 1 addition & 1 deletion packages/fetch-engine/package.json
Expand Up @@ -19,7 +19,7 @@
"@swc/core": "1.3.32",
"@swc/jest": "0.2.24",
"@types/jest": "29.4.0",
"@types/node": "16.18.23",
"@types/node": "14.18.42",
"@types/node-fetch": "2.6.3",
"@types/progress": "2.0.5",
"del": "6.1.1",
Expand Down
2 changes: 1 addition & 1 deletion packages/generator-helper/package.json
Expand Up @@ -31,7 +31,7 @@
"@swc/core": "1.3.32",
"@swc/jest": "0.2.24",
"@types/jest": "29.4.0",
"@types/node": "12.20.55",
"@types/node": "14.18.42",
"esbuild": "0.15.13",
"jest": "29.4.3",
"jest-junit": "15.0.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/get-platform/package.json
Expand Up @@ -17,7 +17,7 @@
"@swc/core": "1.3.32",
"@swc/jest": "0.2.24",
"@types/jest": "29.4.0",
"@types/node": "16.18.23",
"@types/node": "14.18.42",
"jest": "29.4.3",
"jest-junit": "15.0.0",
"typescript": "4.9.5"
Expand Down
2 changes: 1 addition & 1 deletion packages/instrumentation/package.json
Expand Up @@ -14,7 +14,7 @@
"bugs": "https://github.com/prisma/prisma/issues",
"devDependencies": {
"@swc/core": "1.3.32",
"@types/node": "16.18.23",
"@types/node": "14.18.42",
"typescript": "4.9.5",
"jest": "29.4.3",
"jest-junit": "15.0.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/integration-tests/package.json
Expand Up @@ -20,7 +20,7 @@
"@swc/jest": "0.2.24",
"@types/jest": "29.4.0",
"@types/mssql": "8.1.2",
"@types/node": "12.20.55",
"@types/node": "14.18.42",
"@types/pg": "8.6.6",
"@types/sqlite3": "3.1.8",
"decimal.js": "10.4.3",
Expand Down
2 changes: 1 addition & 1 deletion packages/migrate/package.json
Expand Up @@ -23,7 +23,7 @@
"@swc/core": "1.3.32",
"@swc/jest": "0.2.24",
"@types/jest": "29.4.0",
"@types/node": "12.20.55",
"@types/node": "14.18.42",
"@types/pg": "8.6.6",
"@types/prompts": "2.4.4",
"@types/sqlite3": "3.1.8",
Expand Down
Expand Up @@ -6,6 +6,6 @@
},
"license": "MIT",
"devDependencies": {
"@types/node": "14.14.21"
"@types/node": "14.18.42"
}
}
Expand Up @@ -3,7 +3,7 @@
"version": "1.0.0",
"license": "MIT",
"devDependencies": {
"@types/node": "14.14.21"
"@types/node": "14.18.42"
},
"prisma": {
"seed": "node prisma/seed.js"
Expand Down
Expand Up @@ -6,6 +6,6 @@
},
"license": "MIT",
"devDependencies": {
"@types/node": "14.14.21"
"@types/node": "14.18.42"
}
}
Expand Up @@ -3,6 +3,6 @@
"version": "1.0.0",
"license": "MIT",
"devDependencies": {
"@types/node": "14.14.21"
"@types/node": "14.18.42"
}
}
Expand Up @@ -3,7 +3,7 @@
"version": "1.0.0",
"license": "MIT",
"devDependencies": {
"@types/node": "14.14.21"
"@types/node": "14.18.42"
},
"prisma": {
"seed": "./prisma/seed.sh"
Expand Down
Expand Up @@ -4,7 +4,7 @@
"license": "MIT",
"type": "module",
"devDependencies": {
"@types/node": "14.14.21",
"@types/node": "14.18.42",
"ts-node": "10",
"typescript": "4.7.4"
},
Expand Down
Expand Up @@ -3,7 +3,7 @@
"version": "1.0.0",
"license": "MIT",
"devDependencies": {
"@types/node": "14.14.21"
"@types/node": "14.18.42"
},
"prisma": {
"seed": "ts-node prisma/seed.ts"
Expand Down
Expand Up @@ -3,6 +3,6 @@
"version": "1.0.0",
"license": "MIT",
"devDependencies": {
"@types/node": "14.14.21"
"@types/node": "14.18.42"
}
}

0 comments on commit ccf3df0

Please sign in to comment.