This repository was archived by the owner on Dec 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathatajo.events.js
More file actions
236 lines (143 loc) · 4.95 KB
/
atajo.events.js
File metadata and controls
236 lines (143 loc) · 4.95 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
const path = require('path');
const fs = require('fs');
class Events {
constructor() {
this.cache = {};
this.cachePath = path.join(__dirname, 'cache');
this.cacheTXPath = path.join(this.cachePath, 'tx');
fs.exists(this.cachePath, (exists) => {
if (!exists) {
fs.mkdirSync(this.cachePath);
fs.mkdirSync(this.cacheTXPath);
}
});
}
add(socket) {
this.socket = socket;
socket.on('disconnect', data => {
log.debug('CORE DISCONNECT - PLEASE MAKE SURE THE SUPPLIED SECRET KEY IS VALID');
process.exit(1);
});
socket.on('client:connect', data => {
log.debug('client:connect', data);
});
socket.on('client:disconnect', data => {
log.debug('client:disconnect', data);
});
socket.on('provider:rx', rx => {
rx = this.processLatency(rx, 'rx');
this.providerRx(rx);
});
}
providerRx(rx) {
try {
rx = this.verifyTransaction(rx);
if (!rx) {
log.error("EVENTS:PROVIDER:RX INVALID REQUEST", rx);
return;
}
log.debug("RX IS ", rx);
let lambda = rx.lambda;
log.debug("LAMBDA IS ", lambda);
if (this.cache[lambda]) {
log.debug("SENDING TO CACHED LAMBDA " + lambda);
//SEND REQUEST TO PROCESS
try {
this.cache[lambda].send({
action: 'rx',
request: rx
});
} catch (e) {
this.spawnHandler(lambda, rx);
}
} else {
log.debug("SPINNING UP LAMBDA " + lambda);
this.spawnHandler(lambda, rx);
}
} catch (e) {
log.error("EVENTS:PROVIDER:RX ", e);
}
}
providerTx(tx) {
try {
tx = this.verifyTransaction(tx);
if (!tx) {
log.error("EVENTS:PROVIDER:TX INVALID RESPONSE (NO PID)", rx);
return;
}
//GET THE FIRST CHUNK'S DATA
let firstChunk = path.join(this.cacheTXPath, tx.pid + '.0.chunk');
fs.readFile(firstChunk, 'utf8', (err, data) => {
if (err) {
log.error("CHUNK @ " + firstChunk + " NOT FOUND. DROPPING");
return;
}
//CREATE THE PAYLOAD
try { data = JSON.parse(data); } catch (e) { log.warn("COULD NOT PARSE CHUNK"); }
let response = {
error: tx.error,
version: tx.version,
pid: tx.pid,
response: {
chunkTotal: tx.response.chunkTotal,
chunkId: 0,
data: data
},
latency: tx.latency
}
response = this.processLatency(response, 'tx');
this.socket.emit('provider:tx', response);
});
} catch (e) {
}
}
spawnHandler(name, rx) {
let lambdaFile = path.join(__dirname, '../', 'lambdas', name + '.js');
fs.exists(lambdaFile, (exists) => {
if (!exists) {
log.error("LAMBDA FOR " + name + " (lambdas/" + name + ".js) NOT FOUND");
return;
}
try {
log.debug("FORKING PROCESS FOR " + name + "@" + lambdaFile);
let fork = require('child_process').fork;
this.cache[name] = fork(lambdaFile);
this.cache[name].send({
action: 'configure',
release: release,
domain: domain
});
this.cache[name].send({
action: 'rx',
request: rx
});
//GET RESPONSE FORM PROCESS
this.cache[name].on('message', msg => {
log.debug("GOT RESPONSE FROM HANDLER ", msg);
this.providerTx(msg);
//CREATE THE PAYLOAD
});
} catch (e) {
log.error("COULD NOT SPAWN HANDLER", e.stack);
}
})
}
verifyTransaction(obj) {
return obj.pid ? obj : false
}
processLatency(obj, type) {
try {
if (obj) {
if (type == 'rx') {
obj.latency.providerReceiveAt = new Date().getTime();
} else {
obj.latency.providerResponseAt = new Date().getTime();
}
}
} catch (e) {
log.error("COULD NOT ADD LATENCY " + type + " TO ", obj);
}
return obj;
}
}
module.exports = Events;