/
index.js
74 lines (59 loc) · 2.35 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
70
71
72
73
74
"use strict"
const loglevel = 'debug';
const Logger = require('./utils/logger.js');
global.syncLogger = new Logger("address-scanner", "log/address-scanner.log", "log/address-scanner_error.log", loglevel);
global.modelOps = require('./db');
let { syncChain } = require('./chain');
const SYNC_INTERVAL_TIME = 10 * 1000;
function sleep(time) {
return new Promise(function(resolve, reject) {
setTimeout(function() {
resolve();
}, time);
})
}
async function syncMain(logger) {
logger.info("********************************** syncMain start **********************************");
let firstSyncDone = false;
while (1) {
try {
let chainType = 'MOVR';
let nodeUrl = 'https://rpc.moonriver.moonbeam.network'
let sync_interval_block_num = 1000;
logger.info("********************************** syncChain begin **********************************", "chainType is", chainType);
await syncChain(chainType, global.syncLogger, nodeUrl, sync_interval_block_num);
logger.info("********************************** syncChain done **********************************", "chainType is", chainType);
if (!firstSyncDone) {
firstSyncDone = true;
logger.info("********************************** syncChain firstSyncDone **********************************", "chainType is", chainType);
}
} catch (err) {
logger.error("syncMain failed:", err);
}
await sleep(SYNC_INTERVAL_TIME);
}
}
async function main() {
try {
global.syncLogger.info("agent start!", global.argv);
while (!global.modelOps || (global.modelOps.db && global.modelOps.db.readyState !== global.modelOps.db.states.connected)) {
global.syncLogger.warn("wait for address-scanner db connected");
await sleep(10 * 1000);
}
syncMain(global.syncLogger);
} catch (err) {
global.syncLogger.error("main start", err);
process.exit(0);
}
}
process.on('uncaughtException', error => {
// Will print "uncaughtException err is not defined"
global.syncLogger.error('uncaughtException', error.message || error);
// console.log('uncaughtException', error.message, error);
});
process.on('unhandledRejection', error => {
// Will print "unhandledRejection err is not defined"
global.syncLogger.error('unhandledRejection', error.message || error);
// console.log('unhandledRejection', error.message, error);
});
main();