Skip to content

Commit

Permalink
Use once with process 'beforeExit'. (#453)
Browse files Browse the repository at this point in the history
* Use `once` with process 'beforeExit'.

This is because when we call `PostgresStore.destroy`, we put a new
task on the event loop. This "revives" the process and so
the callback for process 'beforeExit' will be called repeatedly.

Signed-off-by: Gnuxie [email protected]

* And use once in the beforeExit hook for the postgres-helper too.

Signed-off-by: Gnuxie [email protected]
  • Loading branch information
Gnuxie authored Dec 1, 2022
1 parent 773c283 commit 61d81e1
Show file tree
Hide file tree
Showing 3 changed files with 3 additions and 2 deletions.
1 change: 1 addition & 0 deletions changelog.d/453.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
The PostgresStore had a beforeExit hook that would itself prevent the process from exiting.
2 changes: 1 addition & 1 deletion spec/helpers/postgres-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export function isPostgresTestingEnabled() {
export function initPostgres() {
// Setup postgres for the whole process.
pgClient = postgres(`${process.env.BRIDGE_TEST_PGURL}/postgres`);
process.on("beforeExit", async () => {
process.once("beforeExit", async () => {
pgClient.end();
})
}
Expand Down
2 changes: 1 addition & 1 deletion src/components/stores/postgres-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export abstract class PostgresStore {
constructor(private readonly schemas: SchemaUpdateFunction[], private readonly opts: PostgresStoreOpts) {
opts.autocreateSchemaTable = opts.autocreateSchemaTable ?? true;
this.sql = opts.url ? postgres(opts.url, opts) : postgres(opts);
process.on("beforeExit", () => {
process.once("beforeExit", () => {
// Ensure we clean up on exit
this.destroy().catch(ex => {
log.warn('Failed to cleanly exit', ex);
Expand Down

0 comments on commit 61d81e1

Please sign in to comment.