X-Git-Url: https://git.sesse.net/?p=remoteglot;a=blobdiff_plain;f=www%2Fserve-analysis.js;h=80f969e076a5e343fb712369e78771715208cd22;hp=56845eef01f7d0d37391392a1810605441b51dca;hb=73764545d4560b3ad9f49badb7978eb0f2a98b74;hpb=a66c165f94e06a675b392d133f60c3dbbab22c60 diff --git a/www/serve-analysis.js b/www/serve-analysis.js index 56845ee..80f969e 100644 --- a/www/serve-analysis.js +++ b/www/serve-analysis.js @@ -6,13 +6,14 @@ var http = require('http'); var fs = require('fs'); var url = require('url'); var querystring = require('querystring'); +var path = require('path'); // Constants. var json_filename = '/srv/analysis.sesse.net/www/analysis.json'; // The current contents of the file to hand out, and its last modified time. -var json_contents = null; -var json_last_modified = null; +var json_contents = undefined; +var json_last_modified = undefined; // The list of clients that are waiting for new data to show up, // and their associated timers. Uniquely keyed by request_id @@ -24,7 +25,11 @@ var request_id = 0; // Used to show a viewer count to the user. var last_seen_clients = {}; -var reread_file = function() { +var reread_file = function(event, filename) { + if (filename != path.basename(json_filename)) { + return; + } + console.log("Rereading " + json_filename); fs.open(json_filename, 'r+', function(err, fd) { if (err) throw err; fs.fstat(fd, function(err, st) { @@ -89,8 +94,8 @@ var count_viewers = function() { // Set up a watcher to catch changes to the file, then do an initial read // to make sure we have a copy. -fs.watch(json_filename, reread_file); -reread_file(); +fs.watch(path.dirname(json_filename), reread_file); +reread_file(null, path.basename(json_filename)); http.createServer(function(request, response) { var u = url.parse(request.url, true); @@ -111,7 +116,7 @@ http.createServer(function(request, response) { // If we already have something newer than what the user has, // just send it out and be done with it. - if (!ims || json_last_modified > ims) { + if (json_last_modified !== undefined && (!ims || json_last_modified > ims)) { send_json(response); return; }