forked from JheSue/atomic-gravity
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgravity-subscriber.js
More file actions
245 lines (204 loc) · 5.26 KB
/
gravity-subscriber.js
File metadata and controls
245 lines (204 loc) · 5.26 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
// const { setTimeout } = require('timers/promises');
module.exports = function(RED) {
function SubscriberNode(config) {
const util = require('util');
RED.nodes.createNode(this, config);
var node = this;
// Getting server information from gravity server node
this.server = RED.nodes.getNode(config.server)
function setStatus(type) {
switch(type) {
case 'connected':
node.status({
fill: 'green',
shape: 'dot',
text: 'connected'
});
break;
case 'connecting':
node.status({
fill: 'yellow',
shape: 'ring',
text: 'connecting'
});
break;
case 'registering':
node.status({
fill: 'yellow',
shape: 'ring',
text: 'registering'
});
break;
case 'initializing':
node.status({
fill: 'yellow',
shape: 'ring',
text: 'initializing'
});
break;
case 'disconnected':
node.status({
fill: 'red',
shape: 'ring',
text: 'disconnected'
});
break;
case 'receiving':
node.status({
fill: 'blue',
shape: 'ring',
text: 'receiving'
});
break;
case 'productNotFound':
node.status({
fill: 'red',
shape: 'ring',
text: 'ERR: product not found'
});
break;
}
}
function ack() {
this.ack();
setStatus('connected');
}
setStatus('disconnected');
let uuid = require('uuid');
let Gravity = require('gravity-sdk');
let batchSize;
let batchArr = [];
let timerId = null;
let timeout = config.timeout;
let currentMsgAck = null;
function setTimer(){
if (timerId){
clearTimeout(timerId);
}
timerId = setTimeout( ()=>{
if (batchArr.length > 0){
node.send({payload:batchArr,ack:currentMsgAck.ack});
}
},timeout*1000)
}
function resetTimer(){
if (timerId){
clearTimeout(timerId);
}
setTimer();
}
async function initSubscriber(client) {
// Not specify product
if (!config.product || config.product.length == 0) {
setStatus('connected');
return;
}
setStatus('initializing');
// Subscribe to product
let product = await client.getProduct(config.product);
let subOpts = {
seq: Number(config.startseq) || 1,
delivery: config.delivery || 'new'
};
if (config.delivery == 'startSeq') {
node.log(util.format('Initializing subscriber (domain=%s, product=%s, delivery=%s, seq=%d)', client.opts.domain, product.name, subOpts.delivery, subOpts.seq));
} else {
node.log(util.format('Initializing subscriber (domain=%s, product=%s, delivery=%s)', client.opts.domain, product.name, subOpts.delivery));
}
if (config.batchMode){
batchSize = 1000;
node.log(util.format('Batch Mode is On and timeout is %d',timeout));
}
let sub = await product.subscribe([], subOpts);
sub.on('event', (m) => {
setStatus('receiving');
let subPayload = {
seq: m.seq,
eventName: m.data.eventName,
table: m.data.table,
primaryKeys: m.data.primaryKeys,
primaryKey: m.data.primaryKey,
time: m.time,
timeNano: m.timeNano,
record: m.data.record,
}
currentMsgAck = {
ack:ack.bind(m),
};
if(config.batchMode){
let payload = {
subPayload:subPayload,
natsMsgId: m.seq.toString()
}
batchArr.push(payload);
if (batchArr.length >= batchSize){
if(timerId){
clearTimeout(timerId);
timerId = null;
}
node.send({payload:batchArr,ack:ack.bind(m)});
batchArr = [];
}else{
ack.bind(m)();
}
resetTimer();
}else{
node.send({payload:subPayload,natsMsgId: m.seq.toString(),ack:ack.bind(m)});
}
if (!config.manuallyAck)
ack.bind(m)();
});
node.log(util.format('Subscriber is ready (domain=%s, product=%s, delivery=%s, batchMode=%s)', client.opts.domain, product.name, subOpts.delivery,config.batchMode));
node.on('close', async () => {
node.log(util.format('Closing subscriber (domain=%s, product=%s, delivery=%s)', client.opts.domain, product.name, subOpts.delivery));
await sub.unsubscribe();
});
setStatus('connected');
}
if (!node.server) {
setStatus('disconnected');
return;
}
let accessToken = '';
if (node.credentials) {
accessToken = node.credentials.accessToken || '';
}
let client = new Gravity.Client({
servers: node.server.server + ':' + node.server.port,
domain: config.domain || 'default',
token: accessToken,
waitOnFirstConnect: true
});
node.gravityClient = client;
// Initializing event handlers
client.on('disconnect', () => {
setStatus('disconnected');
});
client.on('reconnect', () => {
node.log('Reconnecting to Gravity... ' + client.opts.servers);
setStatus('connecting');
});
client.on('connected', () => {
setStatus('connected');
initSubscriber(client).catch((e) => {
node.error(e);
setStatus('productNotFound');
});
});
// Connect to gravity
setStatus('connecting');
node.log('Connecting to Gravity... ' + client.opts.servers);
client.connect();
node.on('close', async () => {
node.log('Close current connection');
setStatus('disconnected');
client.disconnect();
});
}
RED.nodes.registerType('Gravity Subscriber', SubscriberNode, {
credentials: {
appID: { type: 'text' },
accessToken: { type: 'text' }
}
});
}