fixed server connection status bug

This commit is contained in:
Jan Prochazka
2021-01-23 17:08:17 +01:00
parent e2ea2809b6
commit c9fefd14fd

View File

@@ -2,6 +2,8 @@ const connections = require('./connections');
const socket = require('../utility/socket'); const socket = require('../utility/socket');
const { fork } = require('child_process'); const { fork } = require('child_process');
const _ = require('lodash'); const _ = require('lodash');
const AsyncLock = require('async-lock');
const lock = new AsyncLock();
module.exports = { module.exports = {
opened: [], opened: [],
@@ -22,34 +24,37 @@ module.exports = {
handle_ping() {}, handle_ping() {},
async ensureOpened(conid) { async ensureOpened(conid) {
const existing = this.opened.find(x => x.conid == conid); const res = await lock.acquire(conid, async () => {
if (existing) return existing; const existing = this.opened.find(x => x.conid == conid);
const connection = await connections.get({ conid }); if (existing) return existing;
const subprocess = fork(process.argv[1], ['serverConnectionProcess', ...process.argv.slice(3)]); const connection = await connections.get({ conid });
const newOpened = { const subprocess = fork(process.argv[1], ['serverConnectionProcess', ...process.argv.slice(3)]);
conid, const newOpened = {
subprocess, conid,
databases: [], subprocess,
connection, databases: [],
status: { connection,
name: 'pending', status: {
}, name: 'pending',
disconnected: false, },
}; disconnected: false,
this.opened.push(newOpened); };
delete this.closed[conid]; this.opened.push(newOpened);
socket.emitChanged(`server-status-changed`); delete this.closed[conid];
// @ts-ignore socket.emitChanged(`server-status-changed`);
subprocess.on('message', ({ msgtype, ...message }) => { // @ts-ignore
if (newOpened.disconnected) return; subprocess.on('message', ({ msgtype, ...message }) => {
this[`handle_${msgtype}`](conid, message); if (newOpened.disconnected) return;
this[`handle_${msgtype}`](conid, message);
});
subprocess.on('exit', () => {
if (newOpened.disconnected) return;
this.close(conid, false);
});
subprocess.send({ msgtype: 'connect', ...connection });
return newOpened;
}); });
subprocess.on('exit', () => { return res;
if (newOpened.disconnected) return;
this.close(conid, false);
});
subprocess.send({ msgtype: 'connect', ...connection });
return newOpened;
}, },
close(conid, kill = true) { close(conid, kill = true) {
@@ -82,10 +87,12 @@ module.exports = {
ping_meta: 'post', ping_meta: 'post',
async ping({ connections }) { async ping({ connections }) {
for (const conid of connections) { await Promise.all(
const opened = await this.ensureOpened(conid); connections.map(async conid => {
opened.subprocess.send({ msgtype: 'ping' }); const opened = await this.ensureOpened(conid);
} opened.subprocess.send({ msgtype: 'ping' });
})
);
return { status: 'ok' }; return { status: 'ok' };
}, },