X-Git-Url: http://git.pjr.cc/?p=nodejs-repoproxy.git;a=blobdiff_plain;f=lib%2Fmaintain.js;h=6ae0429823c3179a97c7fcb162edb9ec5b29f13b;hp=e336d4ad96a04792f5a6cf3a9d39b0f133742e4f;hb=51793863317f57b5e1d7d6d05043a8b8d3fee746;hpb=04f3190a0bcc430af31917456a957ecc80eb160b diff --git a/lib/maintain.js b/lib/maintain.js index e336d4a..6ae0429 100644 --- a/lib/maintain.js +++ b/lib/maintain.js @@ -4,25 +4,71 @@ var url = require("url"); var path = require("path"); var repoapt = require("./repo-apt.js"); var repoyum = require("./repo-yum.js"); +var log = require("./log.js"); function maintainCache() { // TODO i should check that im already running here and exit if i am - console.log("Cache maintainence routine starting..."); + if(global.repoproxy.maintain==1) { + log.warning("cache maintenance routine started, but its already running"); + return; + } + + global.repoproxy.maintain = 1; + + log.info("Cache maintainence routine starting..."); for(var index in global.repoproxy.repo) { - console.log("start cleanup in ", index); - console.log("cleanup array ", global.repoproxy.repo[index]); + //log.debug("start cleanup in ", index); + //log.debug("cleanup array ", global.repoproxy.repo[index]); switch(global.repoproxy.repo[index]["type"]) { case "apt": - console.log("Scanning '%s' as apt", index); - repoapt.maintain(global.repoproxy.repo[index]); + log.debug("Scanning '%s' as apt", index); + var walkin = path.normalize(global.repoproxy.cacheDir + "/" + index); + walkDir(walkin, function(err, list) { + repoapt.maintain(index, global.repoproxy.repo[index], list); + }) break; case "yum": - console.log("Scanning '%s' as apt", index); - repoyum.maintain(global.repoproxy.repo[index]); + //log.debug("Scanning '%s' as apt", index); + //repoyum.maintain(global.repoproxy.repo[index]); + log.debug("Scanning '%s' as yum", index); + var walkin = path.normalize(global.repoproxy.cacheDir + "/" + index); + walkDir(walkin, function(err, list) { + repoyum.maintain(index, global.repoproxy.repo[index], list); + }) + break; break; } } - console.log("Cache maintainence routine ended..."); + log.info("Cache maintainence routine ended..."); + + + + log.info("beginning cache trash cleanup"); + // TODO: do this bit properly, check that globals are set properly + var spawn = require('child_process').spawn; + + // TODO: be carefull we actually have a place to remove + if(typeof global.repoproxy == "undefined") { + log.error("serious issue, globals not accessible?"); + process.exit(10); + } else if(typeof global.repoproxy.cacheDir == "undefined") { + log.error("serious issue, globals not accessible (cache check)?"); + process.exit(10); + } else { + var remove = spawn("rm", ["-r", global.repoproxy.cacheDir + "/.cleanup/*"]); + remove.on("close", function(code, sig) { + log.debug("remove ended with %d, %d", code, sig); + }); + remove.stderr.on("data", function(line) { + log.debug("stderr from lazy remove: ", line.toString("utf8")); + }); + remove.stdout.on("data", function(line) { + log.debug("stdout from lazy remove: ", line.toString("ascii")); + }); + } + log.info("Trash empty"); + + global.repoproxy.maintain = 0; } exports.startTimer = function() { @@ -33,6 +79,32 @@ exports.startTimer = function() { setInterval(maintainCache, cacheTimer); } -function cleanupRoutine() { +// this code comes frmo http://stackoverflow.com/questions/5827612/node-js-fs-readdir-recursive-directory-search +function walkDir(dir, done) { + var results = []; -} \ No newline at end of file + fs.readdir(dir, function(err, list) { + if (err) return done(err); + var i = 0; + (function next() { + var file = list[i++]; + if (!file) return done(null, results); + file = path.normalize(dir + "/" + file).replace(/\/+/g, "/"); + fs.stat(file, function(err, stat) { + if (stat && stat.isDirectory()) { + walkDir(file, function(err, res) { + results = results.concat(res); + next(); + }); + } else { + if(!file.match(/.*\.meta.*\.filesize/)) results.push(file); + next(); + } + }); + })(); + }); +}; + + + +exports.walkDir = walkDir;