//native
import * as wmill from "windmill-client"
// windmill-client errors only carry the status text: throw the response body, which says why
wmill.OpenAPI.interceptors.response.use(async (response) => {
if (!response.ok) {
throw new Error(`${response.status} ${await response.text()}`)
}
return response
})
/**
* Wait for Job
* Poll a job until it completes and return its result. Throws if the job failed or did not complete within the timeout.
*/
export async function main(
id: string,
timeout_secs: number = 600,
poll_interval_secs: number = 2
) {
const deadline = Date.now() + (timeout_secs || 600) * 1000
while (true) {
const job = await wmill.JobService.getCompletedJobResultMaybe({
workspace: wmill.getWorkspace(),
id,
})
if (job.completed) {
if (!job.success) {
throw new Error(`Job ${id} failed: ${JSON.stringify(job.result)}`)
}
return job.result
}
if (Date.now() > deadline) {
throw new Error(`Job ${id} did not complete within ${timeout_secs}s`)
}
await new Promise((r) => setTimeout(r, (poll_interval_secs || 2) * 1000))
}
}
Submitted by hugo989 5 hours ago