-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
69 lines (59 loc) · 1.9 KB
/
index.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
require('dotenv').config();
const axios = require('axios');
const { CronJob } = require('cron');
const db = require('./database/Mongo.database');
const { initApi } = require('./api');
const { check } = require('./service/check.service');
const { notify } = require('./service/notifications/discord.notifications');
const {
ONYX_MARKETS_URL,
CRON_ARCHIVE_INTERVAL,
CRON_CHECK_INTERVAL,
} = process.env;
async function main() {
try {
await db.init();
const archiveJob = new CronJob(CRON_ARCHIVE_INTERVAL, async () => {
try {
const data = await axios.get(ONYX_MARKETS_URL);
await db.insertOne({
timestamp: new Date(),
metadata: {
sourceUrl: ONYX_MARKETS_URL,
},
data: data.data,
});
} catch (error) {
console.error(error);
notify('Archive job resulted in error' + error, 'warn');
}
});
const checkJob = new CronJob(CRON_CHECK_INTERVAL, async () => {
try {
const failedChecks = await check();
if (!failedChecks.length) {
console.log('Check successful');
} else {
for (const failedCheck of failedChecks) {
const diffHours = (failedCheck.endTimestamp - failedCheck.startTimestamp) / 1000 / 60 / 60;
const message = `${failedCheck.value}${failedCheck.symbol ? ':' + failedCheck.symbol : ''} has changed by ${failedCheck.diffPercent}% (${failedCheck.diff}) in ${diffHours} hours`;
notify(message, 'warn');
console.error(message);
}
}
} catch (error) {
console.error(error);
}
});
archiveJob.start();
console.log('Started archive markets job');
checkJob.start();
console.log('Started check job');
initApi();
notify('Onyx archive markets service started', 'info');
} catch (error) {
console.error(error);
process.exit(1);
}
}
main();