-
Notifications
You must be signed in to change notification settings - Fork 83
/
cron.js
95 lines (89 loc) · 3.32 KB
/
cron.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
// Configure logger
if (process.env.NODE_ENV === "production") require("./configs/logger");
// Configure globals
GLOBAL.appConfig = require("./configs/config");
var fs = require("fs");
var request = require("request");
var CronJob = require("cron").CronJob;
var cronCurrencyPath = "./cron_dump/currency.dump";
var cronCurrencyExcludePath = "./cron_dump/currency_exclude.dump";
var excludedCurrencies = fs.readFileSync(cronCurrencyExcludePath).toString().split("\n");
var MarketHelper = require("./lib/market_helper");
var currencies = MarketHelper.getCurrencyTypes();
var transactionsInProgress = false;
var paymentsInProgress = false;
var tradeStatsInProgress = false;
var nextCurrencyIndex, nextCurrency;
var transactionsJob = new CronJob({
cronTime: "*/3 * * * * *",
onTick: function() {
if (!transactionsInProgress) {
transactionsInProgress = true;
nextCurrency = fs.readFileSync(cronCurrencyPath).toString();
if (excludedCurrencies.indexOf(nextCurrency) === -1) {
var url = "http://" + GLOBAL.appConfig().wallets_host + "/load_latest_transactions/" + nextCurrency;
var statusUrl = "http://" + GLOBAL.appConfig().wallets_host + "/wallet_health/" + nextCurrency;
request.post(url, function (err, httpResponse, body) {
if (err) {
console.error(nextCurrency, "Error loading transactions.", err);
} else {
console.log(nextCurrency, body);
}
request.get(statusUrl, function (err, httpResponse, body) {
if (err) {
console.error(nextCurrency, "Error updating wallet status.", err);
}
nextCurrencyIndex = currencies.indexOf(nextCurrency);
nextCurrencyIndex = currencies[nextCurrencyIndex + 1] ? nextCurrencyIndex + 1 : 0;
nextCurrency = currencies[nextCurrencyIndex];
fs.writeFileSync(cronCurrencyPath, nextCurrency);
transactionsInProgress = false;
});
});
} else {
nextCurrencyIndex = currencies.indexOf(nextCurrency);
nextCurrencyIndex = currencies[nextCurrencyIndex + 1] ? nextCurrencyIndex + 1 : 0;
nextCurrency = currencies[nextCurrencyIndex];
fs.writeFileSync(cronCurrencyPath, nextCurrency);
transactionsInProgress = false;
}
}
},
start: false
});
transactionsJob.start();
var paymentsJob = new CronJob({
cronTime: "0 */5 * * * *",
onTick: function() {
paymentsInProgress = true;
var url = "http://" + GLOBAL.appConfig().wallets_host + "/process_pending_payments";
request.post(url, function (err, httpResponse, body) {
if (err) {
console.error("Could not process payments.", err);
} else {
console.log("Processed payments: ", body);
}
paymentsInProgress = false;
});
},
start: false
});
paymentsJob.start();
var tradeStatsJob = new CronJob({
cronTime: "0 */30 * * * *",
onTick: function() {
tradeStatsInProgress = true;
var url = "http://" + GLOBAL.appConfig().wallets_host + "/trade_stats";
request.post(url, function (err, httpResponse, body) {
if (err) {
console.error("Could not aggregate trade stats.", err);
} else {
console.log("Trade stats: ", body);
}
tradeStatsInProgress = false;
});
},
start: false
});
tradeStatsJob.start();
console.log("Processing jobs...");