I'm implementing a worker process with the following requirements:
- The notification listener should retry reconnecting indefinitely. (Infinite retry attempts, no timeout)
- The worker process should respond to
SIGINT signals, and gracefully terminate.
The "reconnect" loop does not respond to any signal from calling close(), so it will attempt to reconnect indefinitely. The promise hangs around and prevents the process from closing normally. (I've even seen odd behaviour where I've killed the process using kill -15 1234, and it returns to the terminal prompt, but I still see "reconnecting" messages logged. 😳)
A rough sketch:
import createPostgresSubscriber from "pg-listen";
async function demo() {
try {
process.on("SIGTERM", () => {
console.log("Attempting graceful termination");
await subscriber.close();
console.log("Subscriber closed");
});
var subscriber = createPostgresSubscriber(
{
connectionString:
"postgresql://invalid_user:invalid_password@localhost/postgres"
},
{
retryInterval: 500,
retryTimeout: Number.POSITIVE_INFINITY
}
);
this.subscriber.events.on("reconnect", () => {
console.log(`Reconnecting...`);
});
await subscriber.connect();
await subscriber.listenTo("some_channel");
} catch (err) {
console.error(err);
}
}
demo().then(
() => console.log("Done"),
err => console.error("Error", err)
);
I'm implementing a worker process with the following requirements:
SIGINTsignals, and gracefully terminate.The "reconnect" loop does not respond to any signal from calling
close(), so it will attempt to reconnect indefinitely. The promise hangs around and prevents the process from closing normally. (I've even seen odd behaviour where I've killed the process usingkill -15 1234, and it returns to the terminal prompt, but I still see "reconnecting" messages logged. 😳)A rough sketch: