fix: re-establish connections
This commit is contained in:
+42
-3
@@ -13,18 +13,19 @@ let params = {
|
||||
port: config.telnet.port,
|
||||
// shellPrompt: '', // or negotiationMandatory: false
|
||||
negotiationMandatory: config.telnet.negotiationMandatory,
|
||||
timeout: config.telnet.timeout,
|
||||
// timeout: config.telnet.timeout,
|
||||
timeout: 10000,
|
||||
debug: true
|
||||
}
|
||||
|
||||
try {
|
||||
connection.connect(params);
|
||||
console.log("connection is now: ", connection);
|
||||
} catch(error) {
|
||||
// handle the throw (timeout)
|
||||
console.log("error while connect to telnet:", error);
|
||||
}
|
||||
|
||||
|
||||
function setMucData(actor, muc, data) {
|
||||
var query = "select value::jsonb as config from prosody where prosody.user=$1 and prosody.host=$2 and prosody.key=$3;"
|
||||
var queryparams = [];
|
||||
@@ -133,7 +134,7 @@ var dbpool = new Pool({
|
||||
password: config.database.pass,
|
||||
host: config.database.host,
|
||||
port: config.database.port,
|
||||
ssl: true,
|
||||
ssl: { rejectUnauthorized: false },
|
||||
max: 20, // set pool max size to 20
|
||||
min: 4, // set min pool size to 4
|
||||
idleTimeoutMillis: config.database.idleTimeoutMillis, // close idle clients after 1 second
|
||||
@@ -232,6 +233,11 @@ router.put('/groupchats/:target', async function (req, res) {
|
||||
if (has_data || has_subject) {
|
||||
if (has_subject) {
|
||||
if ( connection && connection.socket) {
|
||||
if (!connection.socket.writable) {
|
||||
await connection.connect(params).catch(error => {
|
||||
console.log("connection error: ", error);
|
||||
});
|
||||
}
|
||||
if ( connection.socket.writable === false ) {
|
||||
res.status(500).json({message: 'telnet connection is not established.'});
|
||||
} else {
|
||||
@@ -352,6 +358,12 @@ router.post('/affiliations/:target', async function (req, res) {
|
||||
res.status(500).json({message: '500 empty user'});
|
||||
} else {
|
||||
if ( connection && connection.socket) {
|
||||
// try to re-establish connection
|
||||
if (!connection.socket.writable) {
|
||||
await connection.connect(params).catch(error => {
|
||||
console.log("connection error: ", error);
|
||||
});
|
||||
}
|
||||
if ( connection.socket.writable === false ) {
|
||||
res.status(500).json({message: 'telnet connection is not established.'});
|
||||
} else {
|
||||
@@ -379,4 +391,31 @@ router.post('/affiliations/:target', async function (req, res) {
|
||||
}
|
||||
});
|
||||
|
||||
router.get('/healthcheck', async function (req, res) {
|
||||
if ( connection && connection.socket) {
|
||||
if (!connection.socket.writable) {
|
||||
await connection.connect(params).catch(error => {
|
||||
console.log("connection error: ", error);
|
||||
});
|
||||
}
|
||||
if (connection.socket.writable) {
|
||||
console.log("getting c2s connections...");
|
||||
let response = await connection.send('c2s:show();\n');
|
||||
console.log(response);
|
||||
let rawresults;
|
||||
if (response.indexOf("https://prosody.im/doc/console\r\n") > -1) {
|
||||
rawresults = response.split("https://prosody.im/doc/console\r\n")[1];
|
||||
} else {
|
||||
rawresults = response;
|
||||
}
|
||||
res.json({res: rawresults});
|
||||
} else {
|
||||
console.log("connection not writable");
|
||||
res.json({status: "ok"});
|
||||
}
|
||||
} else {
|
||||
res.json({status: "ok"});
|
||||
}
|
||||
});
|
||||
|
||||
module.exports = router;
|
||||
|
||||
Reference in New Issue
Block a user