-
Notifications
You must be signed in to change notification settings - Fork 4
/
FeedSource.js
81 lines (75 loc) · 3.24 KB
/
FeedSource.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
const fs = require('fs-extra');
const path = require('path');
const os = require('os');
const chalk = require('chalk');
const argv = require('minimist')(process.argv.slice(2));
const Logger = require('../lib/Logger.js');
let logger = new Logger(argv['d']);
class FeedSource {
constructor(config) {
this.options = {
scaleVolumeBy: 1.0,
enable: true,
allowFailure: true,
timeout: 5,
quotes: [],
bases: []
};
logger.transient(chalk.white.bold('Source: ' + this.__proto__.constructor.name) + ' - Initialising...');
this.options = Object.assign(this.options, config);
}
async fetch() {
try {
logger.transient(chalk.white.bold('Source: ' + this.__proto__.constructor.name) + ' - Getting data from source...');
var feed = await this._fetch();
logger.transient(chalk.white.bold('Source: ' + this.__proto__.constructor.name) + ' - Updating cache...');
this.updateCache(feed);
logger.verbose(chalk.white.bold('Source: ' + this.__proto__.constructor.name) + ' - Source loaded.');
return feed;
} catch (e) {
logger.warning(chalk.white.bold('Source: ' + this.__proto__.constructor.name) + ' - Could not load live data. Trying to recover from cache.');
if (this.options.allowFailure != true) {
logger.warning(chalk.white.bold('Source: ' + this.__proto__.constructor.name) + ' - Exiting due to source importance (allowFailure is false).');
process.exit(1);
}
}
try {
let cached = this.recoverFromCache();
logger.verbose(chalk.white.bold('Source: ' + this.__proto__.constructor.name) + ' - Recovered from cache.');
return cached;
} catch (e) {
logger.warning(chalk.white.bold('Source: ' + this.__proto__.constructor.name) + ' - Unable to fetch live or cached data. Skipping.');
}
}
today() {
return (new Date()).toISOString().substr(0, 10);
}
recoverFromCache() {
var cacheFile = this.getCacheFilename();
//console.log('this: '+cacheFile);
try {
return JSON.parse(fs.readFileSync(cacheFile, 'utf8'));
} catch (e) {
logger.warning(chalk.white.bold('Source: ' + this.__proto__.constructor.name) + ' - Could not open cache file.');
throw('Could not open cache file');
}
}
getCacheFilename() {
var cachePath = path.join(os.homedir(), 'bitshares-pricefeed', 'cache', this.__proto__.constructor.name);
fs.ensureDirSync(cachePath);
return path.join(cachePath, this.today() + '.json');
}
updateCache(feed) {
var cacheFile = this.getCacheFilename();
//console.log('here'+ cacheFile);
try {
fs.writeFileSync(cacheFile, JSON.stringify(feed));
logger.verbose(chalk.white.bold('Source: ' + this.__proto__.constructor.name) + ' - Cache file updated.');
return true;
} catch (e) {
logger.warning(chalk.white.bold('Source: ' + this.__proto__.constructor.name) + ' - Could not update cache file.');
return false;
}
}
}
module.exports = FeedSource;