diff --git a/src/indexers/hosted/hosted-indexers.test.ts b/src/indexers/hosted/hosted-indexers.test.ts index 3417a67..40bb20c 100644 --- a/src/indexers/hosted/hosted-indexers.test.ts +++ b/src/indexers/hosted/hosted-indexers.test.ts @@ -1,12 +1,9 @@ -import { setTimeout } from "timers/promises"; import { expect, test, beforeEach, afterEach } from "vitest"; import { Catalog, CatalogConfig } from "../../catalog"; import { testClient } from "../../vitest-test-client"; import { GithubDataSourceConfig, Indexer, - IndexerExecutionHistory, - IndexerExecutionResult, IndexerScheduleFrequency, WebScraperDataSourceConfig, } from "./indexer"; @@ -44,28 +41,15 @@ test("Test hosted indexer APIs", { timeout: 60000 }, async () => { const retrievedIndexer: Indexer = await testClient.getIndexer(indexerName); expect(retrievedIndexer.config).toMatchObject(indexer.config); - await indexer.run(); // start the run and do a few other things before checking status so the indexer has time to run - const list = await testClient.listIndexers(); expect(list).toHaveLength(1); expect(list[0].config).toMatchObject(indexer.config); - let executionResult: IndexerExecutionResult | undefined; - while (!executionResult) { - const history: IndexerExecutionHistory = - await indexer.getExecutionHistory(); - if ( - history && - history.results.length > 0 && - history.results[0].status !== "inProgress" - ) { - executionResult = history.results[0]; - } else { - await setTimeout(500); - } - } + const executionResult = await indexer.runAndWaitForCompletion({ + timeoutMs: 60000, + }); - expect(executionResult!.status).toBe("success"); + expect(executionResult.status).toBe("success"); expect(executionResult!.errors).toHaveLength(0); expect(executionResult!.warnings).toHaveLength(0); diff --git a/src/indexers/hosted/indexer.ts b/src/indexers/hosted/indexer.ts index f39a64a..aa4ccca 100644 --- a/src/indexers/hosted/indexer.ts +++ b/src/indexers/hosted/indexer.ts @@ -1,3 +1,4 @@ +import { setTimeout } from "timers/promises"; import { CortexApiClient } from "../../api-client"; export type IndexerExecutionStatus = "success" | "failure" | "inProgress"; @@ -65,6 +66,10 @@ export type IndexerConfig = { schedule: IndexerSchedule; }; +export type RunAndWaitForCompletionOptions = { + timeoutMs?: number; +}; + export class Indexer { private constructor( readonly config: IndexerConfig, @@ -138,6 +143,33 @@ export class Indexer { } } + async runAndWaitForCompletion( + options?: RunAndWaitForCompletionOptions, + ): Promise { + await this.run(); + + const timeoutMs = options?.timeoutMs ?? 30000; + const startTime = performance.now(); + let executionResult: IndexerExecutionResult | undefined; + while (!executionResult) { + const history: IndexerExecutionHistory = + await this.getExecutionHistory(); + if ( + history && + history.results.length > 0 && + history.results[0].status !== "inProgress" + ) { + executionResult = history.results[0]; + } else { + if (performance.now() - startTime > timeoutMs) { + throw new Error("Timed out waiting for indexer to complete"); + } + await setTimeout(500); + } + } + return executionResult; + } + async getExecutionHistory(): Promise { const res = await this.apiClient.GET( `/indexers/${this.config.name}/history`,