Skip to content

Commit 20a8ac1

Browse files
committed
chore: Add warning on the monitor's failure to update the latest block data
1 parent 8dbd169 commit 20a8ac1

File tree

4 files changed

+28
-0
lines changed

4 files changed

+28
-0
lines changed

src/config/config.schema.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ const MONITOR_SCHEMA = {
100100
maximum: 120_000, // 2 minutes
101101
},
102102
blockDelay: { $ref: "positive-number-schema" },
103+
noBlockUpdateWarningInterval: { $ref: "positive-number-schema"},
103104
},
104105
additionalProperties: false
105106
}

src/config/config.types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ export interface GlobalConfig {
1313
export interface MonitorGlobalConfig {
1414
interval?: number;
1515
blockDelay?: number;
16+
noBlockUpdateWarningInterval?: number;
1617
}
1718

1819
export interface MonitorConfig extends MonitorGlobalConfig {}

src/monitor/monitor.service.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,21 @@ import { ChainConfig } from 'src/config/config.types';
1010

1111
export const DEFAULT_MONITOR_INTERVAL = 5000;
1212
export const DEFAULT_MONITOR_BLOCK_DELAY = 1;
13+
export const DEFAULT_MONITOR_NO_BLOCK_UPDATE_WARNING_INTERVAL = 5 * 60000;
1314

1415

1516
interface DefaultMonitorWorkerData {
1617
interval: number,
1718
blockDelay: number,
19+
noBlockUpdateWarningInterval: number,
1820
}
1921

2022
export interface MonitorWorkerData {
2123
chainId: string,
2224
chainName: string,
2325
rpc: string,
2426
blockDelay: number,
27+
noBlockUpdateWarningInterval: number,
2528
interval: number,
2629
loggerOptions: LoggerOptions
2730
}
@@ -79,10 +82,12 @@ export class MonitorService implements OnModuleInit {
7982

8083
const blockDelay = globalMonitorConfig.blockDelay ?? DEFAULT_MONITOR_BLOCK_DELAY;
8184
const interval = globalMonitorConfig.interval ?? DEFAULT_MONITOR_INTERVAL;
85+
const noBlockUpdateWarningInterval = globalMonitorConfig.noBlockUpdateWarningInterval ?? DEFAULT_MONITOR_NO_BLOCK_UPDATE_WARNING_INTERVAL;
8286

8387
return {
8488
interval,
8589
blockDelay,
90+
noBlockUpdateWarningInterval,
8691
}
8792
}
8893

@@ -98,6 +103,7 @@ export class MonitorService implements OnModuleInit {
98103
rpc: chainConfig.rpc,
99104
blockDelay: chainMonitorConfig.blockDelay ?? defaultConfig.blockDelay,
100105
interval: chainMonitorConfig.interval ?? defaultConfig.interval,
106+
noBlockUpdateWarningInterval: chainMonitorConfig.noBlockUpdateWarningInterval ?? defaultConfig.noBlockUpdateWarningInterval,
101107
loggerOptions: this.loggerService.loggerOptions
102108
};
103109
}

src/monitor/monitor.worker.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ class MonitorWorker {
1818
private portsCount = 0;
1919
private readonly ports: Record<number, MessagePort> = {};
2020

21+
private noBlockUpdateReferenceTimestamp = Date.now();
22+
2123
private lastBroadcastBlockNumber = -1;
2224
private latestBlock: Block | null = null;
2325

@@ -95,6 +97,22 @@ class MonitorWorker {
9597
try {
9698
const newBlock = await this.provider.getBlock(-this.config.blockDelay);
9799
if (!newBlock || newBlock.number <= this.lastBroadcastBlockNumber) {
100+
101+
const noBlockUpdateWarningElapsedTime = Date.now()
102+
- this.noBlockUpdateReferenceTimestamp;
103+
104+
if (noBlockUpdateWarningElapsedTime > this.config.noBlockUpdateWarningInterval) {
105+
this.logger.warn(
106+
{
107+
warningInterval: this.config.noBlockUpdateWarningInterval,
108+
latestBlockQuery: newBlock
109+
},
110+
`Failed to update block.`
111+
);
112+
113+
// Avoid the logs from getting filled with warnings once 'noBlockUpdateWarningInterval' has elapsed
114+
this.noBlockUpdateReferenceTimestamp = Date.now();
115+
}
98116
await wait(this.config.interval);
99117
continue;
100118
}
@@ -104,6 +122,8 @@ class MonitorWorker {
104122
);
105123

106124
this.latestBlock = newBlock;
125+
this.noBlockUpdateReferenceTimestamp = Date.now();
126+
107127
this.broadcastStatus();
108128
}
109129
catch (error) {

0 commit comments

Comments
 (0)