Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //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)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement