space.schedule.every()

Call a URL on a schedule, with signed calls unless you turn them off.

Creates a job that calls a URL on a schedule, such as @daily or */15 * * * *. Its calls are signed unless you set signed: false, so your endpoint can check them with verify().

function every(
  schedule: string,
  url: string,
  options?: EveryOptions
): Promise<Job>

Parameters

schedulestringRequired

A 5-field cron schedule, or a shortcut such as @hourly, @daily, @weekly, @monthly, or @yearly. It cannot be empty. See Schedule format.

urlstringRequired

The address to call: an https URL of at most 2,048 characters, without a user name, password, or # part.

options.namestring

1 to 100 characters, on one line, unique in the project ignoring case. When you leave it out, the job is named after its schedule and URL, such as @daily https://shop.example/tasks/reminders, cut to 100 characters.

options.descriptionstring

At most 500 characters.

options.timeZonestring

An IANA time zone, such as Africa/Dar_es_Salaam. UTC when you leave it out. It cannot be empty.

options.methodstring

GET, POST, PUT, PATCH, DELETE, HEAD, or OPTIONS. POST when you leave it out.

options.headersRecord<string, string>

At most 50 headers. You cannot set Host, Content-Length, Transfer-Encoding, Connection, User-Agent, or any X-Scheduler- header, nor Authorization while calls are signed.

options.bodystring

At most 100 KB, and only when method is POST, PUT, PATCH, or left out.

options.signedboolean

Sign every call with a token your endpoint can check. true when you leave it out. See Verify signed calls.

options.audiencestring

The audience the token carries. The URL without its query when you leave it out. A custom audience must use the same scheme and host as the URL.

options.retry.countinteger

How many times to try a failed run again, 0 to 5. 0 when you leave it out, so a failed run is not retried.

options.retry.minBackoffSecondsinteger

The first wait, 1 to 3,600 seconds. 5 when you leave it out.

options.retry.maxBackoffSecondsinteger

The longest wait, 1 to 3,600 seconds, and at least minBackoffSeconds. 3,600 when you leave it out.

options.retry.maxDoublingsinteger

How many times the wait doubles before it grows by the same step, 0 to 16. 5 when you leave it out.

options.retry.maxDurationSecondsinteger

Stop retrying once the run is this old, 0 to 86,400 seconds. 0, no limit, when you leave it out.

options.attemptDeadlineSecondsinteger

How long to wait for your endpoint to answer, 15 to 1,800 seconds. 180 when you leave it out.

options.signalAbortSignal

Cancels the call.

options.timeoutMsnumber

How long the call may take, in milliseconds. The timeoutMs set in createSpace when you leave it out, 30,000 by default.

Every rule is checked before the job is sent, so a job the scheduler would refuse fails with validation_failed and nothing is created. How the waits between retries grow is in Retry jobs.

Returns

A promise of the new job.

every() is not sent again after a network failure or a timeout, because the job may already exist. It is sent again, up to retries times, only when it was refused with 429 or 503 before any work began.

Errors

CodeWhen
validation_failedThe schedule, URL, or an option breaks a rule above, or the scheduler cannot read the schedule or time zone. issues names each field
job_name_takenAnother job in the project has that name, ignoring case. Calling every() twice with the same schedule and URL, and no name, gives this
job_limit_reachedThe project already holds as many jobs as it can

Every call can also fail with the codes shared by all of space, such as missing_project or network_error. See SpaceError.

Example

reminders.ts
import { space } from "digitospace"

const job = await space.schedule.every("0 8 * * MON-FRI", "https://shop.example/tasks/reminders", {
  name: "Payment reminders",
  timeZone: "Africa/Dar_es_Salaam",
  body: JSON.stringify({ channel: "sms" }),
  headers: { "Content-Type": "application/json" },
  retry: { count: 3, minBackoffSeconds: 30 },
})

console.log(job.id, job.nextRunAt)

Last updated