Introduce detailed console logging across document parsing API routes and worker operation state fetches. Logs now capture stream openings, error conditions, worker state unavailability, and invalid responses, improving observability and debuggability for PDF parsing and ONNX layout processing flows.
63 lines
1.9 KiB
TypeScript
63 lines
1.9 KiB
TypeScript
import { getWorkerClientConfigFromEnv } from '@/lib/server/compute/worker';
|
|
import type { WorkerOperationState } from '@openreader/compute-core/api-contracts';
|
|
|
|
const WORKER_OP_REQUEST_TIMEOUT_MS = 2_500;
|
|
|
|
export async function fetchWorkerOperationState<Result>(
|
|
opId: string | null | undefined,
|
|
): Promise<WorkerOperationState<Result> | null> {
|
|
const normalized = opId?.trim();
|
|
if (!normalized) return null;
|
|
|
|
let cfg: { baseUrl: string; token: string };
|
|
try {
|
|
cfg = getWorkerClientConfigFromEnv();
|
|
} catch (error) {
|
|
console.warn('[worker-op-state] worker client env missing/invalid', {
|
|
opId: normalized,
|
|
error: error instanceof Error ? error.message : String(error),
|
|
});
|
|
return null;
|
|
}
|
|
|
|
const controller = new AbortController();
|
|
const timeout = setTimeout(() => controller.abort(), WORKER_OP_REQUEST_TIMEOUT_MS);
|
|
|
|
try {
|
|
const res = await fetch(`${cfg.baseUrl}/ops/${encodeURIComponent(normalized)}`, {
|
|
method: 'GET',
|
|
headers: {
|
|
Authorization: `Bearer ${cfg.token}`,
|
|
Accept: 'application/json',
|
|
},
|
|
cache: 'no-store',
|
|
signal: controller.signal,
|
|
});
|
|
|
|
if (!res.ok) {
|
|
const detail = await res.text().catch(() => '');
|
|
console.warn('[worker-op-state] worker op request failed', {
|
|
opId: normalized,
|
|
status: res.status,
|
|
detail,
|
|
});
|
|
return null;
|
|
}
|
|
const parsed = await res.json() as WorkerOperationState<Result>;
|
|
if (!parsed || typeof parsed !== 'object' || parsed.opId !== normalized) {
|
|
console.warn('[worker-op-state] worker op response invalid', {
|
|
opId: normalized,
|
|
});
|
|
return null;
|
|
}
|
|
return parsed;
|
|
} catch (error) {
|
|
console.warn('[worker-op-state] worker op request threw', {
|
|
opId: normalized,
|
|
error: error instanceof Error ? error.message : String(error),
|
|
});
|
|
return null;
|
|
} finally {
|
|
clearTimeout(timeout);
|
|
}
|
|
}
|