Advertisement
Guest User

Untitled

a guest
Aug 18th, 2017
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //Various settings.
  2. var Client = require('mysql').Client;
  3.     client = new Client();
  4.     TEST_DATABASE = 't_voice';
  5.     TEST_TABLE = 't_calls';
  6. client.host = 'database.domain.com';
  7. client.user = 'auser';
  8. client.password = 'apassword';
  9. client.connect();
  10. client.query('USE '+TEST_DATABASE);
  11. console.log("Looking for expired calls...");
  12.  
  13. //Kill function, make REST call to kill call and update the database.
  14. function tropoKill(cs){
  15.         console.log("Killing call: "+cs);
  16.         var http = require('http')
  17.         var SESSIONID = cs;
  18.         var options = {
  19.           host: 'api.tropo.com',
  20.           port: 80,
  21.           path: '/1.0/sessions/'+SESSIONID+'/signals?action=signal&value=exit'
  22.         };
  23.         var req = http.get(options, function(res) {
  24.             res.on('data', function (chunk) {
  25.                 if(chunk=='<signal><status>QUEUED</status></signal>'){
  26.                         console.log("Hung up...");
  27.                         var a = client.query(
  28.                                 'UPDATE t_calls SET endDateTime = ? WHERE callSession = ?',['1000-00-00 00:00:00',cs], function (error, results) {
  29.                         })
  30.                 }else if(chunk=='<signal><status>NOTFOUND</status></signal>'){
  31.                         console.log("Already Gone...");
  32.                         var a = client.query(
  33.                                 'UPDATE t_calls SET endDateTime = ? WHERE callSession = ?',['1000-00-00 00:00:00',cs], function (error, results) {
  34.                 });
  35.  
  36.                 }
  37.                 req.end();
  38.             });
  39.         }).on('error', function(e) {
  40.         console.log("Got error: " + e.message);
  41.         req.end();
  42.         });
  43.  
  44. };
  45. //--------MAIN FUNCTION
  46. //Every second query database to look for expired records, iterate each record and kill the session and update the record.
  47. setInterval(function() {
  48. var a = client.query(
  49.   'SELECT * FROM '+TEST_TABLE+' '+
  50.   'WHERE ttl <= UTC_TIMESTAMP() AND endDateTime = ?',['0000-00-00 00:00:00'], function (error, results) {
  51.     if (error) {
  52.       throw err;
  53.     }
  54.     if(results.length > 0)
  55.       {
  56.           for(var i in results){
  57.           tropoKill(results[i]['callSession']);
  58.           }
  59.       };
  60.  
  61.  
  62.   });
  63. }, 1000)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement