@@ -2,6 +2,7 @@ import cron from "node-cron";
2
2
import { getBlockTimeSeconds } from "../../shared/utils/indexer/get-block-time" ;
3
3
import { logger } from "../../shared/utils/logger" ;
4
4
import { handleContractSubscriptions } from "../tasks/chain-indexer" ;
5
+ import { env } from "../../shared/utils/env" ;
5
6
6
7
// @TODO : Move all worker logic to Bullmq to better handle multiple hosts.
7
8
export const INDEXER_REGISTRY = { } as Record < number , cron . ScheduledTask > ;
@@ -24,9 +25,10 @@ export const addChainIndexer = async (chainId: number) => {
24
25
} ) ;
25
26
blockTimeSeconds = 2 ;
26
27
}
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
+ } ) ;
30
32
logger ( {
31
33
service : "worker" ,
32
34
level : "info" ,
@@ -74,5 +76,17 @@ export const removeChainIndexer = async (chainId: number) => {
74
76
delete INDEXER_REGISTRY [ chainId ] ;
75
77
} ;
76
78
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