added a logger, started working on the maintenance timer routines
[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
8 function maintainCache() {
9         // TODO i should check that im already running here and exit if i am
10         console.log("Cache maintainence routine starting...");
11         for(var index in global.repoproxy.repo) {
12                 //console.log("start cleanup in ", index);
13                 //console.log("cleanup array ", global.repoproxy.repo[index]);
14                 switch(global.repoproxy.repo[index]["type"]) {
15                 case "apt":
16                         console.log("Scanning '%s' as apt", index);
17                         var walkin = path.normalize(global.repoproxy.cacheDir + "/" + index);
18                         walkDir(walkin, function(err, list) {
19                                 repoapt.maintain(index, global.repoproxy.repo[index], list);
20                         })
21                         break;
22                 case "yum":
23                         //console.log("Scanning '%s' as apt", index);
24                         //repoyum.maintain(global.repoproxy.repo[index]);
25                         console.log("Scanning '%s' as yum", index);
26                         var walkin = path.normalize(global.repoproxy.cacheDir + "/" + index);
27                         walkDir(walkin, function(err, list) {
28                                 repoyum.maintain(index, global.repoproxy.repo[index], list);
29                         })
30                         break;
31                         break;
32                 }
33         }
34         console.log("Cache maintainence routine ended...");
35 }
36
37 exports.startTimer = function() {
38         // our once-a-day cache maintainer
39         var cacheTimer = global.repoproxy.scancache*3600*1000;
40         //var cacheTimer = global.repoproxy.scancache*100;
41         maintainCache();
42         setInterval(maintainCache, cacheTimer);
43 }
44
45 // this code comes frmo http://stackoverflow.com/questions/5827612/node-js-fs-readdir-recursive-directory-search
46 function walkDir(dir, done) {
47         var results = [];
48         
49         fs.readdir(dir, function(err, list) {
50                 if (err) return done(err);
51                 var i = 0;
52                 (function next() {
53                         var file = list[i++];
54                         if (!file) return done(null, results);
55                         file = path.normalize(dir + "/" + file);
56                         fs.stat(file, function(err, stat) {
57                                 if (stat && stat.isDirectory()) {
58                                         walkDir(file, function(err, res) {
59                                                 results = results.concat(res);
60                                                 next();
61                                         });
62                                 } else {
63                                         if(!file.match(/.*\.meta.*\.filesize/)) results.push(file);
64                                         next();
65                                 }
66                         });
67                 })();
68         });
69 };
70
71
72
73 exports.walkDir = walkDir;