@@ -2,6 +2,7 @@ import cron from "node-cron";
22import { getBlockTimeSeconds } from "../../shared/utils/indexer/get-block-time" ;
33import { logger } from "../../shared/utils/logger" ;
44import { handleContractSubscriptions } from "../tasks/chain-indexer" ;
5+ import { env } from "../../shared/utils/env" ;
56
67// @TODO : Move all worker logic to Bullmq to better handle multiple hosts.
78export const INDEXER_REGISTRY = { } as Record < number , cron . ScheduledTask > ;
@@ -24,9 +25,10 @@ export const addChainIndexer = async (chainId: number) => {
2425 } ) ;
2526 blockTimeSeconds = 2 ;
2627 }
27- const cronSchedule = createScheduleSeconds (
28- Math . max ( Math . round ( blockTimeSeconds ) , 1 ) ,
29- ) ;
28+ const cronSchedule = createScheduleSeconds ( {
29+ blockTimeSeconds,
30+ numBlocks : env . CONTRACT_SUBSCRIPTION_BLOCK_RANGE ,
31+ } ) ;
3032 logger ( {
3133 service : "worker" ,
3234 level : "info" ,
@@ -74,5 +76,17 @@ export const removeChainIndexer = async (chainId: number) => {
7476 delete INDEXER_REGISTRY [ chainId ] ;
7577} ;
7678
77- export const createScheduleSeconds = ( seconds : number ) =>
78- `*/${ seconds } * * * * *` ;
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 } * * * * *` ;
92+ }
0 commit comments