This repository was archived by the owner on Apr 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscheduler.js
60 lines (51 loc) · 1.57 KB
/
scheduler.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
var debug = require('debug');
var build_readme = require('./lib/build_readme');
var amqp = require('amqplib');
function run() {
var broker_url = process.env.RABBITMQ_URL || 'amqp://localhost';
var q = 'readme';
amqp.connect(broker_url).then(function(conn) {
process.once('SIGINT', function() { conn.close(); });
return conn.createChannel().then(function(ch) {
var ok = ch.assertQueue(q, {durable: true});
ok = ok.then(function() { ch.prefetch(1); });
ok = ok.then(function() {
ch.consume(q, doWork, {noAck: false});
});
return ok;
function doWork(msg) {
var msg_obj = JSON.parse(msg.content.toString());
console.log(msg_obj.package + " start.");
build_readme(msg_obj, function(error, html) {
if (!error) {
if (html != "") {
store_html(msg_obj.package, html);
}
console.log(msg_obj.package + " done.");
} else {
console.log(msg_obj.package + ' error.');
}
ch.ack(msg);
});
}
})
}).then(null, console.warn);
}
function store_html(package, html) {
var couch_url = process.env.DOCSDB_URL || 'http://127.0.0.1:5984';
var nano = require('nano')(couch_url);
var db = nano.db.use('readme');
var doc = { 'date': new Date().toISOString(),
'html': html };
db.update = function(obj, key, callback) {
var db = this;
db.get(key, function (error, existing) {
if(!error) obj._rev = existing._rev;
db.insert(obj, key, callback);
});
}
db.update(doc, package, function(error, response) {
if (error) { return console.log(error); }
});
}
module.exports = run;