1- import cron from "node-cron" ;
21import { getBlockTimeSeconds } from "../../shared/utils/indexer/get-block-time" ;
32import { logger } from "../../shared/utils/logger" ;
43import { handleContractSubscriptions } from "../tasks/chain-indexer" ;
54import { env } from "../../shared/utils/env" ;
5+ import { CronJob } from "cron" ;
66
77// @TODO : Move all worker logic to Bullmq to better handle multiple hosts.
8- export const INDEXER_REGISTRY = { } as Record < number , cron . ScheduledTask > ;
8+ export const INDEXER_REGISTRY : Record < number , CronJob > = { } ;
99
1010export const addChainIndexer = async ( chainId : number ) => {
1111 if ( INDEXER_REGISTRY [ chainId ] ) {
1212 return ;
1313 }
1414
15- // Estimate the block time in the last 100 blocks. Default to 2 second block times.
16- let blockTimeSeconds : number ;
17- try {
18- blockTimeSeconds = await getBlockTimeSeconds ( chainId , 100 ) ;
19- } catch ( error ) {
20- logger ( {
21- service : "worker" ,
22- level : "error" ,
23- message : `Could not estimate block time for chain ${ chainId } ` ,
24- error,
25- } ) ;
26- blockTimeSeconds = 2 ;
15+ let cronSchedule = env . CONTRACT_SUBSCRIPTION_CRON_SCHEDULE_OVERRIDE ;
16+ if ( ! cronSchedule ) {
17+ // Estimate the block time in the last 100 blocks. Default to 2 second block times.
18+ let blockTimeSeconds : number ;
19+ try {
20+ blockTimeSeconds = await getBlockTimeSeconds ( chainId , 100 ) ;
21+ } catch ( error ) {
22+ logger ( {
23+ service : "worker" ,
24+ level : "error" ,
25+ message : `Could not estimate block time for chain ${ chainId } ` ,
26+ error,
27+ } ) ;
28+ blockTimeSeconds = 2 ;
29+ }
30+ cronSchedule = createScheduleSeconds ( blockTimeSeconds ) ;
2731 }
28- const cronSchedule = createScheduleSeconds ( {
29- blockTimeSeconds,
30- numBlocks : env . CONTRACT_SUBSCRIPTION_BLOCK_RANGE ,
31- } ) ;
3232 logger ( {
3333 service : "worker" ,
3434 level : "info" ,
@@ -37,7 +37,7 @@ export const addChainIndexer = async (chainId: number) => {
3737
3838 let inProgress = false ;
3939
40- const task = cron . schedule ( cronSchedule , async ( ) => {
40+ const task = new CronJob ( cronSchedule , async ( ) => {
4141 if ( inProgress ) {
4242 return ;
4343 }
@@ -58,6 +58,7 @@ export const addChainIndexer = async (chainId: number) => {
5858 } ) ;
5959
6060 INDEXER_REGISTRY [ chainId ] = task ;
61+ task . start ( ) ;
6162} ;
6263
6364export const removeChainIndexer = async ( chainId : number ) => {
@@ -76,17 +77,7 @@ export const removeChainIndexer = async (chainId: number) => {
7677 delete INDEXER_REGISTRY [ chainId ] ;
7778} ;
7879
79- /**
80- * Returns the cron schedule given the chain's block time and the number of blocks to batch per job.
81- * Minimum is every 2 seconds.
82- */
83- function createScheduleSeconds ( {
84- blockTimeSeconds,
85- numBlocks,
86- } : { blockTimeSeconds : number ; numBlocks : number } ) {
87- const pollFrequencySeconds = Math . max (
88- Math . round ( blockTimeSeconds * numBlocks ) ,
89- 2 ,
90- ) ;
91- return `*/${ pollFrequencySeconds } * * * * *` ;
80+ function createScheduleSeconds ( blockTimeSeconds : number ) {
81+ const pollIntervalSeconds = Math . max ( Math . round ( blockTimeSeconds ) , 2 ) ;
82+ return `*/${ pollIntervalSeconds } * * * * *` ;
9283}
0 commit comments