From 9d2ac40888b4284c866ac466683933ecef2131af Mon Sep 17 00:00:00 2001 From: paulo-ocean Date: Thu, 9 Jan 2025 10:11:42 +0000 Subject: [PATCH] add process.env options for wait for indexing max retries and interval --- README.md | 12 ++++++++++++ src/commands.ts | 25 +++++++++++++++---------- src/helpers.ts | 34 ++++++++++++++++++++++++++++++++++ 3 files changed, 61 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index a263090..881a97c 100644 --- a/README.md +++ b/README.md @@ -81,6 +81,18 @@ export PROVIDER_URL='XXXX' export ADDRESS_FILE='path-to-address-file' ``` +- Optional set INDEXING_MAX_RETRIES to the max number of retries when waiting for an asset to be indexed. Default is 100 retries max. + +``` +export INDEXING_MAX_RETRIES='100' +``` + +- Optional set INDEXING_RETRY_INTERVAL to the interval (in miliseconds) for each retry when waiting for an asset to be indexed. Default is 3 seconds. + +``` +export INDEXING_RETRY_INTERVAL='3000' +``` + ### Build the TypeScript code ``` diff --git a/src/commands.ts b/src/commands.ts index bd6562c..98da26d 100644 --- a/src/commands.ts +++ b/src/commands.ts @@ -8,6 +8,8 @@ import { downloadFile, isOrderable, getMetadataURI, + getIndexingWaitSettings, + IndexerWaitParams, } from "./helpers"; import { Aquarius, @@ -34,11 +36,14 @@ export class Commands { public aquarius: Aquarius; public providerUrl: string; public macOsProviderUrl: string; + // optional settings for indexing wait time + private indexingParams: IndexerWaitParams; constructor(signer: Signer, network: string | number, config?: Config) { this.signer = signer; this.config = config || new ConfigHelper().getConfig(network); this.providerUrl = process.env.NODE_URL || process.env.PROVIDER_URL || this.config.providerUri; + this.indexingParams = getIndexingWaitSettings(); if ( !process.env.PROVIDER_URL && !process.env.NODE_URL && this.config.chainId === 8996 && @@ -142,7 +147,7 @@ export class Commands { } public async editAsset(args: string[]) { - const asset = await this.aquarius.waitForIndexer(args[1]); + const asset = await this.aquarius.waitForIndexer(args[1],null,null, this.indexingParams.retryInterval, this.indexingParams.maxRetries); if (!asset) { console.error( "Error fetching DDO " + args[1] + ". Does this asset exists?" @@ -178,7 +183,7 @@ export class Commands { public async getDDO(args: string[]) { console.log("Resolving Asset with DID: " + args[1]); - const resolvedDDO = await this.aquarius.waitForIndexer(args[1]); + const resolvedDDO = await this.aquarius.waitForIndexer(args[1],null,null, this.indexingParams.retryInterval, this.indexingParams.maxRetries); if (!resolvedDDO) { console.error( "Error fetching Asset with DID: " + @@ -189,7 +194,7 @@ export class Commands { } public async download(args: string[]) { - const dataDdo = await this.aquarius.waitForIndexer(args[1]); + const dataDdo = await this.aquarius.waitForIndexer(args[1],null,null, this.indexingParams.retryInterval, this.indexingParams.maxRetries); if (!dataDdo) { console.error( "Error fetching DDO " + args[1] + ". Does this asset exists?" @@ -258,7 +263,7 @@ export class Commands { const ddos = []; for (const dataset in inputDatasets) { - const dataDdo = await this.aquarius.waitForIndexer(inputDatasets[dataset]); + const dataDdo = await this.aquarius.waitForIndexer(inputDatasets[dataset],null,null, this.indexingParams.retryInterval, this.indexingParams.maxRetries); if (!dataDdo) { console.error( "Error fetching DDO " + dataset[1] + ". Does this asset exists?" @@ -277,7 +282,7 @@ export class Commands { ? this.macOsProviderUrl : ddos[0].services[0].serviceEndpoint; - const algoDdo = await this.aquarius.waitForIndexer(args[2]); + const algoDdo = await this.aquarius.waitForIndexer(args[2],null,null, this.indexingParams.retryInterval, this.indexingParams.maxRetries); if (!algoDdo) { console.error( "Error fetching DDO " + args[1] + ". Does this asset exists?" @@ -436,7 +441,7 @@ export class Commands { } public async computeStop(args: string[]) { - const dataDdo = await this.aquarius.waitForIndexer(args[1]); + const dataDdo = await this.aquarius.waitForIndexer(args[1],null,null, this.indexingParams.retryInterval, this.indexingParams.maxRetries); if (!dataDdo) { console.error( "Error fetching DDO " + args[1] + ". Does this asset exists?" @@ -468,7 +473,7 @@ export class Commands { } public async allowAlgo(args: string[]) { - const asset = await this.aquarius.waitForIndexer(args[1]); + const asset = await this.aquarius.waitForIndexer(args[1],null,null, this.indexingParams.retryInterval, this.indexingParams.maxRetries); if (!asset) { console.error( "Error fetching DDO " + args[1] + ". Does this asset exists?" @@ -491,7 +496,7 @@ export class Commands { ); return; } - const algoAsset = await this.aquarius.waitForIndexer(args[2]); + const algoAsset = await this.aquarius.waitForIndexer(args[2],null,null, this.indexingParams.retryInterval, this.indexingParams.maxRetries); if (!algoAsset) { console.error( "Error fetching DDO " + args[2] + ". Does this asset exists?" @@ -538,7 +543,7 @@ export class Commands { } public async disallowAlgo(args: string[]) { - const asset = await this.aquarius.waitForIndexer(args[1]); + const asset = await this.aquarius.waitForIndexer(args[1],null,null, this.indexingParams.retryInterval, this.indexingParams.maxRetries); if (!asset) { console.error( "Error fetching DDO " + args[1] + ". Does this asset exists?" @@ -603,7 +608,7 @@ export class Commands { // args[3] - agreementId const hasAgreementId = args.length === 4; - const dataDdo = await this.aquarius.waitForIndexer(args[1]); + const dataDdo = await this.aquarius.waitForIndexer(args[1],null,null, this.indexingParams.retryInterval, this.indexingParams.maxRetries); if (!dataDdo) { console.error( "Error fetching DDO " + args[1] + ". Does this asset exists?" diff --git a/src/helpers.ts b/src/helpers.ts index 4e38a11..fa89360 100644 --- a/src/helpers.ts +++ b/src/helpers.ts @@ -433,4 +433,38 @@ export function isPrivateIP(ip): boolean { } return ip } + // for waiting for an asset to index + export interface IndexerWaitParams { + maxRetries: number, + retryInterval: number + } + + // defines how much time we wait for an asset to index + the interval for retries + export function getIndexingWaitSettings(): IndexerWaitParams { + const indexingParams: IndexerWaitParams = { + maxRetries: 100, // 100 retries + retryInterval: 3000 // retries every 3 seconds + } + try { + + if(!isNaN(Number(process.env.INDEXING_RETRY_INTERVAL))) { + + indexingParams.retryInterval = Number(process.env.INDEXING_RETRY_INTERVAL) + if(indexingParams.retryInterval < 0) { + indexingParams.retryInterval = 3000 + } + } + if(!isNaN(Number(process.env.INDEXING_MAX_RETRIES))) { + + indexingParams.maxRetries = Number(process.env.INDEXING_MAX_RETRIES) + if(indexingParams.maxRetries < 0) { + indexingParams.maxRetries = 100 + } + } + }catch(err) { + console.error('Error getting indexing wait arguments:' , err) + } + + return indexingParams + } \ No newline at end of file