| 270 | }; |
| 271 | |
| 272 | export const pollForJobCompletion = async ( |
| 273 | client: ReturnType<typeof createApiClient>, |
| 274 | clusterId: string, |
| 275 | jobId: string, |
| 276 | initialStatus?: string | null, |
| 277 | initialResult: string = "", |
| 278 | initialResultType: string = "rejection", |
| 279 | ): Promise<{ |
| 280 | status: string; |
| 281 | result: string; |
| 282 | resultType: string; |
| 283 | }> => { |
| 284 | let status: string | null = initialStatus ?? null; |
| 285 | let result: string = initialResult; |
| 286 | let resultType: string = initialResultType; |
| 287 | |
| 288 | while (!status || !["failure", "done"].includes(status)) { |
| 289 | const details = await client.getJob({ |
| 290 | params: { clusterId, jobId }, |
| 291 | query: { waitTime: 20 }, |
| 292 | }); |
| 293 | |
| 294 | if (details.status !== 200) { |
| 295 | throw new AgentRPCError(`Failed to fetch job details: ${details.status}`); |
| 296 | } |
| 297 | |
| 298 | const body = details.body; |
| 299 | status = body.status; |
| 300 | result = body.result || ""; |
| 301 | resultType = body.resultType || "rejection"; |
| 302 | |
| 303 | await new Promise((resolve) => setTimeout(resolve, 1000)); |
| 304 | } |
| 305 | |
| 306 | return { status: status as string, result, resultType }; |
| 307 | }; |
| 308 | |
| 309 | export const createAndPollJob = async ( |
| 310 | client: ReturnType<typeof createApiClient>, |