Improve logging date format and integrate transactions in database ops. default tip

Thu, 14 Aug 2014 19:15:12 +0200

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 14 Aug 2014 19:15:12 +0200
changeset 10
f48fa3532729
parent 9
3d4d0e0dd4de

Improve logging date format and integrate transactions in database ops.

src/lib/app.js file | annotate | diff | comparison | revisions
     1.1 --- a/src/lib/app.js	Thu Aug 14 19:14:04 2014 +0200
     1.2 +++ b/src/lib/app.js	Thu Aug 14 19:15:12 2014 +0200
     1.3 @@ -37,6 +37,8 @@
     1.4  |                                                          |
     1.5  | Support: http://list.europalab.com/mailman/mdnsgs/       |
     1.6  |                                                          |
     1.7 +| Test: dig @nodeapp.host.tld A realhost.local             |
     1.8 +|                                                          |
     1.9  ***********************************************************/
    1.10  
    1.11  // import module dependencies
    1.12 @@ -55,6 +57,24 @@
    1.13    cleardb();
    1.14  });
    1.15  
    1.16 +// format a date and time
    1.17 +function getDateTime() {
    1.18 +    var date = new Date();
    1.19 +    var hour = date.getHours();
    1.20 +    hour = (hour < 10 ? '0' : '') + hour;
    1.21 +    var min  = date.getMinutes();
    1.22 +    min = (min < 10 ? '0' : '') + min;
    1.23 +    var sec  = date.getSeconds();
    1.24 +    sec = (sec < 10 ? '0' : '') + sec;
    1.25 +    var year = date.getFullYear();
    1.26 +    var month = date.getMonth() + 1;
    1.27 +    month = (month < 10 ? '0' : '') + month;
    1.28 +    var day  = date.getDate();
    1.29 +    day = (day < 10 ? '0' : '') + day;
    1.30 +
    1.31 +    return year + '.' + month + '.' + day + '-' + hour + ':' + min + ':' + sec;
    1.32 +}
    1.33 +
    1.34  // instantiate a new redis client
    1.35  // http://www.rediscookbook.org/
    1.36  var rediscli = redisdat.createClient();
    1.37 @@ -96,16 +116,27 @@
    1.38        browserv.on('serviceUp', function(service) {
    1.39          //console.log('service up: ', service);
    1.40          //{interfaceIndex: 2, type: {name: 'ssh', protocol: 'tcp', subtypes: [], fullyQualified: true}, replyDomain: 'local.', flags: 2, name: 'hostname-mich', networkInterface: 'eth0', fullname: 'hostname-mich._ssh._tcp.local.', host: 'hostname-mich.local.', port: 22, addresses: ['192.168.1.50']}
    1.41 +        rediscli.hget('hostnames', service.host.replace(/\.$/, ''), function (error, value) {
    1.42 +          // handle unexpected errors
    1.43 +          if (error) throw(error);
    1.44  
    1.45 -        // insert one or more IP addresses for each hostname.local.
    1.46 -        rediscli.hset('hostnames', service.host.replace(/\.$/, ''), service.addresses);
    1.47 +          // insert one or more IP addresses for each hostname.local.
    1.48 +          if (!value) { // only visit new (or with changed IPs) hosts
    1.49 +            var rcmulti = rediscli.multi(); // start a new transaction
    1.50 +            rcmulti.hsetnx('hostnames', service.host.replace(/\.$/, ''), service.addresses, function (error, nret) {
    1.51 +              if (nret === 1) // database wrote a new entry
    1.52 +                console.log('    ' + getDateTime() + ' Detected host: ' + service.host.replace(/\.$/, '') + ' ' + service.addresses);
    1.53 +            }); //rcmulti.hsetnx();
    1.54 +            rcmulti.exec(); // flush transaction queue
    1.55 +          }
    1.56 +        });
    1.57        });
    1.58        browserv.on('serviceDown', function(service) {
    1.59 -        console.log('service down: ', service);
    1.60 +        //console.log('service down: ', service);
    1.61          //FIXME: still need to selectively remove hosts
    1.62        });
    1.63        browserv.on('serviceChanged', function(service) {
    1.64 -        console.log('service changed: ', service);
    1.65 +        //console.log('service changed: ', service);
    1.66          //FIXME: still need to selectively update hosts
    1.67        });
    1.68        browserv.start();
    1.69 @@ -125,6 +156,17 @@
    1.70      if (error) throw(error);
    1.71  
    1.72      if (value) { // the db succeeded in finding a match
    1.73 +      // FIXME: need to test incoming questions stripping trailing '.'
    1.74 +      // FIXME: and adding '.local' to handle cases of non FQDNs.
    1.75 +      // FIXME:   var found; // = {}; doesnt work unfortunately
    1.76 +      // FIXME:   if (request.question[0].name == host)
    1.77 +      // FIXME:     found = host;
    1.78 +      // FIXME:   else if (request.question[0].name + '.local' == host)
    1.79 +      // FIXME:     found = host.replace(/\.local$/, '');
    1.80 +      //
    1.81 +      // FIXME: replace silly new block with simple 'push(nameinst.A)
    1.82 +      // FIXME: since we already know that the host in question exists
    1.83 +      //
    1.84        // populate the DNS response with the chosen hostname
    1.85        rediscli.hkeys('hostnames', function (error, replies) {
    1.86            replies.forEach(function (host, index) {

mercurial