forked from eco-stake/cosmos-directory
-
Notifications
You must be signed in to change notification settings - Fork 1
/
chainWorker.js
84 lines (73 loc) · 2.98 KB
/
chainWorker.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
import Bugsnag from "@bugsnag/js"
import ChainRegistry from './chains/chainRegistry.js';
import Repository from './repository/repository.js';
import HealthMonitor from './status/healthMonitor.js';
import { redisClient } from "./redisClient.js";
import ChainMonitor from "./chains/chainMonitor.js";
import JumperRegistryRepository from "./repository/jumperRegistryRepository.js";
const chainUrl = process.env.CHAIN_URL || 'https://github.com/cosmos/chain-registry'
const chainBranch = process.env.CHAIN_BRANCH || 'master'
const chainPath = process.env.CHAIN_PATH
const repoRefreshSeconds = parseInt(process.env.REPO_REFRESH || 60 * 15)
const validatorUrl = process.env.VALIDATOR_URL || 'https://github.com/eco-stake/validator-registry'
const validatorBranch = process.env.VALIDATOR_BRANCH || 'master'
const chainRefreshSeconds = parseInt(process.env.CHAIN_REFRESH || 60 * 5)
const healthRefreshSeconds = parseInt(process.env.HEALTH_REFRESH || 10)
console.log("Using config:", {
chainUrl,
chainBranch,
repoRefreshSeconds,
validatorUrl,
validatorBranch,
chainRefreshSeconds,
healthRefreshSeconds,
})
if(process.env.BUGSNAG_KEY){
Bugsnag.start({
apiKey: process.env.BUGSNAG_KEY,
enabledReleaseStages: ['production', 'staging'],
releaseStage: process.env.NODE_ENV
})
}
async function queueHealthCheck(client, registry, health) {
setTimeout(async () => {
const chains = await registry.getChains()
await health.refreshApis(client, chains)
queueHealthCheck(client, registry, health)
}, 1000 * healthRefreshSeconds)
}
async function queueChainCheck(client, registry, monitor) {
setTimeout(async () => {
const chains = await registry.getChains()
await monitor.refreshChains(client, chains)
queueChainCheck(client, registry, monitor)
}, 1000 * chainRefreshSeconds)
}
(async () => {
const client = await redisClient();
const chainRepo = JumperRegistryRepository(client, chainUrl, chainBranch, { exclude: ['_template'], require: 'chain.json' })
const validatorRepo = Repository(client, validatorUrl, validatorBranch, { require: 'chains.json', storeMeta: async (name, allData) => {
await client.json.set([name, 'addresses'].join(':'), '$', allData.reduce((sum, validator) => {
for(const chain of validator.chains.chains){
sum[chain.address] = validator.path
}
return sum
}, {}))
} })
await chainRepo.refresh()
setInterval(() => chainRepo.refresh(), 1000 * repoRefreshSeconds)
await validatorRepo.refresh()
setInterval(() => validatorRepo.refresh(), 1000 * repoRefreshSeconds)
const chainRegistry = ChainRegistry(client)
const chains = await chainRegistry.getChains()
const healthMonitor = HealthMonitor()
await healthMonitor.refreshApis(client, chains)
if (healthRefreshSeconds > 0) {
queueHealthCheck(client, chainRegistry, healthMonitor)
}
const chainMonitor = ChainMonitor()
chainMonitor.refreshChains(client, chains)
if (chainRefreshSeconds > 0) {
queueChainCheck(client, chainRegistry, chainMonitor)
}
})();