-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdataFileUpdateService.js
398 lines (345 loc) · 11.6 KB
/
dataFileUpdateService.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
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
/* *********************************************************************
* This Original Work is copyright of 51 Degrees Mobile Experts Limited.
* Copyright 2023 51 Degrees Mobile Experts Limited, Davidson House,
* Forbury Square, Reading, Berkshire, United Kingdom RG1 3EU.
*
* This Original Work is licensed under the European Union Public Licence
* (EUPL) v.1.2 and is subject to its terms as set out below.
*
* If a copy of the EUPL was not distributed with this file, You can obtain
* one at https://opensource.org/licenses/EUPL-1.2.
*
* The 'Compatible Licences' set out in the Appendix to the EUPL (as may be
* amended by the European Commission) shall be deemed incompatible for
* the purposes of the Work and the provisions of the compatibility
* clause in Article 5 of the EUPL shall not apply.
*
* If using the Work as, or as part of, a network application, by
* including the attribution notice(s) required under Article 5 of the EUPL
* in the end user terms of the application under an appropriate heading,
* such notice(s) shall fulfill the requirements of that article.
* ********************************************************************* */
const fs = require('fs');
const zlib = require('zlib');
const crypto = require('crypto');
const https = require('https');
const http = require('http');
const url = require('url');
const EventEmitter = require('events');
const AutoUpdateStatus = require('./autoUpdateStatus');
const minToMs = (min) => min * 60000;
/**
* @typedef {import('fiftyone.pipeline.core').Pipeline} Pipeline
* @typedef {import('./dataFile')} DataFile
*/
/**
* Datafiles attached to FlowElements register with
* the dataFileUpdateService so the datafiles can receive
* automatic updates
**/
class DataFileUpdateService {
/**
* Constructor for a DataFileUpdateService
*
* @param {Pipeline} pipeline
* pipeline the update service is attached to
**/
constructor (pipeline) {
this.registerPipeline(pipeline);
this.eventEmitter = new EventEmitter();
}
registerPipeline (pipeline) {
this.pipeline = pipeline;
}
on (listener, callback) {
this.eventEmitter.on(listener, callback);
}
once (listener, callback) {
this.eventEmitter.once(listener, callback);
}
/**
* Method that updates a datafile when it is due an update
*
* @param {DataFile} dataFile the datafile to update
* @returns {undefined}
*/
updateDataFile (dataFile) {
const dataFileUpdateService = this;
dataFile.attemptedDownload = true;
if (dataFile.updating) {
return false;
} else {
dataFile.updating = true;
}
const urlParts = url.parse(dataFile.updateUrl);
const requestOptions = urlParts;
if (dataFile.verifyIfModifiedSince) {
try {
requestOptions.headers = {
'If-Modified-Since': dataFile.getDatePublished()
};
} catch (e) {
// getPublished might not exist if no datafile
}
}
let request;
if (urlParts.protocol === 'https:') {
request = https.get(requestOptions);
} else {
request = http.get(requestOptions);
}
request.on('response', function (response) {
if (response.statusCode !== 200) {
dataFile.updating = false;
switch (response.statusCode) {
case (429):
dataFileUpdateService.pipeline.log(
'error',
"Too many requests to '" + dataFile.updateUrl +
"' for engine '" +
dataFile.flowElement.dataKey + "'");
dataFileUpdateService.eventEmitter.emit(
'updateComplete',
AutoUpdateStatus.AUTO_UPDATE_ERR_429_TOO_MANY_ATTEMPTS,
dataFile);
break;
case (304):
dataFileUpdateService.pipeline.log(
'warn',
'No data update available from ' +
dataFile.updateUrl +
"' for engine '" +
dataFile.flowElement.dataKey + "'");
dataFileUpdateService.eventEmitter.emit(
'updateComplete',
AutoUpdateStatus.AUTO_UPDATE_NOT_NEEDED,
dataFile);
break;
case (403):
dataFileUpdateService.pipeline.log('error',
'Access denied from ' +
dataFile.updateUrl +
"' for engine '" +
dataFile.flowElement.dataKey + "'");
dataFileUpdateService.eventEmitter.emit(
'updateComplete',
AutoUpdateStatus.AUTO_UPDATE_ERR_403_FORBIDDEN,
dataFile);
break;
default:
dataFileUpdateService.pipeline.log(
'error',
'Error' + response.statusCode +
' from ' +
dataFile.updateUrl +
"' for engine '" +
dataFile.flowElement.dataKey + "'");
dataFileUpdateService.eventEmitter.emit(
'updateComplete',
AutoUpdateStatus.AUTO_UPDATE_HTTPS_ERR,
dataFile);
break;
}
dataFileUpdateService.checkNextUpdate(dataFile);
return false;
}
const filename = dataFile.tempDataDirectory +
'/' + dataFile.identifier +
Date.now();
response.pipe(fs.createWriteStream(filename));
response.on('end', function () {
// Open file
if (dataFile.verifyMD5) {
const headerMD5 = response.headers['content-md5'];
const fd = fs.createReadStream(filename);
const hash = crypto.createHash('md5');
hash.setEncoding('hex');
fd.on('end', function () {
hash.end();
if (hash.read() !== headerMD5) {
dataFileUpdateService.pipeline.log(
'error',
"MD5 doesn't match from '" +
dataFile.updateUrl +
"' for engine '" +
dataFile.flowElement.dataKey +
"'");
dataFile.updating = false;
dataFileUpdateService.checkNextUpdate(dataFile);
dataFileUpdateService.eventEmitter.emit(
'updateComplete',
AutoUpdateStatus.AUTO_UPDATE_ERR_MD5_VALIDATION_FAILED,
dataFile);
} else {
dataFileUpdateService.processFile(dataFile, filename);
}
});
fd.pipe(hash);
} else {
dataFileUpdateService.processFile(dataFile, filename);
}
});
});
}
/**
* Internal method called when the datafile has
* been downloaded and is ready after an update
*
* @param {DataFile} dataFile the datafile that is ready
* @param {string} filename the filename of the updated datafile
* @returns {undefined}
*/
fileReady (dataFile, filename) {
const dataFileUpdateService = this;
fs.readFile(filename, function (err, data) {
if (err) {
dataFileUpdateService.pipeline.log('error', err);
dataFileUpdateService.eventEmitter.emit(
'updateComplete',
AutoUpdateStatus.AUTO_UPDATE_MASTER_FILE_CANT_RENAME,
dataFile);
}
fs.writeFile(dataFile.path, data, function (err) {
if (err) {
dataFileUpdateService.pipeline.log('error', err);
dataFileUpdateService.eventEmitter.emit(
'updateComplete',
AutoUpdateStatus.AUTO_UPDATE_NEW_FILE_CANT_RENAME,
dataFile);
}
try {
dataFile.refresh();
} catch (e) {
dataFileUpdateService.pipeline.log('error', e);
dataFileUpdateService.eventEmitter.emit(
'updateComplete',
AutoUpdateStatus.AUTO_UPDATE_REFRESH_FAILED,
dataFile);
}
dataFileUpdateService.checkNextUpdate(dataFile);
dataFile.updating = false;
// Delete the temp file
fs.unlink(filename, function (err) {
if (err) {
dataFileUpdateService.pipeline.log('error', err);
dataFileUpdateService.eventEmitter.emit(
'updateComplete',
AutoUpdateStatus.AUTO_UPDATE_NEW_FILE_CANT_RENAME,
dataFile);
}
dataFileUpdateService.eventEmitter.emit(
'updateComplete',
AutoUpdateStatus.AUTO_UPDATE_SUCCESS,
dataFile);
});
});
});
}
/**
* Internal method to process the datafile has been downloaded
* Including unzipping if needed
*
* @param {DataFile} dataFile the datafile to processs
* @param {string} filename the filename of the downloaded data
*/
processFile (dataFile, filename) {
const dataFileUpdateService = this;
if (dataFile.decompress) {
fs.readFile(filename, function (err, buffer) {
if (err) {
dataFileUpdateService.pipeline.log('error', err);
dataFileUpdateService.eventEmitter.emit(
'updateComplete',
AutoUpdateStatus.AUTO_UPDATE_ERR_READING_STREAM,
dataFile);
}
zlib.gunzip(buffer, function (err, data) {
if (err) {
dataFileUpdateService.pipeline.log('error', err);
dataFileUpdateService.eventEmitter.emit(
'updateComplete',
AutoUpdateStatus.AUTO_UPDATE_ERR_READING_STREAM,
dataFile);
}
const doneFileName = filename + '_done';
fs.writeFile(doneFileName, data, function () {
dataFileUpdateService.fileReady(dataFile, doneFileName);
});
});
});
} else {
dataFileUpdateService.fileReady(dataFile, filename);
}
}
/**
* Internal method to check the next update of a
* datafile at a polling interval set on the datafile
*
* @param {DataFile} dataFile the datafile to check updates for
*/
checkNextUpdate (dataFile) {
try {
const dataFileUpdateService = this;
let interval = minToMs(
(Math.floor(Math.random() *
dataFile.updateTimeMaximumRandomisation) +
1) +
dataFile.pollingInterval
);
interval += dataFile.getNextUpdate().getMilliseconds();
// Run update on start if specified to do so
if (dataFile.updateOnStart && !dataFile.attemptedDownload) {
dataFileUpdateService.updateDataFile(dataFile);
} else {
setTimeout(function () {
dataFileUpdateService.updateDataFile(dataFile);
}, interval);
}
} catch (e) {
// Catch any extra errors with datafile updates
this.pipeline.log('error', e);
}
}
/**
* Method that registers a datafile with the update service
*
* @param {DataFile} dataFile the datafile to register
*/
registerDataFile (dataFile) {
dataFile.registered = true;
if (dataFile.updateOnStart) {
this.updateDataFile(dataFile);
} else {
if (dataFile.autoUpdate) {
this.checkNextUpdate(dataFile);
}
}
// check if fileSystemWatcher is enabled and listen for datafile changes
if (dataFile.fileSystemWatcher) {
const dataFileUpdateService = this;
const watch = function () {
fs.watch(dataFile.path, { persistent: false }, function (event) {
if (!dataFile.updating) {
dataFile.refresh();
dataFileUpdateService.eventEmitter.emit(
'updateComplete',
AutoUpdateStatus.AUTO_UPDATE_SUCCESS,
dataFile);
}
});
};
if (fs.existsSync(dataFile.path)) {
watch();
} else {
const checkWatcherCanStart = setInterval(function () {
try {
watch();
clearInterval(checkWatcherCanStart);
} catch (e) {
}
}, 500);
}
}
}
}
module.exports = DataFileUpdateService;