mirror of
https://github.com/jambonz/jambonz-feature-server.git
synced 2025-12-20 16:50:39 +00:00
added option for clearing old tts files and orphaned channels periodically
This commit is contained in:
15
app.js
15
app.js
@@ -124,4 +124,19 @@ function handle(signal) {
|
|||||||
srf.locals.disabled = true;
|
srf.locals.disabled = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (process.env.JAMBONZ_CLEANUP_INTERVAL_MINS) {
|
||||||
|
const {clearFiles, clearChannels} = require('./lib/utils/cron-jobs');
|
||||||
|
|
||||||
|
/* cleanup orphaned files or channels every so often */
|
||||||
|
setTimeout(async() => {
|
||||||
|
try {
|
||||||
|
await clearFiles();
|
||||||
|
const count = await clearChannels();
|
||||||
|
if (count > 0) logger.info(`app.js: cleared ${count} orphaned channels`);
|
||||||
|
} catch (err) {
|
||||||
|
this.logger.error({err}, 'app.js: error clearing files and channels');
|
||||||
|
}
|
||||||
|
}, 1000 * 60 * (process.env.JAMBONZ_CLEANUP_INTERVAL_MINS || 60));
|
||||||
|
}
|
||||||
|
|
||||||
module.exports = {srf, logger, disconnect};
|
module.exports = {srf, logger, disconnect};
|
||||||
|
|||||||
52
lib/utils/cron-jobs.js
Normal file
52
lib/utils/cron-jobs.js
Normal file
@@ -0,0 +1,52 @@
|
|||||||
|
const {execSync} = require('child_process');
|
||||||
|
const now = Date.now();
|
||||||
|
const fsInventory = process.env.JAMBONES_FREESWITCH
|
||||||
|
.split(',')
|
||||||
|
.map((fs) => {
|
||||||
|
const arr = /^([^:]*):([^:]*):([^:]*)(?::([^:]*))?/.exec(fs);
|
||||||
|
const opts = {address: arr[1], port: arr[2], secret: arr[3]};
|
||||||
|
if (arr.length > 4) opts.advertisedAddress = arr[4];
|
||||||
|
if (process.env.NODE_ENV === 'test') opts.listenAddress = '0.0.0.0';
|
||||||
|
return opts;
|
||||||
|
});
|
||||||
|
|
||||||
|
const clearChannels = () => {
|
||||||
|
const {logger} = require('../..');
|
||||||
|
const pwd = fsInventory[0].secret;
|
||||||
|
const maxDurationMins = process.env.JAMBONES_FREESWITCH_MAX_CALL_DURATION_MINS || 180;
|
||||||
|
|
||||||
|
const calls = execSync(`sudo /usr/local/freeswitch/bin/fs_cli -p ${pwd} -x "show calls"`, {encoding: 'utf8'})
|
||||||
|
.split('\n')
|
||||||
|
.filter((line) => line.match(/^[0-9A-Fa-f]{8}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{8}/))
|
||||||
|
.map((line) => {
|
||||||
|
const arr = line.split(',');
|
||||||
|
const dt = new Date(arr[2]);
|
||||||
|
const duration = (now - dt.getTime()) / 1000;
|
||||||
|
return {
|
||||||
|
uuid: arr[0],
|
||||||
|
time: arr[2],
|
||||||
|
duration
|
||||||
|
};
|
||||||
|
})
|
||||||
|
.filter((c) => c.duration > 60 * maxDurationMins);
|
||||||
|
|
||||||
|
if (calls.length > 0) {
|
||||||
|
logger.debug(`clearChannels: clearing ${calls.length} old calls longer than ${maxDurationMins} mins`);
|
||||||
|
for (const call of calls) {
|
||||||
|
const cmd = `sudo /usr/local/freeswitch/bin/fs_cli -p ${pwd} -x "uuid_kill ${call.uuid}"`;
|
||||||
|
const out = execSync(cmd, {encoding: 'utf8'});
|
||||||
|
logger.debug({out}, 'clearChannels: command output');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return calls.length;
|
||||||
|
};
|
||||||
|
|
||||||
|
const clearFiles = () => {
|
||||||
|
const {logger} = require('../..');
|
||||||
|
const out = execSync('find /tmp -name "*.mp3" -mtime +2 -exec rm {} \;');
|
||||||
|
logger.debug({out}, 'clearFiles: command output');
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
module.exports = {clearChannels, clearFiles};
|
||||||
|
|
||||||
Reference in New Issue
Block a user