forked from CartoDB/mobile-tile-packager
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcleanup.js
107 lines (95 loc) · 3.07 KB
/
cleanup.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
var fs = require('fs');
var kue = require('kue'),
jobs = kue.createQueue();
var async = require('async');
var conf = require('./config.js');
var hlprs = require('./helpers.js');
var now = new Date().getTime();
CLEANUP_MAX_FAILED_TIME = 30 * 24 * 60 * 60 * 1000; // 30 days
CLEANUP_MAX_ACTIVE_TIME = 1 * 24 * 60 * 60 * 1000; // 1 day
CLEANUP_MAX_COMPLETE_TIME = 2 * 24 * 60 * 60 * 1000; // 2 days
//CLEANUP_MAX_COMPLETE_TIME = 50 * 60 * 1000; // 50 minutes
MAX_JOBS = 100
async.parallel([
function(callback) {
kue.Job.rangeByState('complete', 0, MAX_JOBS, 'asc', function(err, jobs) {
if (err) return callback(err);
else {
var counter = jobs.length;
if (counter == 0)
return callback();
jobs.forEach(function(job) {
var valid = (parseInt(job.created_at) + CLEANUP_MAX_COMPLETE_TIME);
if (now > valid) {
job.remove();
var zipFile = conf.output + '/' + job.data.username + '_' + job.id + '.zip';
if (hlprs.fileExists(zipFile)) {
fs.unlink(zipFile, function(err){
if (err) return callback(err);
else console.log('removed file %s', zipFile);
});
}
console.log('removed completed job %s', job.id);
}
else
console.log('valid complete %s < %s job - %s', now, valid, job.id);
counter -=1;
if (counter == 0) {
return callback();
}
});
}
});
},
function(callback) {
kue.Job.rangeByState('failed', 0, MAX_JOBS, 'asc', function(err, jobs) {
if (err) return callback(err);
else {
var counter = jobs.length;
if (counter == 0)
return callback();
jobs.forEach(function(job) {
var valid = (parseInt(job.created_at) + CLEANUP_MAX_COMPLETE_TIME);
if (now > valid) {
job.remove();
console.log('removed failed job %s', job.id);
}
else
console.log('valid failed %s < %s job - %s', now, valid, job.id);
counter -=1;
if (counter == 0) {
return callback();
}
});
}
});
},
function(callback) {
kue.Job.rangeByState('active', 0, MAX_JOBS, 'asc', function(err, jobs) {
if (err) return callback(err);
else {
var counter = jobs.length;
if (counter == 0)
return callback();
jobs.forEach(function(job) {
var valid = (parseInt(job.created_at) + CLEANUP_MAX_COMPLETE_TIME);
if (now > valid) {
job.remove();
console.log('removed active job %s', job.id);
}
else
console.log('valid active %s < %s job - %s', now, valid, job.id);
counter -=1;
if (counter == 0) {
return callback();
}
});
}
});
}
],
function(err) {
if (err) throw err;
else console.log('Done..');
process.exit(0);
});