Monitor for disconnection from DB.

The events listener now listens to the 'end' event from
the PostgreSQL driver and will attempt to reconnect if
we get disconnected.
This commit is contained in:
D. Berge
2020-10-06 19:22:56 +02:00
parent 739cf4b9ec
commit 2486cb3944

View File

@@ -19,14 +19,26 @@ async function notify (data) {
}
}
function reconnect () {
console.log("Reconnecting");
// No need to provide parameters, channels should already be populated.
listen();
}
async function listen (addChannels, callback) {
if (!client) {
client = await pool.connect();
client.on('notification', notify);
console.log("Client connected");
client.on('error', (err) => console.error("Events client error: ", err));
client.on('end', () => {
console.warning("Events client disconnected. Will attempt to reconnect in five seconds");
setImmediate(() => client = null);
setTimeout(reconnect, 5000);
});
}
if (!Array.isArray(addChannels)) {
if (addChannels && !Array.isArray(addChannels)) {
addChannels = [addChannels];
}