Skip to content

Commit

Permalink
Add runAndWaitForCompletion API
Browse files Browse the repository at this point in the history
  • Loading branch information
chaosrealm committed Nov 11, 2024
1 parent db50801 commit 124f25d
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 20 deletions.
24 changes: 4 additions & 20 deletions src/indexers/hosted/hosted-indexers.test.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -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);

Expand Down
32 changes: 32 additions & 0 deletions src/indexers/hosted/indexer.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { setTimeout } from "timers/promises";
import { CortexApiClient } from "../../api-client";

export type IndexerExecutionStatus = "success" | "failure" | "inProgress";
Expand Down Expand Up @@ -65,6 +66,10 @@ export type IndexerConfig = {
schedule: IndexerSchedule;
};

export type RunAndWaitForCompletionOptions = {
timeoutMs?: number;
};

export class Indexer {
private constructor(
readonly config: IndexerConfig,
Expand Down Expand Up @@ -138,6 +143,33 @@ export class Indexer {
}
}

async runAndWaitForCompletion(
options?: RunAndWaitForCompletionOptions,
): Promise<IndexerExecutionResult> {
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<IndexerExecutionHistory> {
const res = await this.apiClient.GET(
`/indexers/${this.config.name}/history`,
Expand Down

0 comments on commit 124f25d

Please sign in to comment.