cd8ea8b5bf9db930fd90bb6b9cd538e64e39af75
[nodejs-repoproxy.git] / lib / maintain.js
1 var fs = require("fs");
2 var http = require("http");
3 var url = require("url");
4 var path = require("path");
5 var repoapt = require("./repo-apt.js");
6 var repoyum = require("./repo-yum.js");
7 var log = require("./log.js");
8
9 function maintainCache() {
10         // TODO i should check that im already running here and exit if i am
11         if(global.repoproxy.maintain==1) {
12                 log.warning("cache maintenance routine started, but its already running");
13                 return;
14         }
15         
16         global.repoproxy.maintain = 1;
17         
18         log.info("Cache maintainence routine starting...");
19         for(var index in global.repoproxy.repo) {
20                 //log.debug("start cleanup in ", index);
21                 //log.debug("cleanup array ", global.repoproxy.repo[index]);
22                 switch(global.repoproxy.repo[index]["type"]) {
23                 case "apt":
24                         log.debug("Scanning '%s' as apt", index);
25                         var walkin = path.normalize(global.repoproxy.cacheDir + "/" + index);
26                         walkDir(walkin, function(err, list) {
27                                 repoapt.maintain(index, global.repoproxy.repo[index], list);
28                         })
29                         break;
30                 case "yum":
31                         //log.debug("Scanning '%s' as apt", index);
32                         //repoyum.maintain(global.repoproxy.repo[index]);
33                         log.debug("Scanning '%s' as yum", index);
34                         var walkin = path.normalize(global.repoproxy.cacheDir + "/" + index);
35                         walkDir(walkin, function(err, list) {
36                                 repoyum.maintain(index, global.repoproxy.repo[index], list);
37                         })
38                         break;
39                         break;
40                 }
41         }
42         log.info("Cache maintainence routine ended...");
43         
44         
45         
46         log.info("beginning cache trash cleanup");
47         // TODO: do this bit properly, check that globals are set properly
48         var spawn = require('child_process').spawn;
49         
50         // TODO: be carefull we actually have a place to remove
51         if(typeof global.repoproxy == "undefined") {
52                 log.error("serious issue, globals not accessible?");
53                 process.exit(10);
54         } else if(typeof global.repoproxy.cacheDir == "undefined") {
55                 log.error("serious issue, globals not accessible (cache check)?");
56                 process.exit(10);
57         } else {
58             var remove  = spawn("rm", ["-r", global.repoproxy.cacheDir + "/.cleanup/*"]);
59             remove.on("close", function(code, sig) {
60                 log.debug("remove ended with %d, %d", code, sig);
61             });
62             remove.stderr.on("data", function(line) {
63                 log.debug("stderr from lazy remove: ", line.toString("utf8"));
64             });
65             remove.stdout.on("data", function(line) {
66                 log.debug("stdout from lazy remove: ", line.toString("ascii"));
67             });
68         }
69         log.info("Trash empty");
70         
71         global.repoproxy.maintain = 0;
72 }
73
74 exports.startTimer = function() {
75         // our once-a-day cache maintainer
76         var cacheTimer = global.repoproxy.scancache*3600*1000;
77         //var cacheTimer = global.repoproxy.scancache*100;
78         maintainCache();
79         setInterval(maintainCache, cacheTimer);
80 }
81
82 // this code comes frmo http://stackoverflow.com/questions/5827612/node-js-fs-readdir-recursive-directory-search
83 function walkDir(dir, done) {
84         var results = [];
85         
86         fs.readdir(dir, function(err, list) {
87                 if (err) return done(err);
88                 var i = 0;
89                 (function next() {
90                         var file = list[i++];
91                         if (!file) return done(null, results);
92                         file = path.normalize(dir + "/" + file);
93                         fs.stat(file, function(err, stat) {
94                                 if (stat && stat.isDirectory()) {
95                                         walkDir(file, function(err, res) {
96                                                 results = results.concat(res);
97                                                 next();
98                                         });
99                                 } else {
100                                         if(!file.match(/.*\.meta.*\.filesize/)) results.push(file);
101                                         next();
102                                 }
103                         });
104                 })();
105         });
106 };
107
108
109
110 exports.walkDir = walkDir;