-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathindex.js
executable file
·74 lines (60 loc) · 1.36 KB
/
index.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
'use strict';
const Redis = require('ioredis');
const {BigQuery} = require('@google-cloud/bigquery');
const JobPool = require('./lib/job_pool');
const Sender = require('./lib/sender');
const _ = require('lodash');
class Streamer {
constructor(config) {
this.config = config;
const services = {
redis_client: Streamer.getRedis(config),
bq_client: new BigQuery(config.gcloud)
};
this.jobPool = new JobPool({
redis_client: services.redis_client,
config: config
});
this.senders = [];
for (let i = 0; i < config.streamer.senders; i++) {
const sender = new Sender({
services: services,
config: config,
jobPool: this.jobPool
});
this.senders.push(sender);
}
}
static getRedis(config) {
return new Redis(_.merge(config.redis, {
reconnectOnError: function (err) {
const targetError = 'READONLY';
if (err.message.includes(targetError)) {
return 2;
}
},
retryStrategy: function (times) {
return 500;
}
}))
}
start() {
this.jobPool.start();
for (const sender of this.senders) {
sender.start().then(() => {}).catch(() => {});
}
}
stop() {
this.jobPool.stop();
for (const sender of this.senders) {
sender.stop();
}
}
updateConfig(config) {
this.jobPool.config = config;
for (const sender of this.senders) {
sender.config = config;
}
}
}
module.exports = Streamer;