forked from mikeal/relaximation
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompact_all.js
More file actions
57 lines (53 loc) · 1.53 KB
/
compact_all.js
File metadata and controls
57 lines (53 loc) · 1.53 KB
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
try {
require('request');
} catch(e) {
require('sys').debug('You must install request http://github.com/mikeal/node-utils/tree/master/request/')
process.exit();
}
var request = require('request')
, sys = require('sys');
if (process.argv[process.argv.length - 1].slice(0, 4) === 'http') {
var uri = process.argv[process.argv.length - 1];
sys.debug('DBURI '+uri)
} else {
var uri = 'http://localhost:5984/'
sys.debug('No DBURI was set, using '+uri)
}
if (!uri[uri.length - 1] === '/') uri += '/'
request({uri:uri + '_all_dbs'}, function (error, resp, body) {
if (error) throw error
var dbs = JSON.parse(body)
if (!dbs.length) throw "Did not get databases "+body;
var i = 0;
var compact = function () {
if (i === dbs.length) {
sys.debug('All compactions finished');
return;
}
var self = this;
var db = dbs[i];
sys.debug('Starting compaction of '+db)
request({uri:uri + db + '/_compact', method:"POST"}, function (error, resp, body) {
if (error) throw error
if (body !== '{"ok":true}\n') {
sys.debug('Could not start compaction '+body);
i += 1;
compact();
}
var check = function () {
request({uri:uri + db}, function (error, resp, body) {
if (error) throw error
if (JSON.parse(body).compact_running === true) {
setTimeout(check, 100)
} else {
sys.debug('Compaction finished.')
i += 1
compact()
}
})
}
check();
})
}
compact();
})