added a logger, started working on the maintenance timer routines
[nodejs-repoproxy.git] / lib / cache.js
index a649991..588bc73 100644 (file)
@@ -2,19 +2,7 @@ var fs = require("fs");
 var http = require("http");
 var url = require("url");
 var path = require("path");
-
-function maintainCache() {
-       // TODO i should check that im already running here and exit if i am
-       console.log("Cache maintainence routine starting...");
-       console.log("Cache maintainence routine ended...");
-}
-
-exports.startTimer = function() {
-       // our once-a-day cache maintainer
-       var cacheTimer = global.repoproxy.scancache*3600*1000;
-       //var cacheTimer = global.repoproxy.scancache*100;
-       setInterval(maintainCache, cacheTimer);
-}
+var crypto = require("crypto");
 
 function upstreamRequest(unify) {
        // first do a head request
@@ -151,6 +139,7 @@ function inlineService(unify) {
        var metafilename = unify.fullPathDirName + "/.meta."+ path.basename(unify.requestFor) +".filesize";
        var fsizef = fs.createReadStream(metafilename);
        var fsize = "";
+       var lastchunk = 0;
        fsizef.on("data", function(data) {
                fsize += data;
        });
@@ -168,6 +157,9 @@ function inlineService(unify) {
                        fs.stat(unify.fullFilePath, function(err, stats) {
                                if(err == null) {
                                        if(stats["size"] > sentSoFar) {
+                                               // if file size changed between last chunk and this chunk, send the chunks
+                                               
+                                               lastChunk = 0;
                                                // open the file, send the data
                                                var rs = fs.createReadStream(unify.fullFilePath, {start: sentSoFar, end: stats["size"]});
                                                
@@ -186,6 +178,18 @@ function inlineService(unify) {
                                                                unify.b.end();
                                                        }
                                                });
+                                       } else {
+                                               // if file size did not change between last timeout and this one, incremement the chunk counter
+                                               // if we reach 60, we had a problem, and so we bomb out
+                                               
+                                               lastChunk++;
+                                               
+                                               // we bombed out somehow
+                                               if(lastChunk > 60) {
+                                                       unify.b.end();
+                                               } else {
+                                                       setTimeout(sendPieces, 1000);
+                                               }
                                        }
                                } else {
                                        console.log("inline service - we're in a very bad place");
@@ -329,4 +333,22 @@ function serviceDirectory(unify) {
        });
 }
 
-exports.serviceDirectory = serviceDirectory;
\ No newline at end of file
+function moveToCleanup(file_or_dir) {
+       // err..?
+       var cleanup = global.repoproxy.cacheDir + "/.cleanup";
+       var ctime = new Date().getTime();
+       var encoded = (++global.repoproxy.fileid).toString();
+       var toloc = cleanup + "/" + ctime.toString() + "." + encoded;
+       
+       //console.log("Moving %s to %s for cleanup", file_or_dir.replace(/\/$/, ""), toloc);
+       
+       fs.renameSync(file_or_dir.replace(/\/$/, ""), toloc);
+}
+
+function cleanupRoutine() {
+       
+}
+
+
+exports.serviceDirectory = serviceDirectory;
+exports.moveToCleanup = moveToCleanup;