Skip to content

Commit c6d0266

Browse files
author
ax4
committed
add cron to server.js, a daily clean-up of ./tmp/input & ./tmp/output
as described
1 parent 80aa10d commit c6d0266

File tree

1 file changed

+60
-1
lines changed

1 file changed

+60
-1
lines changed

server.js

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,26 @@ var PythonShell = require('python-shell');
88
// web content
99
var client = fs.readFileSync('client.html');
1010

11+
// ref: http://stackoverflow.com/questions/18052762/remove-directory-which-is-not-empty
12+
var deleteFolderRecursive = function (path) {
13+
if (!(path == './tmp/del' || path == './tmp/input' || path == './tmp/output')) {
14+
return console.log('Bad Try with rm -r');
15+
}
16+
17+
if (fs.existsSync(path)) {
18+
fs.readdirSync(path).forEach(function (file, index) {
19+
var curPath = path + "/" + file;
20+
if (fs.lstatSync(curPath).isDirectory()) { // recurse
21+
deleteFolderRecursive(curPath);
22+
} else { // delete file
23+
fs.unlinkSync(curPath);
24+
}
25+
});
26+
fs.rmdirSync(path);
27+
}
28+
};
29+
30+
1131
// http server: send the web content
1232
var server = http.createServer(function (req, res) {
1333
res.writeHead(200, {});
@@ -70,6 +90,45 @@ ioserver.on('connection', function (socket) {
7090
});
7191
});
7292

73-
// run the server
93+
// create the cron job to clean folder
94+
var CronJob = require('cron').CronJob;
95+
//new CronJob('00 30 12 * * *', function () {
96+
// console.log('You will see this message every day on 12:30 AM as LAX time.');
97+
//}, null, true, 'America/Los_Angeles');
98+
new CronJob('00 30 12 * * *', function () {
99+
100+
var clean = function (path) {
101+
if (!(path == './tmp/del' || path == './tmp/input' || path == './tmp/output')) {
102+
return console.log('Bad Try with clean function()');
103+
}
104+
fs.stat(path, function (err, stats) {
105+
if (err) {
106+
return console.log(err);
107+
}
108+
if (stats.isDirectory()) {
109+
deleteFolderRecursive(path);
110+
console.log("rm -r " + path);
111+
fs.mkdir(path, function () {
112+
console.log("Re-mkdir" + path);
113+
});
114+
}
115+
});
116+
};
117+
server.close()
118+
//fixme: close socket.io server
119+
console.log('Server restart');
120+
var my_clean_list = function () {
121+
var folder_1 = './tmp/input';
122+
clean(folder_1);
123+
var folder_2 = './tmp/output';
124+
clean(folder_2);
125+
};
126+
127+
my_clean_list();
128+
129+
return server.listen(port);
130+
//fixme: re-open socket.io server
131+
}, null, true, 'America/Los_Angeles');
74132

133+
// run the server
75134
server.listen(port);

0 commit comments

Comments
 (0)