Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 49 additions & 52 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions packages/embedded-postgres/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
"license": "MIT",
"devDependencies": {
"@embedded-postgres/symlink-reader": "^18.4.0-beta.17",
"@types/async-exit-hook": "^2.0.0",
"@types/pg": "^8.6.5",
"eslint": "^8.56.0",
"typescript": "^4.7.3",
Expand All @@ -42,7 +41,7 @@
"@embedded-postgres/windows-x64": "^18.4.0-beta.17"
},
"dependencies": {
"async-exit-hook": "^2.0.1",
"exit-hook": "^5.1.0",
"pg": "^8.7.3"
},
"publishConfig": {
Expand Down
13 changes: 6 additions & 7 deletions packages/embedded-postgres/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { platform, tmpdir, userInfo } from 'os';
import { ChildProcess, spawn, exec, execSync } from 'child_process';

import pg from 'pg';
import AsyncExitHook from 'async-exit-hook';
import { asyncExitHook } from 'exit-hook';

import getBinaries from './binary.js';
import { PostgresOptions } from './types.js';
Expand Down Expand Up @@ -430,17 +430,16 @@ async function execAsync(command: string) {
* nicely shutdown all potentially started clusters, and we don't end up with
* zombie processes.
*/
async function gracefulShutdown(done: () => void) {
async function gracefulShutdown() {
// Loop through all instances, stop them, and await the response
await Promise.all([...instances].map((instance) => {
return instance.stop();
}));

// Let NodeJS know we're done
done();
}

// Register graceful shutdown function
AsyncExitHook(gracefulShutdown);
asyncExitHook(gracefulShutdown, {
wait: 5000
});

export default EmbeddedPostgres;
export default EmbeddedPostgres;
10 changes: 10 additions & 0 deletions packages/embedded-postgres/tests/helpers/exit-code-test.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import EmbeddedPostgres from '../../dist/index.js';

const pg = new EmbeddedPostgres({
port: 15433,
databaseDir: '/tmp/ep-exit-code-test',
persistent: false,
onLog: () => {},
});

process.exitCode = 42;
23 changes: 23 additions & 0 deletions packages/embedded-postgres/tests/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { it, expect, afterEach } from 'vitest';
import fs from 'fs/promises';
import path from 'path';
import { spawn } from 'child_process';
import EmbeddedPostgres from '../src/index.js';
import { PostgresOptions } from '../src/types.js';
import { beforeEach } from 'node:test';
Expand Down Expand Up @@ -159,4 +160,26 @@ it('should ensure binary files have correct permissions', async () => {
const afterFixStat = await fs.stat(postgres);
// Permissions should still be correct
expect((afterFixStat.mode & expectedPermissions)).toBe(expectedPermissions);
});

it('should preserve a custom process.exitCode when the process exits (gracefulShutdown does not force exit 0)', async () => {
const scriptPath = new URL('./helpers/exit-code-test.mjs', import.meta.url).pathname;

await new Promise<void>((resolve, reject) => {
const child = spawn(process.execPath, [scriptPath], {
stdio: ['ignore', 'pipe', 'pipe'],
});

child.on('exit', (code) => {
try {
expect(code).toBe(42);
} catch (e) {
reject(e);
return;
}
resolve();
});

child.on('error', reject);
});
});