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