-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhead-blaster.js
66 lines (57 loc) · 1.78 KB
/
head-blaster.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
var Queue = require('notify-queue');
var argv = require('yargs').argv;
var fs = require('fs');
var concurrency = argv.concurrency;
var completed = 0;
var errors = 0;
var objectQueue = new Queue();
if (!argv.file || !argv.concurrency || !argv.operation) {
console.error([
"Usage: node index.js --file=input.file --concurrency=2 --operation=./operations/fix-cache-headers.js > output.log",
" --file : input file with one 'bucket:key' per line",
" --concurrency : number of api calls to make in parallel (typically 2-10)",
" --operation : path to module exporting: ",
" function(bucket, key, callback) {/* do stuff */ callback(err)}",
" see operations/*",
''
].join('\n'));
process.exit(-1);
}
var operation = require(argv.operation);
out("Reading file...");
var lines = fs.readFileSync(argv.file, 'utf8').split("\n");
var count = lines.length;
lines.forEach(function(line) {
objectQueue.push(line);
});
out(" Done.\n");
out("Read " + count + " lines.\n");
for(var i=0; i<concurrency; i++) {
objectQueue.pop(processLine);
}
function processLine(line, done) {
var object = parseLine(line);
operation(object.bucket, object.key, function(err) {
completed++;
if (err) errors++;
printStatus(completed, count);
done();
});
}
function printStatus(current, count) {
var ratio = current / count * 100;
var pct = Math.round(1000*ratio) / 1000 + "%";
out("\rProgress: " + pct + " (" + current + ' / ' + count + ") Errors: " + errors);
}
function parseLine(line) {
var sep = line.indexOf(':');
var bucket = line.substring(0,sep);
var key = line.substring(sep+1);
return {
bucket: bucket,
key: key
};
}
function out(str) {
process.stderr.write(str);
}