Graceful shutdown
See original GitHub issueProblem
I’d like to shutdown my application gracefully and do some database work before I exit the process myself.
However, Prisma exits the process on a SIGINT event on its own. Even though I got a work-around by monkey-patching “process.exit”, the Prisma query engine still exits with the SIGINT event and I’m unable to execute queries therefore.
The Node.js process already received a SIGINT signal, therefore the Prisma query engine exited and your request can't be processed.
Suggested solution
- Make prisma stop listening to process events by setting an option
- Stop the engines manually by executing a stop method (?) just after the application is ready to exit
Alternatives
here’s the very hacky work-around that I use (though I’d appreciate something else)
const realProcessExit = process.exit;
process.exit = () => {
// Monkey-patch process.exit because Prisma wants to exit before
// we can shut down gracefully
};
import { PrismaClient } from "@prisma/client";
["EXIT", "SIGINT", "SIGUSR1", "SIGUSR2", "uncaughtException", "SIGTERM"].forEach((eventType) => {
const previousListeners = process.listeners(eventType);
process.removeAllListeners(eventType);
process.once(eventType, async (param) => {
await shutdown();
// Make prisma stop the engines after the application is ready to exit
previousListeners.forEach((l) => l(param));
if (eventType !== "EXIT") {
realProcessExit();
}
});
});
Issue Analytics
- State:
- Created 3 years ago
- Reactions:10
- Comments:15 (8 by maintainers)
Top Results From Across the Web
What is graceful shutdown and hard shutdown? - TechTarget
Graceful shutdown and hard shutdown are two opposing methods of turning off a computer. A graceful shutdown is when a computer is turned...
Read more >Graceful Shutdown - RisingStack Engineering
Graceful shutdown means that the OS (operating system) can safely shut down its processes and close all connections, however long that takes.
Read more >Health Checks and Graceful Shutdown - Express.js
Terminus is an open-source project that adds health checks and graceful shutdown to your application to eliminate the need to write boilerplate code....
Read more >Implementing Graceful Shutdown in Go | RudderStack Blog
Shutdown gracefully shuts down the server without interrupting any active connections. Nice, but how? Shutdown works by first closing all open ...
Read more >Explain Graceful shutdown in Express.js - GeeksforGeeks
Procedure of Graceful Shutdown: For this purpose, a SIGTERM (the program manager sends it ) signal is sent to the application that tells...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
All I want is to call
$disconnect
myself even in the case of a SIGINT, that way I have completely predictable behavior for my app(so the app will react to the SIGINT/process closing).I looked at the code and found that I can just returned a never resolving promise in the callback for
beforeExit
, blocking the SIGINT behavior.The opt-out and callback on exit seems to make sense though. I just discovered that the reason my NestJs app isn’t performing it’s
onApplicationShutdown
cleanups are that Prisma is killing the process before it gets there.I know this is slatted for current development, so wanted to get my thoughts in. Thanks!