1
- import { Job , Processor , Worker } from "bullmq" ;
2
- import { Address , getAddress } from "thirdweb" ;
1
+ import { Worker , type Job , type Processor } from "bullmq" ;
2
+ import { getAddress , type Address } from "thirdweb" ;
3
3
import {
4
4
getUsedBackendWallets ,
5
5
inspectNonce ,
@@ -19,6 +19,9 @@ const CHECK_PERIODS = 3;
19
19
// Frequency of the worker
20
20
const RUN_FREQUENCY_SECONDS = 60 ; // Run every minute
21
21
22
+ // The number of wallets to check in parallel.
23
+ const BATCH_SIZE = 500 ;
24
+
22
25
// Interfaces
23
26
interface NonceState {
24
27
onchainNonce : number ;
@@ -51,26 +54,27 @@ export const initNonceHealthCheckWorker = () => {
51
54
const handler : Processor < any , void , string > = async ( job : Job < string > ) => {
52
55
const allWallets = await getUsedBackendWallets ( ) ;
53
56
54
- const walletHealthPromises = allWallets . map (
55
- async ( { chainId, walletAddress } ) => {
56
- const [ _ , isStuck , currentState ] = await Promise . all ( [
57
- updateNonceHistory ( walletAddress , chainId ) ,
58
- isQueueStuck ( walletAddress , chainId ) ,
59
- getCurrentNonceState ( walletAddress , chainId ) ,
60
- ] ) ;
61
-
62
- return {
63
- walletAddress,
64
- chainId,
65
- isStuck,
66
- onchainNonce : currentState . onchainNonce ,
67
- largestSentNonce : currentState . largestSentNonce ,
68
- } ;
69
- } ,
70
- ) ;
57
+ for ( let i = 0 ; i < allWallets . length ; i += BATCH_SIZE ) {
58
+ const batch = allWallets . slice ( i , i + BATCH_SIZE ) ;
59
+
60
+ await Promise . all (
61
+ batch . map ( async ( { chainId, walletAddress } ) => {
62
+ const [ _ , isStuck , currentState ] = await Promise . all ( [
63
+ updateNonceHistory ( walletAddress , chainId ) ,
64
+ isQueueStuck ( walletAddress , chainId ) ,
65
+ getCurrentNonceState ( walletAddress , chainId ) ,
66
+ ] ) ;
67
+
68
+ logger ( {
69
+ service : "worker" ,
70
+ level : isStuck ? "fatal" : "info" ,
71
+ message : `[WALLET_HEALTH] ${ walletAddress } :${ chainId } isStuck:${ isStuck } onchainNonce:${ currentState . onchainNonce } largestSentNonce:${ currentState . largestSentNonce } ` ,
72
+ } ) ;
73
+ } ) ,
74
+ ) ;
71
75
72
- const walletHealthResults = await Promise . all ( walletHealthPromises ) ;
73
- logWalletHealth ( walletHealthResults , job ) ;
76
+ await sleep ( 500 ) ;
77
+ }
74
78
} ;
75
79
76
80
// Check if a queue is stuck
@@ -145,21 +149,5 @@ async function updateNonceHistory(walletAddress: Address, chainId: number) {
145
149
. exec ( ) ;
146
150
}
147
151
148
- // Log wallet health
149
- function logWalletHealth ( healthResults : WalletHealth [ ] , job : Job < string > ) {
150
- healthResults . forEach ( ( result ) => {
151
- const message =
152
- `[WALLET_HEALTH] ${ result . walletAddress } :${ result . chainId } ` +
153
- `isStuck:${ result . isStuck } ` +
154
- `onchainNonce:${ result . onchainNonce } ` +
155
- `largestSentNonce:${ result . largestSentNonce } ` ;
156
-
157
- logger ( {
158
- service : "worker" ,
159
- level : result . isStuck ? "fatal" : "info" ,
160
- message,
161
- } ) ;
162
- } ) ;
163
-
164
- job . log ( JSON . stringify ( healthResults ) ) ;
165
- }
152
+ const sleep = async ( ms : number ) =>
153
+ new Promise ( ( resolve ) => setTimeout ( resolve , ms , null ) ) ;
0 commit comments