This commit is contained in:
Dave Horton
2025-01-10 19:05:47 -05:00
parent c807fdb93d
commit 53a205db7d
4 changed files with 29 additions and 9 deletions
+2
View File
@@ -68,6 +68,7 @@ const {
const { const {
client: redisClient, client: redisClient,
addKey, addKey,
deleteKey,
retrieveKey, retrieveKey,
createSet, createSet,
retrieveSet, retrieveSet,
@@ -111,6 +112,7 @@ srf.locals = {...srf.locals,
}, },
realtimeDbHelpers: { realtimeDbHelpers: {
addKey, addKey,
deleteKey,
retrieveKey, retrieveKey,
createSet, createSet,
incrKey, incrKey,
+4 -2
View File
@@ -454,13 +454,15 @@ class CallSession extends Emitter {
if (process.env.JAMBONES_HOSTING || trackingOn) { if (process.env.JAMBONES_HOSTING || trackingOn) {
const {writeCallCount, writeCallCountSP, writeCallCountApp} = this.req.srf.locals; const {writeCallCount, writeCallCountSP, writeCallCountApp} = this.req.srf.locals;
if (!this.req.locals.callCountNudged) { if (!this.req.locals.callCountNudged) {
const {callId} = this.req.locals;
this.req.locals.callCountNudged = true; this.req.locals.callCountNudged = true;
this.logger.debug('decrementing call count at end of call'); this.logger.debug('decrementing call count at end of call');
await nudgeCallCounts(this.logger, { await nudgeCallCounts(this.logger, {
service_provider_sid: this.service_provider_sid, service_provider_sid: this.service_provider_sid,
account_sid: this.account_sid, account_sid: this.account_sid,
application_sid: this.application_sid application_sid: this.application_sid,
}, this.decrKey, {writeCallCountSP, writeCallCount, writeCallCountApp}) callId
}, this.decrKey, {writeCallCountSP, writeCallCount, writeCallCountApp}, -1)
.catch((err) => this.logger.error(err, 'Error decrementing call counts')); .catch((err) => this.logger.error(err, 'Error decrementing call counts'));
} }
} }
+7 -4
View File
@@ -248,6 +248,7 @@ module.exports = function(srf, logger) {
}; };
const checkLimits = async(req, res, next) => { const checkLimits = async(req, res, next) => {
const {callId} = req.locals;
const trackingOn = process.env.JAMBONES_TRACK_ACCOUNT_CALLS || const trackingOn = process.env.JAMBONES_TRACK_ACCOUNT_CALLS ||
process.env.JAMBONES_TRACK_SP_CALLS || process.env.JAMBONES_TRACK_SP_CALLS ||
process.env.JAMBONES_TRACK_APP_CALLS; process.env.JAMBONES_TRACK_APP_CALLS;
@@ -267,8 +268,9 @@ module.exports = function(srf, logger) {
nudgeCallCounts(logger, { nudgeCallCounts(logger, {
service_provider_sid, service_provider_sid,
account_sid, account_sid,
application_sid application_sid,
}, decrKey, {writeCallCountSP, writeCallCount, writeCallCountApp}) callId
}, decrKey, {writeCallCountSP, writeCallCount, writeCallCountApp}, -1)
.catch((err) => logger.error(err, 'Error decrementing call counts')); .catch((err) => logger.error(err, 'Error decrementing call counts'));
} }
}); });
@@ -278,8 +280,9 @@ module.exports = function(srf, logger) {
const {callsSP, calls} = await nudgeCallCounts(logger, { const {callsSP, calls} = await nudgeCallCounts(logger, {
service_provider_sid, service_provider_sid,
account_sid, account_sid,
application_sid application_sid,
}, incrKey, {writeCallCountSP, writeCallCount, writeCallCountApp}); callId
}, incrKey, {writeCallCountSP, writeCallCount, writeCallCountApp}, 1);
/* compare to account's limit, though avoid db hit when call count is low */ /* compare to account's limit, though avoid db hit when call count is low */
const minLimit = process.env.MIN_CALL_LIMIT ? const minLimit = process.env.MIN_CALL_LIMIT ?
+16 -3
View File
@@ -117,8 +117,8 @@ const createHealthCheckApp = (port, logger) => {
}); });
}; };
const nudgeCallCounts = async(logger, sids, nudgeOperator, writers) => { const nudgeCallCounts = async(logger, sids, nudgeOperator, writers, direction) => {
const { service_provider_sid, account_sid, application_sid } = sids; const { service_provider_sid, account_sid, callId, application_sid } = sids;
const { writeCallCount, writeCallCountSP, writeCallCountApp } = writers; const { writeCallCount, writeCallCountSP, writeCallCountApp } = writers;
const nudges = []; const nudges = [];
const writes = []; const writes = [];
@@ -152,7 +152,20 @@ const nudgeCallCounts = async(logger, sids, nudgeOperator, writers) => {
logger.debug({ logger.debug({
calls, callsSP, callsApp, calls, callsSP, callsApp,
service_provider_sid, account_sid, application_sid service_provider_sid, account_sid, application_sid
}, 'call counts after adjustment'); }, `call counts after ${direction > 0 ? 'increment' : 'decrement'}`);
if (process.env.JAMBONES_DEBUG_CALL_COUNTS) {
const {srf} = require('..');
const {addKey, deleteKey} = srf.locals.realtimeDbHelpers;
if (direction > 0) {
await addKey(`debug:incalls:${account_sid}:${callId}`, Date.now());
}
else {
await deleteKey(`debug:incalls:${account_sid}:${callId}`);
}
}
if (process.env.JAMBONES_TRACK_SP_CALLS) { if (process.env.JAMBONES_TRACK_SP_CALLS) {
writes.push(writeCallCountSP({ service_provider_sid, calls_in_progress: callsSP })); writes.push(writeCallCountSP({ service_provider_sid, calls_in_progress: callsSP }));
} }