initial changes for queue webhooks

This commit is contained in:
Dave Horton
2021-06-17 15:06:52 -04:00
parent 473a34ec9f
commit bb0ec8e184
3 changed files with 75 additions and 8 deletions

View File

@@ -76,11 +76,20 @@ class TaskEnqueue extends Task {
const members = await pushBack(this.queueName, url);
this.logger.info(`TaskEnqueue:_addToQueue: added to queue, length now ${members}`);
this.notifyUrl = url;
/* invoke account-level webhook for queue event notifications */
cs.performQueueWebhook({
event: 'join',
queue: this.data.name,
length: members,
joinTime: this.waitStartTime
});
}
async _removeFromQueue(cs, dlg) {
const {removeFromList} = cs.srf.locals.dbHelpers;
return await removeFromList(this.queueName, getUrl(cs));
async _removeFromQueue(cs) {
const {removeFromList, lengthOfList} = cs.srf.locals.dbHelpers;
await removeFromList(this.queueName, getUrl(cs));
return await lengthOfList(this.queueName);
}
async performAction() {
@@ -109,8 +118,20 @@ class TaskEnqueue extends Task {
}
resolve(this._doBridge(cs, dlg, ep));
})
.once('kill', () => {
this._removeFromQueue(cs);
.once('kill', async() => {
try {
const members = await this._removeFromQueue(cs);
/* invoke account-level webhook for queue event notifications */
cs.performQueueWebhook({
event: 'leave',
queue: this.data.name,
length: members,
leaveReason: 'abandoned',
leaveTime: Date.now()
});
} catch (err) {}
if (this._playSession) {
this.logger.debug('killing waitUrl');
this._playSession.kill();
@@ -277,7 +298,7 @@ class TaskEnqueue extends Task {
const json = await cs.application.requestor.request(hook, params);
const tasks = normalizeJambones(this.logger, json).map((tdata) => makeTask(this.logger, tdata));
const allowedTasks = tasks.filter((t) => allowed.includes(t.verb));
const allowedTasks = tasks.filter((t) => allowed.includes(t.name));
if (tasks.length !== allowedTasks.length) {
this.logger.debug({tasks, allowedTasks}, 'unsupported task');
throw new Error(`unsupported verb in enqueue waitHook: only ${JSON.stringify(allowed)}`);
@@ -288,7 +309,7 @@ class TaskEnqueue extends Task {
const tasksToRun = [];
let leave = false;
for (const o of tasks) {
if (o.verb === TaskName.Leave) {
if (o.name === TaskName.Leave) {
leave = true;
this.logger.info('waitHook returned a leave task');
break;
@@ -304,7 +325,7 @@ class TaskEnqueue extends Task {
dlg,
ep: cs.ep,
callInfo: cs.callInfo,
tasksToRun
tasks: tasksToRun
});
await this._playSession.exec();
this._playSession = null;