forked from pilif/tempalias
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgarbage_collect.js
81 lines (74 loc) · 2.11 KB
/
garbage_collect.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
require.paths.unshift('lib');
require.paths.unshift('./deps/redis-node-client/lib');
var sys = require('sys'),
tempalias = require('tempalias'),
p = tempalias.AliasProvider,
redis = require('redis').saveClient;
function prune_aliases(){
redis.keys('aliases:*', function(data){
var i, aid, qc = 0, garbage = [];
if (!data){
sys.puts('No aliases found');
prune_rate_limits();
return;
}
for (i = 0; i < data.length; i++){
(function(aid){
p.findById(aid.substr(8, aid.length), false, function(alias){
qc++;
if (!alias){
garbage.unshift(aid);
garbage.unshift('counter:'+aid.substr(8, aid.length));
}
if (qc == data.length){
if (garbage.length > 0){
sys.puts("Pruning: "+garbage.join(', '));
garbage[garbage.length] = function(data){
sys.puts(data + ' sets removed');
prune_rate_limits();
};
redis.del.apply(redis, garbage);
}else{
sys.puts("No aliases to prune");
prune_rate_limits();
}
}
});
}(""+data[i]));
}
});
}
function prune_rate_limits(){
redis.keys('lock:*', function(locks){
var i = 0, garbage = [], gc=0;
if (!locks){
sys.puts("No locks found");
redis.close();
return;
}
for (i = 0; i < locks.length; i++){
(function(lockset){
redis.get(lockset, function(exp){
gc++;
if (exp < (new Date()).getTime()){
garbage.unshift(lockset);
}
if (gc >= locks.length){
if (garbage.length > 0){
sys.puts("Pruning: "+garbage.join(', '));
garbage[garbage.length] = function(data){
sys.puts(data + ' sets removed');
redis.close();
};
redis.del.apply(redis, garbage);
}else{
sys.puts('No rate locks to prune');
redis.close();
}
}
});
}(""+locks[i]));
}
});
}
prune_aliases();