Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 21 additions & 2 deletions src/polling/pollScraperResults.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { logger, wait } from "@trigger.dev/sdk/v3";
import { wait } from "@trigger.dev/sdk/v3";
import { getScraperResults } from "../recoup/getScraperResults";
import { logStep } from "../utils/logStep";

// Base type with shared fields
type ScrapeRun = {
Expand All @@ -12,6 +13,8 @@ export type PollResult = ScrapeRun & {
data?: unknown[];
};

const MAX_POLL_RETRIES = 30;

/**
* Polls each scraper run in parallel until all are completed (SUCCEEDED or FAILED).
* Returns an array of results for each run.
Expand All @@ -23,14 +26,30 @@ export async function pollScraperResults(
const pendingRuns = new Map<string, ScrapeRun>(
runs.map((run) => [run.runId, run])
);
const retryCounts = new Map<string, number>();

while (pendingRuns.size > 0) {
// Poll all pending runs in parallel
const pollPromises = Array.from(pendingRuns.values()).map(async (run) => {
const result = await getScraperResults(run.runId);

if (!result) {
logger.warn("Failed to get scraper result", { runId: run.runId });
const retries = (retryCounts.get(run.runId) ?? 0) + 1;
retryCounts.set(run.runId, retries);

if (retries >= MAX_POLL_RETRIES) {
logStep("poll-scraper", "Max retries reached, marking as FAILED", { runId: run.runId, retries }, "error");
return {
run,
pollResult: {
runId: run.runId,
datasetId: run.datasetId,
status: "FAILED",
},
};
}

logStep("poll-scraper", "Failed to get scraper result", { runId: run.runId, retry: retries }, "warn");
return null;
}

Expand Down
22 changes: 22 additions & 0 deletions src/utils/logStep.ts
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove this file @recoup-coding-agent in favor of the existing logStep function file in the tasks codebase.

Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { logger } from "@trigger.dev/sdk/v3";

type LogLevel = "log" | "warn" | "error";

/**
* Logs a step with a consistent format across tasks.
* Wraps the Trigger.dev logger with a structured step name + detail pattern.
*
* @param step - Short label for the step (e.g. "poll-scraper")
* @param message - What happened
* @param detail - Optional structured metadata
* @param level - Log level: "log" | "warn" | "error" (default: "log")
*/
export function logStep(
step: string,
message: string,
detail?: Record<string, unknown>,
level: LogLevel = "log"
): void {
const payload = { step, ...detail };
logger[level](message, payload);
}