80 lines
2.2 KiB
TypeScript
80 lines
2.2 KiB
TypeScript
import * as dotenv from 'dotenv'
|
|
import logger from 'logger/logger'
|
|
import { RedisClient } from 'redis/RedisClient'
|
|
const envFile = process.env.NODE_ENV && process.env.NODE_ENV === 'production' ? `.env.production` : '.env.development'
|
|
dotenv.config({ path: envFile })
|
|
const scriptions = require('../config/scription_list.json')
|
|
|
|
import 'common/Extend'
|
|
import { AllChains, IChain } from 'chain/allchain'
|
|
import { BlockSyncSvr } from 'service/block.sync.service'
|
|
import { IScriptionCfg } from 'interface/IScriptionCfg'
|
|
import { buildScriptionFilters } from 'utils/block.util'
|
|
import { CheckIn } from 'models/CheckIn'
|
|
|
|
let svrs: any[] = []
|
|
let lock = false
|
|
|
|
let eventProcessers = {
|
|
CheckIn: CheckIn,
|
|
}
|
|
|
|
async function initEventSvrs() {
|
|
const cfgMap: Map<IChain, IScriptionCfg[]> = new Map()
|
|
for (let cfg of scriptions) {
|
|
if (!cfg.filter && cfg.filters) {
|
|
cfg.filter = buildScriptionFilters(cfg)
|
|
}
|
|
cfg.process = async (event: any) => {
|
|
let processer = eventProcessers[cfg.dataModel]
|
|
if (!processer) {
|
|
logger.error('processer not found: ' + cfg.dataModel)
|
|
return
|
|
}
|
|
await processer.saveEvent(event)
|
|
}
|
|
const chainCfg = AllChains.find(chain => chain.id === cfg.chain)
|
|
if (!chainCfg) {
|
|
logger.error('chainCfg not found: ' + cfg.chain)
|
|
process.exit(1)
|
|
}
|
|
if (!cfgMap.has(chainCfg)) {
|
|
cfgMap.set(chainCfg, [])
|
|
}
|
|
cfgMap.get(chainCfg)?.push(cfg)
|
|
}
|
|
for (let chainCfg of cfgMap.keys()) {
|
|
const svr = new BlockSyncSvr(chainCfg, cfgMap.get(chainCfg)!)
|
|
svrs.push(svr)
|
|
}
|
|
}
|
|
|
|
async function parseAllEvents() {
|
|
if (lock) {
|
|
logger.warn('sync in process, cancel.')
|
|
return
|
|
}
|
|
lock = true
|
|
logger.info('begin sync block: ' + svrs.map(svr => svr.chainCfg.id).join(','))
|
|
for (let svr of svrs) {
|
|
try {
|
|
await svr.execute()
|
|
} catch (err) {
|
|
logger.info('sync block with error:: chain: ' + svr.chainCfg.id)
|
|
logger.info(err)
|
|
}
|
|
}
|
|
lock = false
|
|
}
|
|
|
|
;(async () => {
|
|
let opts = { url: process.env.REDIS }
|
|
new RedisClient(opts)
|
|
logger.info('REDIS Connected')
|
|
await initEventSvrs()
|
|
setInterval(function () {
|
|
parseAllEvents()
|
|
}, 20000)
|
|
parseAllEvents()
|
|
})()
|