repository maintainence code
[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                         break;
26                 }
27         }
28         console.log("Cache maintainence routine ended...");
29 }
30
31 exports.startTimer = function() {
32         // our once-a-day cache maintainer
33         var cacheTimer = global.repoproxy.scancache*3600*1000;
34         //var cacheTimer = global.repoproxy.scancache*100;
35         maintainCache();
36         setInterval(maintainCache, cacheTimer);
37 }
38
39 // this code comes frmo http://stackoverflow.com/questions/5827612/node-js-fs-readdir-recursive-directory-search
40 function walkDir(dir, done) {
41         var results = [];
42         
43         fs.readdir(dir, function(err, list) {
44                 if (err) return done(err);
45                 var i = 0;
46                 (function next() {
47                         var file = list[i++];
48                         if (!file) return done(null, results);
49                         file = path.normalize(dir + "/" + file);
50                         fs.stat(file, function(err, stat) {
51                                 if (stat && stat.isDirectory()) {
52                                         walkDir(file, function(err, res) {
53                                                 results = results.concat(res);
54                                                 next();
55                                         });
56                                 } else {
57                                         if(!file.match(/.*\.meta.*\.filesize/)) results.push(file);
58                                         next();
59                                 }
60                         });
61                 })();
62         });
63 };
64
65 exports.walkDir = walkDir;
66
67 function cleanupRoutine() {
68         
69 }