Send current task to server. * @param {*} [timeout=-1] Optional timeout value in ms * @returns the response from the render request. * @memberof Task
(url, timeout = -1)
| 455 | * @memberof Task |
| 456 | */ |
| 457 | async post(url, timeout = -1) { |
| 458 | if (this.status !== TaskStatus.init && this.status !== TaskStatus.pending) { |
| 459 | throw new Error(`Task status ${this.status} is not valid for post.`) |
| 460 | } |
| 461 | this._setStatus(TaskStatus.pending) |
| 462 | Object.freeze(this._reqBody) |
| 463 | |
| 464 | const abortSignal = timeout >= 0 ? AbortSignal.timeout(timeout) : undefined |
| 465 | let res = undefined |
| 466 | try { |
| 467 | this.checkReqBody() |
| 468 | do { |
| 469 | abortSignal?.throwIfAborted() |
| 470 | res = await fetch(url, { |
| 471 | method: "POST", |
| 472 | headers: { |
| 473 | "Content-Type": "application/json", |
| 474 | }, |
| 475 | body: JSON.stringify(this._reqBody), |
| 476 | signal: abortSignal, |
| 477 | }) |
| 478 | // status_code 503, already a task running. |
| 479 | } while (res.status === 503 && (await asyncDelay(RETRY_DELAY_IF_SERVER_IS_BUSY))) |
| 480 | } catch (err) { |
| 481 | this.abort(err) |
| 482 | throw err |
| 483 | } |
| 484 | if (!res.ok) { |
| 485 | const err = new Error(`Unexpected response HTTP${res.status}. Details: ${res.statusText}`) |
| 486 | this.abort(err) |
| 487 | throw err |
| 488 | } |
| 489 | return await res.json() |
| 490 | } |
| 491 | |
| 492 | static getReader(url) { |
| 493 | const reader = new ChunkedStreamReader(url) |
no test coverage detected