bugsnag-js icon indicating copy to clipboard operation
bugsnag-js copied to clipboard

Request Drain Hook

Open gtowne opened this issue 3 years ago • 2 comments

Description

In our Express application on shutdown we want to make sure that in-flight requests to Bugsnag have drained before we exit.

Describe the solution you'd like It would be helpful if the SDK provided a method that we could call on shutdown where we could pass a callback to be called when outstanding requests have drained.

Example:

Bugsnag.stop(() => {
    // in-flight requests have now drained
})

Describe alternatives you've considered Our current workaround is to add an arbitrary delay on shutdown, but this negatively impacts restart times.

gtowne avatar Sep 13 '22 21:09 gtowne

Hi @gtowne - thanks for raising this. Please could you tell us a bit more about your use case and what your main concerns are that mean you'd want to make sure in-flight requests are completed before your app exits? By default for unhandled exceptions Bugsnag's Node.js notifier does keep the process alive for long enough to send the error report before logging the error and exiting with a non-zero status code, which is in line with the default Node.js behaviour.

If you e.g. had an issue with network connectivity that meant requests to Bugsnag weren't being delivered immediately - do you envision that you'd keep your app alive for long enough that these could be sent?

luke-belton avatar Sep 14 '22 09:09 luke-belton

Thanks for the quick reply, @luke-belton !

In our application there are some code paths on which we want to forcibly kill the process with process.exit(1) to allow it to more quickly be replaced. Before we do that, we want to make sure that the error that caused us to need to shutdown is fully reported to Bugsnag.

I don't anticipate issues with intermittent network connectivity like you describe will be an immediate need for us. When we get into one of these forcible shutdown scenarios I don't think we'd ever want to keep the process alive for more than a few seconds to report an error.

gtowne avatar Sep 14 '22 13:09 gtowne

Hi @gtowne - apologies for the slow response on this one. Rather than using process.exit(1), could you instead use server.close() to prevent your server from accepting any new connections; this would give Bugsnag (and anything else in-progress) the opportunity to finish any in-flight requests before the process exits? For example something like:

app.get("/your_route", (req, res) => {

    //... your app code
	
	var healthy = checkIfServerHealthy()

	// decide to terminate the process
	if (!healthy) {
		// start a new event request to notify.bugsnag.com
		req.bugsnag.notify(new Error("Something went wrong - terminating"));

    	res.sendStatus(503);

		server.close();
	}
});

var server = app.listen(1234, () => {
  console.log("App listening at http://localhost:1234");
});

luke-belton avatar Oct 04 '22 14:10 luke-belton

Hi @gtowne.

How did you get on with the suggestion above? Does this give you a suitable way forward? I'm going to close out the issue for now as we believe this should be the recommended solution. Happy to reopen if necessary.

johnkiely1 avatar Oct 24 '22 10:10 johnkiely1