io.backgroundPoll()
io.backgroundPoll()
allows you to fetch data from a URL on an interval.
client.defineJob({
id: "background-poll-job",
name: "Background Poll Job",
version: "0.0.1",
trigger: invokeTrigger({
schema: z.object({ url: z.string().url() }),
}),
run: async (payload, io, ctx) => {
const result = await io.backgroundPoll<{ foo: string }>("poll", {
url: payload.url,
interval: 10, // every 10 seconds
timeout: 300, // stop polling after 5 minutes
responseFilter: {
// stop polling once this filter matches
status: [200],
body: {
status: ["SUCCESS"],
},
},
});
},
});
Parameters
Should be a stable and unique cache key inside the run()
. See
resumability for more information.
The url to fetch.
The interval in seconds to wait between requests. Minimum interval is 10 seconds and maximum is 5 minutes.
The timeout in seconds before aborting the polling. Minimum timeout is 30 seconds and maximum is 1 hour.
Options for the fetch request
The HTTP method to use for the request.
Any headers to send with the request. Note that you can use redactString to prevent sensitive information from being stored (e.g. in the logs), like API keys and tokens.
The body of the request.
Allows you to filter the response to determine when to stop polling.
An array of status codes to match against.
An object of header key/values to match. This uses the EventFilter matching syntax
filter: {
header: {
"content-type": [{ $startsWith: "application/json" }],
},
},
An EventFilter object to match against the response body. This will only be applied if the response body is JSON.
An optional object to specify a timeout for each individual request.
The timeout in milliseconds before aborting the request.
Returns
A Promise
that resolves with the JSON response body of the matching background fetch request. You can specify the type of the response body as a generic parameter.
Was this page helpful?
client.defineJob({
id: "background-poll-job",
name: "Background Poll Job",
version: "0.0.1",
trigger: invokeTrigger({
schema: z.object({ url: z.string().url() }),
}),
run: async (payload, io, ctx) => {
const result = await io.backgroundPoll<{ foo: string }>("poll", {
url: payload.url,
interval: 10, // every 10 seconds
timeout: 300, // stop polling after 5 minutes
responseFilter: {
// stop polling once this filter matches
status: [200],
body: {
status: ["SUCCESS"],
},
},
});
},
});