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

@@ -8,6 +8,7 @@ const makeTask = require('../tasks/make_task');
const normalizeJambones = require('../utils/normalize-jambones');
const listTaskNames = require('../utils/summarize-tasks');
const HttpRequestor = require('../utils/http-requestor');
const WsRequestor = require('../utils/ws-requestor');
const BADPRECONDITIONS = 'preconditions not met';
const CALLER_CANCELLED_ERR_MSG = 'Response not sent due to unknown transaction';
@@ -291,6 +292,7 @@ class CallSession extends Emitter {
*/
async exec() {
this.logger.info({tasks: listTaskNames(this.tasks)}, `CallSession:exec starting ${this.tasks.length} tasks`);
while (this.tasks.length && !this.callGone) {
const taskNum = ++this.taskIdx;
const stackNum = this.stackIdx;
@@ -304,13 +306,22 @@ class CallSession extends Emitter {
this.logger.info(`CallSession:exec completed task #${stackNum}:${taskNum}: ${task.name}`);
} catch (err) {
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}`);
}
else {
this.logger.error(err, `Error executing task #${stackNum}:${taskNum}: ${task.name}`);
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 = null;
}
if (this.wakeupResolver) {
this.wakeupResolver();
this.wakeupResolver = null;
}
}
/**
@@ -620,7 +635,7 @@ class CallSession extends Emitter {
}
_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) {
case 'redirect':
if (Array.isArray(data)) {
@@ -665,6 +680,10 @@ class CallSession extends Emitter {
default:
this.logger.info(`CallSession:_onCommand - invalid command ${command}`);
}
if (this.wakeupResolver) {
this.wakeupResolver();
this.wakeupResolver = null;
}
}
_evaluatePreconditions(task) {
@@ -1008,6 +1027,10 @@ class CallSession extends Emitter {
this.emit('callStatusChange', {callStatus: CallStatus.Completed, duration});
this.logger.debug('CallSession: call terminated by jambones');
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)
.catch((err) => this.logger.error(err, 'redis error'));
}
_awaitCommandsOrHangup() {
assert(!this.wakeupResolver);
return new Promise((resolve, reject) => {
this.wakeupResolver = resolve;
});
}
}
module.exports = CallSession;