Created
May 15, 2011 22:34
-
-
Save anonymous/973620 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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