Skip to main content

Interval

The Interval class is the entrypoint for defining actions and pages.

Usage

import path from "path";
import { Interval } from "@interval/sdk";

const interval = new Interval({
apiKey: "<YOUR API KEY>" // get an API key at https://interval.com/dashboard/develop/keys
routesDirectory: path.resolve(__dirname, "routes"),
});

interval.listen();

Props

apiKey

Required

string

Your API key. Get yours at https://interval.com/dashboard/develop/keys.

routesDirectory

Optional

string

The path to the directory of route definitions when using file-based routing.

routes

Optional

Record<string, Action | Page>

An object mapping string route slugs to Actions or Pages when defining routes inline.

logLevel

Optional

"quiet" | "info" | "debug"

Controls the type of logs emitted by Interval in the console's standard out and standard error. Defaults to 'info', set to 'quiet' to reduce output to only fatal errors, and 'debug' to include low-level internal debugging information.

retryIntervalMs

Optional

number

How long to wait in milliseconds before retrying a failed call to Interval. Defaults to 3000 (3 seconds).

pingIntervalMs

Optional

number

How often in milliseconds Interval is pinged to ensure a consistent connection. Defaults to 30000 (30 seconds).

pingTimeoutMs

Optional

number

How long to wait in milliseconds for an attempted ping to Interval before assuming it fails and trying again. Defaults to 3000 (3 seconds).

closeUnresponsiveTimeoutMs

Optional

number

How long to wait in milliseconds after receiving the last successful ping response before closing the connection and attempting to reconnect. Defaults to 180000 (3 minutes).

reinitializeBatchTimeoutMs

Optional

number

How long to wait in milliseconds before updating Interval with any programmatic changes made to defined routes. Defaults to 200.

Methods

listen()

Establish the persistent connection to Interval.

async safelyClose()

Safely close the connection to Interval, preventing new actions from being launched and closing the persistent connection afterward. Resolves when the connection is successfully safely closed.

immediatelyClose()

Immediately terminate the connection to interval, terminating any actions currently in progress.

Examples

Safely shutting down a live deployment (rolling deploys)

Leverage the safelyClose method to ensure there are no service interruptions, lost progress, or malformed data when shutting down a previous version of a live deployment.

src/interval.ts
import path from "path";
import { Interval } from "@interval/sdk";

const interval = new Interval({
apiKey: "<YOUR API KEY>" // get an API key at https://interval.com/dashboard/develop/keys
routesDirectory: path.resolve(__dirname, "routes"),
});

interval.listen();

process.on('SIGINT', () => {
interval
.safelyClose()
.then(() => {
console.log("Safely shut down successfully.");
process.exit(0);
})
.catch(err => {
console.error(
"Failed shutting down safely, forcibly closing connection."
)
interval.immediatelyClose();
process.exit(0);
});
});
Did this section clearly explain what you wanted to learn?