feat: allow overriding init fail handling #1342
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Previously, if a fatal error occurred that prevented building the GraphQL schema:
retryOnInitFail
was not set/falsy, the process would exit with code 34retryOnInitFail
was truthy, PostGraphile would keep retrying forever, with exponential backoffFatal errors can include things like not being able to connect to the database, and not being able to build the schema due to a naming conflict. These issues could be solved (e.g. when the database comes online, or if someone adds a smart comment to the database) so for some users this made sense. For others, particularly those running multiple versions of PostGraphile through the same node process, it made less sense.
This PR gives users a third option; they may specify
retryOnInitFail
as a callback function. The function is called with the error that occurred, and the number of attempts that have so far been made. The function is expected to return a promise which:true
to retryfalse
or reject to abortWhen returning true, the function is expected to perform exponential backoff to prevent overwhelming the database. An example function to perform exponential back-off and retry forever could be:
If the function returns false or rejects, PostGraphile will release any Postgres pool it has created (if it was passed an already created pool, it will not release that since that is not its responsibility) and any requests that come through in the interrim will be met with 503 status code and the resulting JSON
{"errors":[{"message":"Failed to initialize GraphQL schema."}]}
.The callback could be used for other purposes too, such as to unmount the middleware or shut down the server. The following example server, for example, cleanly shuts down after 10 retries if an error occurs during startup.
Fixes #1315
Fixes #1302
Fixes #1073