Interval
The Interval
class is the entrypoint for defining
actions and pages.
Usage
- TypeScript
- JavaScript
- Python
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();
const path = require("path");
const { Interval } = require("@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();
import os
from interval_sdk import Interval
interval = Interval(
api_key=os.environ["INTERVAL_API_KEY"],
)
interval.listen()
Props
- TypeScript
- JavaScript
- Python
routesDirectory
Optional
string
routes
Optional
Record<string, Action | Page>
logLevel
Optional
"quiet" | "info" | "debug"
sendTimeoutMs
Optional
number
retryIntervalMs
Optional
number
maxResendAttempts
Optional
number
pingIntervalMs
Optional
number
pingTimeoutMs
Optional
number
closeUnresponsiveTimeoutMs
Optional
number
reinitializeBatchTimeoutMs
Optional
number
onError
Optional
(props: IntervalErrorProps) => void
(props: {
error: Error | unknown
route: string
routeDefinition: Action | Page | undefined
params: Record<string, string | number | boolean | null | undefined | Date | bigint>
environment: 'production' | 'development' | string
user: {
email: string
firstName: string | null
lastName: string | null
}
organization: {
name: string
slug: string
}
}) => void
routesDirectory
Optional
string
routes
Optional
Record<string, Action | Page>
logLevel
Optional
"quiet" | "info" | "debug"
sendTimeoutMs
Optional
number
retryIntervalMs
Optional
number
maxResendAttempts
Optional
number
pingIntervalMs
Optional
number
pingTimeoutMs
Optional
number
closeUnresponsiveTimeoutMs
Optional
number
reinitializeBatchTimeoutMs
Optional
number
onError
Optional
(props: IntervalErrorProps) => void
(props: {
error: Error | unknown
route: string
routeDefinition: Action | Page | undefined
params: Record<string, string | number | boolean | null | undefined | Date | bigint>
environment: 'production' | 'development' | string
user: {
email: string
firstName: string | null
lastName: string | null
}
organization: {
name: string
slug: string
}
}) => void
log_level
Optional
"quiet" | "info" | "debug"
send_timeout
Optional
number
retry_interval
Optional
float
max_resend_attempts
Optional
number
ping_interval
Optional
float
ping_timeout
Optional
float
reinitialize_batch_timeout
Optional
float
on_error
Optional
(props: IntervalErrorProps) => None
@dataclass
class ContextUser:
email: str
first_name: str | None
last_name: str | None
@dataclass
class OrganizationDef:
name: str
slug: str
@dataclass
class IntervalErrorProps:
error: BaseException
route: str
route_definition: Action | Page | None
environment: Literal["production", "development"] | str
params: dict[str, bool | int | float | str | datetime | date | time | None]
user: ContextUser
organization: OrganizationDef
Methods
- TypeScript
- JavaScript
- Python
listen()
async ping()
async safelyClose()
immediatelyClose()
async enqueue(slug, { assignee, params })
async (
slug: string,
options?: {
assignee?: string,
params?: Record<string, string | number | boolean | Date | null | undefined>
}
) => Promise<{
id: string;
assignee?: string;
params?: Record<string, string | number | boolean | Date | null | undefined>;
}>
async dequeue(queuedActionId)
async (
queuedActionId: string
) => Promise<void>
async notify(config)
async ({
message: string,
title?: string,
delivery?: {
to: string;
method?: 'SLACK' | 'EMAIL';
}[],
transactionId?: string,
idempotencyKey?: string,
}) => Promise<void>
listen()
async ping()
async safelyClose()
immediatelyClose()
async enqueue(slug, { assignee, params })
async (
slug: string,
options?: {
assignee?: string,
params?: Record<string, string | number | boolean | Date | null | undefined>
}
) => Promise<{
id: string;
assignee?: string;
params?: Record<string, string | number | boolean | Date | null | undefined>;
}>
async dequeue(queuedActionId)
async (
queuedActionId: string
) => Promise<void>
async notify(config)
async ({
message: string,
title?: string,
delivery?: {
to: string;
method?: 'SLACK' | 'EMAIL';
}[],
transactionId?: string,
idempotencyKey?: string,
}) => Promise<void>
listen()
async listen_async()
async ping()
async safely_close()
async immediately_close()
async enqueue(slug, assignee, params)
async (
slug: str,
assignee: str | None = None,
params: dict[str, str | int | float | bool | date | datetime | None] | None = None,
) => QueuedAction
@dataclass
class QueuedAction:
id: str
assignee: str | None = None
params: dict[str, str | int | float | bool | date | datetime | None] | None = None
async dequeue(queued_action_id)
async (
queued_action_id: str
) => None
async notify(message, title, delivery, transaction_id, idempotency_key)
async (
message: str,
title: str | None = None,
delivery: DeliveryInstruction[],
transaction_id: str | None = None,
idempotency_key: str | None = None,
) => None
class DeliveryInstruction(TypedDict):
to: str
method: NotRequired["EMAIL" | "SLACK"]
Examples
Safely shutting down a live deployment (blue-green deploys)
- TypeScript
- JavaScript
- Python
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.
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);
});
});
Leverage the safelyClose
method to ensure there are no service
interruptions when shutting down a previous version of a live
deployment.
const path = require("path");
const { Interval } = require("@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);
});
});
Leverage the safely_close
method to ensure there are no service
interruptions when shutting down a previous version of a live
deployment.
import asyncio, os
from interval_sdk import Interval
interval = Interval(
api_key=os.environ["INTERVAL_API_KEY"],
)
loop = asyncio.get_event_loop()
task = loop.create_task(interval.listen_async())
close_task: asyncio.Task[None] | None = None
async def handle_close_inner():
try:
await interval.safely_close()
print("Safely shut down successfully.")
except Exception as err:
print("Failed shutting down safely, forcibly closing connection.", file=sys.stderr)
await interval.immediately_close()
def stop_loop(fut: asyncio.Future[None]):
fut.result()
loop.stop()
def handle_close():
global close_task
if close_task is not None:
return
close_task = loop.create_task(handle_close_inner())
close_task.add_done_callback(stop_loop)
loop.add_signal_handler(signal.SIGINT, handle_close)
loop.run_forever()