in app/lib/server/processors/ApiJobExecutor.ts [277:349]
private async pollJobCompletion(apiJobId: string, credentials: any) {
const maxAttempts = HUGGINGFACE_API.MAX_POLL_ATTEMPTS;
const pollInterval = HUGGINGFACE_API.POLL_INTERVAL;
const hfToken = credentials?.huggingfaceToken;
if (!hfToken) {
throw new Error(
"Hugging Face token is required for polling job completion"
);
}
// Use authenticated username from credentials or 'anonymous' as fallback
const username = getEffectiveUsername(credentials);
for (let attempt = 1; attempt <= maxAttempts; attempt++) {
try {
console.log(`Polling attempt ${attempt}/${maxAttempts}`);
// Construct URL with username from credentials
const statusUrl = `${HUGGINGFACE_API.BASE_URL.replace(
"/api/jobs/",
`/api/jobs/${username}`
)}/${apiJobId}`;
console.log(`🔗 Using status URL with username: ${statusUrl}`);
const statusResponse = await fetch(statusUrl, {
headers: {
Authorization: `Bearer ${hfToken}`,
},
});
if (!statusResponse.ok) {
console.warn(`⚠️ Status check failed: ${statusResponse.status}`);
await this.sleep(pollInterval);
continue;
}
const status = await statusResponse.json();
console.log(
`📊 Job status:`,
status.status || status.state || "unknown"
);
// Check if job is completed
if (this.isJobCompleted(status)) {
console.log(`✅ Job completed successfully`);
return await this.getJobOutput(apiJobId, credentials);
}
// Check if job failed
if (this.isJobFailed(status)) {
throw new Error(
`Job failed with status: ${status.status || status.status.stage}`
);
}
// Continue polling
await this.sleep(pollInterval);
} catch (error) {
console.error(
`❌ Error during polling attempt ${attempt}:`,
(error as Error).message
);
if (attempt === maxAttempts) {
throw error;
}
await this.sleep(pollInterval);
}
}
throw new Error(
"Job polling timeout - job did not complete within expected time"
);
}