-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsender.js
71 lines (62 loc) · 1.82 KB
/
sender.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
process.env.RETHINKDB_URL = "rethinkdb://207.38.84.54/gambling";
process.env.RABBITMQ_URL = "amqp://test:[email protected]";
require('coffee-script/register')
const db = require('seokit-db');
const debug = require('debug')(__filename);
import autobind from 'autobind-decorator'
const stream = require('stream');
const util = require('util');
const Writable = stream.Writable;
const amqp = require('amqplib');
const RABBITMQ_URL = process.env.RABBITMQ_URL || 'amqp://localhost';
import {EventEmitter} from 'events'
@autobind
class Sender {
channelName = 'wordpress';
maxCount = 1000;
constructor() {
this.createSendStream();
this.start().catch(console.log)
}
async start() {
this.connection = await amqp.connect(RABBITMQ_URL);
this.channel = await this.connection.createChannel();
this.channel.assertQueue(this.channelName, {durable: true}).then(console.log);
db('Domain',model=>{
let reader = model.find().stream();
reader.pipe(this.sendStream).on('finish', _=> {
console.log('Done');
});
});
}
createSendStream() {
function SendStream(options) {
Writable.call(this, options);
};
util.inherits(SendStream, Writable);
let i = 0;
SendStream.prototype._write = (website, enc, callback) => {
let msg = JSON.stringify(website);
this.channel.sendToQueue(this.channelName, new Buffer(msg), {persistent: true});
if (++i > 1000) {
i = 0;
this.checkQueueLength(callback)
} else {
callback()
}
};
this.sendStream = new SendStream({objectMode: true});
}
async checkQueueLength(callback) {
let {messageCount} = await this.channel.checkQueue(this.channelName);
console.log('checkQueueLength',messageCount);
if (messageCount === undefined || messageCount >= this.maxCount) {
setTimeout(_=>{
this.checkQueueLength(callback)
},200)
} else {
callback()
}
}
}
new Sender();