alternate way of keeping ws up when no active tasks

This commit is contained in:
Dave Horton
2022-02-24 13:34:21 -05:00
parent 12f0e9348e
commit 221cd8bd0d
2 changed files with 34 additions and 4 deletions

View File

@@ -192,8 +192,8 @@ module.exports = function(srf, logger) {
if (0 === app.tasks.length) throw new Error('no application provided'); if (0 === app.tasks.length) throw new Error('no application provided');
next(); next();
} catch (err) { } catch (err) {
logger.info({err}, `Error retrieving or parsing application: ${err.message}`); logger.info({err}, `Error retrieving or parsing application: ${err?.message}`);
res.send(480, {headers: {'X-Reason': err.message}}); res.send(480, {headers: {'X-Reason': err?.message || 'unknown'}});
} }
} }

View File

@@ -8,6 +8,7 @@ const makeTask = require('../tasks/make_task');
const normalizeJambones = require('../utils/normalize-jambones'); const normalizeJambones = require('../utils/normalize-jambones');
const listTaskNames = require('../utils/summarize-tasks'); const listTaskNames = require('../utils/summarize-tasks');
const HttpRequestor = require('../utils/http-requestor'); const HttpRequestor = require('../utils/http-requestor');
const WsRequestor = require('../utils/ws-requestor');
const BADPRECONDITIONS = 'preconditions not met'; const BADPRECONDITIONS = 'preconditions not met';
const CALLER_CANCELLED_ERR_MSG = 'Response not sent due to unknown transaction'; const CALLER_CANCELLED_ERR_MSG = 'Response not sent due to unknown transaction';
@@ -291,6 +292,7 @@ class CallSession extends Emitter {
*/ */
async exec() { async exec() {
this.logger.info({tasks: listTaskNames(this.tasks)}, `CallSession:exec starting ${this.tasks.length} tasks`); this.logger.info({tasks: listTaskNames(this.tasks)}, `CallSession:exec starting ${this.tasks.length} tasks`);
while (this.tasks.length && !this.callGone) { while (this.tasks.length && !this.callGone) {
const taskNum = ++this.taskIdx; const taskNum = ++this.taskIdx;
const stackNum = this.stackIdx; const stackNum = this.stackIdx;
@@ -304,13 +306,22 @@ class CallSession extends Emitter {
this.logger.info(`CallSession:exec completed task #${stackNum}:${taskNum}: ${task.name}`); this.logger.info(`CallSession:exec completed task #${stackNum}:${taskNum}: ${task.name}`);
} catch (err) { } catch (err) {
this.currentTask = null; this.currentTask = null;
if (err.message.includes(BADPRECONDITIONS)) { if (err.message?.includes(BADPRECONDITIONS)) {
this.logger.info(`CallSession:exec task #${stackNum}:${taskNum}: ${task.name}: ${err.message}`); this.logger.info(`CallSession:exec task #${stackNum}:${taskNum}: ${task.name}: ${err.message}`);
} }
else { else {
this.logger.error(err, `Error executing task #${stackNum}:${taskNum}: ${task.name}`); this.logger.error(err, `Error executing task #${stackNum}:${taskNum}: ${task.name}`);
break; break;
} }
if (0 === this.tasks.length && this.hasStableDialog && this.requestor instanceof WsRequestor) {
try {
await this._awaitCommandsOrHangup();
} catch (err) {
this.logger.info(err, 'CallSession:exec - error waiting for new commands');
break;
}
}
} }
} }
@@ -370,6 +381,10 @@ class CallSession extends Emitter {
this.currentTask.kill(this); this.currentTask.kill(this);
this.currentTask = null; this.currentTask = null;
} }
if (this.wakeupResolver) {
this.wakeupResolver();
this.wakeupResolver = null;
}
} }
/** /**
@@ -620,7 +635,7 @@ class CallSession extends Emitter {
} }
_onCommand({msgid, command, queueCommand, data}) { _onCommand({msgid, command, queueCommand, data}) {
this.logger.info({msgid, command, data}, 'CallSession:_onCommand - received command'); this.logger.info({msgid, command, queueCommand, data}, 'CallSession:_onCommand - received command');
switch (command) { switch (command) {
case 'redirect': case 'redirect':
if (Array.isArray(data)) { if (Array.isArray(data)) {
@@ -665,6 +680,10 @@ class CallSession extends Emitter {
default: default:
this.logger.info(`CallSession:_onCommand - invalid command ${command}`); this.logger.info(`CallSession:_onCommand - invalid command ${command}`);
} }
if (this.wakeupResolver) {
this.wakeupResolver();
this.wakeupResolver = null;
}
} }
_evaluatePreconditions(task) { _evaluatePreconditions(task) {
@@ -1008,6 +1027,10 @@ class CallSession extends Emitter {
this.emit('callStatusChange', {callStatus: CallStatus.Completed, duration}); this.emit('callStatusChange', {callStatus: CallStatus.Completed, duration});
this.logger.debug('CallSession: call terminated by jambones'); this.logger.debug('CallSession: call terminated by jambones');
origDestroy(); origDestroy();
if (this.wakeupResolver) {
this.wakeupResolver();
this.wakeupResolver = null;
}
} }
}; };
} }
@@ -1076,6 +1099,13 @@ class CallSession extends Emitter {
this.updateCallStatus(Object.assign({}, this.callInfo.toJSON()), this.serviceUrl) this.updateCallStatus(Object.assign({}, this.callInfo.toJSON()), this.serviceUrl)
.catch((err) => this.logger.error(err, 'redis error')); .catch((err) => this.logger.error(err, 'redis error'));
} }
_awaitCommandsOrHangup() {
assert(!this.wakeupResolver);
return new Promise((resolve, reject) => {
this.wakeupResolver = resolve;
});
}
} }
module.exports = CallSession; module.exports = CallSession;