mirror of
https://github.com/jambonz/jambonz-feature-server.git
synced 2025-12-20 08:40:38 +00:00
only connect to drachtio server if connected to freeswitch (#1123)
* only connect to drachtio server if connected to freeswitch * wip * wip
This commit is contained in:
@@ -31,18 +31,26 @@ function getLocalIp() {
|
||||
return '127.0.0.1'; // Fallback to localhost if no suitable interface found
|
||||
}
|
||||
|
||||
function initMS(logger, wrapper, ms) {
|
||||
function initMS(logger, wrapper, ms, {
|
||||
onFreeswitchConnect,
|
||||
onFreeswitchDisconnect
|
||||
}) {
|
||||
Object.assign(wrapper, {ms, active: true, connects: 1});
|
||||
logger.info(`connected to freeswitch at ${ms.address}`);
|
||||
|
||||
onFreeswitchConnect(wrapper);
|
||||
|
||||
ms.conn
|
||||
.on('esl::end', () => {
|
||||
wrapper.active = false;
|
||||
wrapper.connects = 0;
|
||||
logger.info(`lost connection to freeswitch at ${ms.address}`);
|
||||
onFreeswitchDisconnect(wrapper);
|
||||
ms.removeAllListeners();
|
||||
})
|
||||
.on('esl::ready', () => {
|
||||
if (wrapper.connects > 0) {
|
||||
logger.info(`connected to freeswitch at ${ms.address}`);
|
||||
logger.info(`esl::ready connected to freeswitch at ${ms.address}`);
|
||||
}
|
||||
wrapper.connects = 1;
|
||||
wrapper.active = true;
|
||||
@@ -56,7 +64,10 @@ function initMS(logger, wrapper, ms) {
|
||||
});
|
||||
}
|
||||
|
||||
function installSrfLocals(srf, logger) {
|
||||
function installSrfLocals(srf, logger, {
|
||||
onFreeswitchConnect = () => {},
|
||||
onFreeswitchDisconnect = () => {}
|
||||
}) {
|
||||
logger.debug('installing srf locals');
|
||||
assert(!srf.locals.dbHelpers);
|
||||
const {tracer} = srf.locals.otel;
|
||||
@@ -91,7 +102,10 @@ function installSrfLocals(srf, logger) {
|
||||
mediaservers.push(val);
|
||||
try {
|
||||
const ms = await mrf.connect(fs);
|
||||
initMS(logger, val, ms);
|
||||
initMS(logger, val, ms, {
|
||||
onFreeswitchConnect,
|
||||
onFreeswitchDisconnect
|
||||
});
|
||||
}
|
||||
catch (err) {
|
||||
logger.info({err}, `failed connecting to freeswitch at ${fs.address}, will retry shortly: ${err.message}`);
|
||||
@@ -102,9 +116,15 @@ function installSrfLocals(srf, logger) {
|
||||
for (const val of mediaservers) {
|
||||
if (val.connects === 0) {
|
||||
try {
|
||||
// make sure all listeners are removed before reconnecting
|
||||
val.ms?.disconnect();
|
||||
val.ms = null;
|
||||
logger.info({mediaserver: val.opts}, 'Retrying initial connection to media server');
|
||||
const ms = await mrf.connect(val.opts);
|
||||
initMS(logger, val, ms);
|
||||
initMS(logger, val, ms, {
|
||||
onFreeswitchConnect,
|
||||
onFreeswitchDisconnect
|
||||
});
|
||||
} catch (err) {
|
||||
logger.info({err}, `failed connecting to freeswitch at ${val.opts.address}, will retry shortly`);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user