mirror of
https://gitlab.com/wgp/dougal/software.git
synced 2025-12-06 12:27:07 +00:00
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:
@@ -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];
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user