diff --git a/api/src/services/nova/nodeInfoService.ts b/api/src/services/nova/nodeInfoService.ts index 7c35a5083..7bd6d92cd 100644 --- a/api/src/services/nova/nodeInfoService.ts +++ b/api/src/services/nova/nodeInfoService.ts @@ -1,10 +1,15 @@ /* eslint-disable import/no-unresolved */ /* eslint-disable @typescript-eslint/no-unsafe-return */ import { Client, InfoResponse, ProtocolParameters } from "@iota/sdk-nova"; +import cron from "node-cron"; import { NodeInfoError } from "../../errors/nodeInfoError"; import { ServiceFactory } from "../../factories/serviceFactory"; +import logger from "../../logger"; import { INetwork } from "../../models/db/INetwork"; +// The cron interval value to update the node info every 10 minutes. +const NODE_INFO_UPDATE_INTERVAL = "*/1 * * * *"; + /** * Class to handle Nova protocol node info. */ @@ -34,6 +39,8 @@ export class NodeInfoService { this._network = network; this._nodeInfo = nodeInfo; this._client = client; + + this.setupNodeInfoUpdater(); } public static async build(network: INetwork): Promise { @@ -54,4 +61,30 @@ export class NodeInfoService { public async getProtocolParameters(): Promise { return this._client.getProtocolParameters(); } + + private setupNodeInfoUpdater() { + cron.schedule(NODE_INFO_UPDATE_INTERVAL, async () => { + await this.updateNodeInfo(); + }); + } + + private async updateNodeInfo(): Promise { + logger.debug("[NovaNodeInfoService] Updating node info..."); + const apiClient = ServiceFactory.get(`client-${this._network.network}`); + + if (apiClient) { + try { + const response = await apiClient.getNodeInfo(); + + if (response?.info) { + this._nodeInfo = response.info; + logger.verbose("[NovaNodeInfoService] Node info updated successfully"); + } + } catch (err) { + throw new NodeInfoError(`Failed to fetch node info for "${this._network.network}" with error:\n${err}`); + } + } else { + logger.warn("[NovaNodeInfoService] Couldn't update node info"); + } + } }