-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.js
58 lines (46 loc) · 1.76 KB
/
main.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
"use strict";
const log = require('ololog');
const config = require('config');
require('ansicolor').nice;
const ExchangeTicketFetcher = require('./exchangeTickerFetcher');
const Persister = require('./persister');
const PromiseLimit = require('promise-limit');
let promiseLimit = new PromiseLimit(config.concurrency);
let sleep = (ms) => {
return new Promise(resolve => setTimeout(resolve, ms))
};
let processExchanges = (fetcher, persister) => {
return Promise.all(
fetcher.getAllFetchableExchangeMarkets().map((exchangeMarket) => {
return promiseLimit(() => processExchangeMarket(exchangeMarket.exchange, exchangeMarket.symbol, persister))
})
).then(results => {
log.yellow('Fetched exchange prices complete...');
// log.yellow(results)
});
};
let processExchangeMarket = (exchange, symbol, persister) => {
log.yellow('Fetching "' + symbol + '" quotes on ' + exchange.id);
return exchange.client.fetchTicker(symbol)
.then((result) => {
//log.dim(result);
persister.insertQuote(exchange.id, result);
}).catch((error) => {
log.error(exchange.id.red, error.toString().red);
exchange.failures++;
if (exchange.failures > config.maxExchangeFetchFailures) {
exchange.enabled = false
}
})
};
(async function main () {
let persister = new Persister(config.database);
await persister.init();
let fetcher = new ExchangeTicketFetcher(config.fetcher, promiseLimit);
await fetcher.init();
while(true) {
await processExchanges(fetcher, persister);
log.yellow('Waiting ' + config.waitIntervalSeconds + ' seconds');
await sleep(config.waitIntervalSeconds * 1000);
}
}) ();