Skip to content

Instantly share code, notes, and snippets.

Created May 15, 2011 22:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save anonymous/973620 to your computer and use it in GitHub Desktop.
Save anonymous/973620 to your computer and use it in GitHub Desktop.
//Various settings.
var Client = require('mysql').Client;
client = new Client();
TEST_DATABASE = 't_voice';
TEST_TABLE = 't_calls';
client.host = 'database.domain.com';
client.user = 'auser';
client.password = 'apassword';
client.connect();
client.query('USE '+TEST_DATABASE);
console.log("Looking for expired calls...");
//Kill function, make REST call to kill call and update the database.
function tropoKill(cs){
console.log("Killing call: "+cs);
var http = require('http')
var SESSIONID = cs;
var options = {
host: 'api.tropo.com',
port: 80,
path: '/1.0/sessions/'+SESSIONID+'/signals?action=signal&value=exit'
};
var req = http.get(options, function(res) {
res.on('data', function (chunk) {
if(chunk=='<signal><status>QUEUED</status></signal>'){
console.log("Hung up...");
var a = client.query(
'UPDATE t_calls SET endDateTime = ? WHERE callSession = ?',['1000-00-00 00:00:00',cs], function (error, results) {
})
}else if(chunk=='<signal><status>NOTFOUND</status></signal>'){
console.log("Already Gone...");
var a = client.query(
'UPDATE t_calls SET endDateTime = ? WHERE callSession = ?',['1000-00-00 00:00:00',cs], function (error, results) {
});
}
req.end();
});
}).on('error', function(e) {
console.log("Got error: " + e.message);
req.end();
});
};
//--------MAIN FUNCTION
//Every second query database to look for expired records, iterate each record and kill the session and update the record.
setInterval(function() {
var a = client.query(
'SELECT * FROM '+TEST_TABLE+' '+
'WHERE ttl <= UTC_TIMESTAMP() AND endDateTime = ?',['0000-00-00 00:00:00'], function (error, results) {
if (error) {
throw err;
}
if(results.length > 0)
{
for(var i in results){
tropoKill(results[i]['callSession']);
}
};
});
}, 1000)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment