`io.backgroundFetch()` fetches data from a URL that can take longer that the serverless timeout. The actual `fetch` request is performed on the Trigger.dev platform, and the response is sent back to you. * @param cacheKey Should be a stable and unique key inside the `run()`. See [resumability](ht
(
cacheKey: string | any[],
url: string,
requestInit?: FetchRequestInit,
options?: {
retry?: FetchRetryOptions;
timeout?: FetchTimeoutOptions;
}
)
| 549 | * - Wildcards: 2xx, 3xx, 4xx, 5xx |
| 550 | */ |
| 551 | async backgroundFetch<TResponseData>( |
| 552 | cacheKey: string | any[], |
| 553 | url: string, |
| 554 | requestInit?: FetchRequestInit, |
| 555 | options?: { |
| 556 | retry?: FetchRetryOptions; |
| 557 | timeout?: FetchTimeoutOptions; |
| 558 | } |
| 559 | ): Promise<TResponseData> { |
| 560 | const urlObject = new URL(url); |
| 561 | |
| 562 | return (await this.runTask( |
| 563 | cacheKey, |
| 564 | async (task) => { |
| 565 | console.log("task context", task.context); |
| 566 | |
| 567 | return task.output; |
| 568 | }, |
| 569 | { |
| 570 | name: `fetch ${urlObject.hostname}${urlObject.pathname}`, |
| 571 | params: { url, requestInit, retry: options?.retry, timeout: options?.timeout }, |
| 572 | operation: "fetch", |
| 573 | icon: "background", |
| 574 | noop: false, |
| 575 | properties: [ |
| 576 | { |
| 577 | label: "url", |
| 578 | text: url, |
| 579 | url, |
| 580 | }, |
| 581 | { |
| 582 | label: "method", |
| 583 | text: requestInit?.method ?? "GET", |
| 584 | }, |
| 585 | { |
| 586 | label: "background", |
| 587 | text: "true", |
| 588 | }, |
| 589 | ...(options?.timeout |
| 590 | ? [{ label: "timeout", text: `${options.timeout.durationInMs}ms` }] |
| 591 | : []), |
| 592 | ], |
| 593 | retry: { |
| 594 | limit: 0, |
| 595 | }, |
| 596 | } |
| 597 | )) as TResponseData; |
| 598 | } |
| 599 | |
| 600 | /** `io.backgroundPoll()` will fetch data from a URL on an interval. The actual `fetch` requests are performed on the Trigger.dev server, so you don't have to worry about serverless function timeouts. |
| 601 | * @param cacheKey Should be a stable and unique key inside the `run()`. See [resumability](https://trigger.dev/docs/documentation/concepts/resumability) for more information. |