From 92ca40c9b35d8bc1aebda21d6291bdbeb358a5df Mon Sep 17 00:00:00 2001 From: Dave Horton Date: Wed, 6 Apr 2022 15:54:59 -0400 Subject: [PATCH] add feature flag env JAMBONES_INJECT_CONTENT (#98) --- lib/session/call-session.js | 52 +++++++++++++++++++++++++++++++++++-- 1 file changed, 50 insertions(+), 2 deletions(-) diff --git a/lib/session/call-session.js b/lib/session/call-session.js index 6407d1cf..7302a598 100644 --- a/lib/session/call-session.js +++ b/lib/session/call-session.js @@ -763,6 +763,49 @@ class CallSession extends Emitter { this.taskIdx = 0; } + /** + * Append tasks to the current execution stack UNLESS there is a gather in the stack. + * in that case, insert the tasks before the gather AND if the tasks include + * a gather then delete/remove the gather from the existing stack + * @param {*} t array of tasks + */ + _injectTasks(newTasks) { + const gatherPos = this.tasks.map((t) => t.name).indexOf(TaskName.Gather); + const currentlyExecutingGather = this.currentTask?.name === TaskName.Gather; + + this.logger.debug({ + currentTaskList: listTaskNames(this.tasks), + newContent: listTaskNames(newTasks), + currentlyExecutingGather, + gatherPos + }, 'CallSession:_injectTasks - starting'); + + const killGather = () => { + this.logger.debug('CallSession:_injectTasks - killing current gather because we have new content'); + this.currentTask.kill(this); + }; + + if (-1 === gatherPos) { + /* no gather in the stack simply append tasks */ + this.tasks.push(...newTasks); + this.logger.debug({ + updatedTaskList: listTaskNames(this.tasks) + }, 'CallSession:_injectTasks - completed (simple append)'); + + /* we do need to kill the current gather if we are executing one */ + if (currentlyExecutingGather) killGather(); + return; + } + + if (currentlyExecutingGather) killGather(); + const newTasksHasGather = newTasks.find((t) => t.name === TaskName.Gather); + this.tasks.splice(gatherPos, newTasksHasGather ? 1 : 0, ...newTasks); + + this.logger.debug({ + updatedTaskList: listTaskNames(this.tasks) + }, 'CallSession:_injectTasks - completed'); + } + _onCommand({msgid, command, call_sid, queueCommand, data}) { this.logger.info({msgid, command, queueCommand}, 'CallSession:_onCommand - received command'); const resolution = {reason: 'received command', queue: queueCommand, command}; @@ -775,10 +818,15 @@ class CallSession extends Emitter { this.logger.info({tasks: listTaskNames(t)}, 'CallSession:_onCommand new task list'); this.replaceApplication(t); } + else if (process.env.JAMBONES_INJECT_CONTENT) { + this.logger.debug({tasks: listTaskNames(t)}, 'CallSession:_onCommand - queueing tasks (injecting content)'); + this._injectTasks(t); + this.logger.info({tasks: listTaskNames(this.tasks)}, 'CallSession:_onCommand - updated task list'); + } else { - this.logger.info({tasks: listTaskNames(t)}, 'CallSession:_onCommand - queueing tasks'); + this.logger.debug({tasks: listTaskNames(t)}, 'CallSession:_onCommand - queueing tasks'); this.tasks.push(...t); - this.logger.debug({tasks: listTaskNames(this.tasks)}, 'CallSession:_onCommand - updated task list'); + this.logger.info({tasks: listTaskNames(this.tasks)}, 'CallSession:_onCommand - updated task list'); } resolution.command = listTaskNames(t); }