new Job -> client.defineJob

This commit is contained in:
Eric Allam
2023-08-03 09:53:15 +01:00
parent 34d77e830c
commit 1b7c7520b4
40 changed files with 167 additions and 252 deletions
+1 -1
View File
@@ -1,5 +1,5 @@
```typescript Wait example
new Job(client, {
client.defineJob({
id: "delay-job",
name: "Delay Job",
version: "0.0.1",
+1 -1
View File
@@ -1,5 +1,5 @@
```typescript
new Job(client, {
client.defineJob({
//... other options
integrations: {
slack,
+7 -21
View File
@@ -4,9 +4,8 @@ description: "Integrations make it easy to use APIs in your Jobs"
---
<Note>
You can use any API in your Jobs by using existing Node.js SDKs or HTTP
requests. Integrations just make it much easier especially when you want to
use OAuth. And you get great logging.
You can use any API in your Jobs by using existing Node.js SDKs or HTTP requests. Integrations
just make it much easier especially when you want to use OAuth. And you get great logging.
</Note>
An Integration is a package you install that makes it easy to work with a specific API. They:
@@ -35,7 +34,7 @@ const slack = new Slack({
id: "slack",
});
new Job(client, {
client.defineJob({
id: "alert-on-new-github-issues",
name: "Alert on new GitHub issues",
version: "0.1.1",
@@ -84,29 +83,16 @@ You can use OAuth to authenticate your internal team with an Integration or to a
## References
<CardGroup>
<Card
title="Integrations Dashboard"
icon="sidebar"
href="documentation/guides/integrations"
>
The Integrations Dashboard allows you to manage your Integrations and setup
OAuth.
<Card title="Integrations Dashboard" icon="sidebar" href="documentation/guides/integrations">
The Integrations Dashboard allows you to manage your Integrations and setup OAuth.
</Card>
<Card
title="Trigger.dev Connect"
icon="user-plus"
href="/documentation/concepts/connect"
>
<Card title="Trigger.dev Connect" icon="user-plus" href="/documentation/concepts/connect">
Authenticate your users with an Integration using Trigger.dev Connect.
</Card>
<Card title="View Integrations" icon="grid-2" href="/integrations">
Trigger.dev integrates with a wide range of services.
</Card>
<Card
title="Create an Integration"
icon="square-plus"
href="/integrations/create"
>
<Card title="Create an Integration" icon="square-plus" href="/integrations/create">
Create an Integration for your own use or as a public package.
</Card>
</CardGroup>
+2 -6
View File
@@ -17,7 +17,7 @@ A Job is made up of a few things:
```ts
//Job definition uses the client
new Job(client, {
client.defineJob({
// 1. Metadata
id: "event-1",
name: "Run when the foo.bar event happens",
@@ -51,11 +51,7 @@ Events [trigger](/documentation/concepts/triggers) Jobs. Jobs generate a [Run](/
<Card title="Job SDK reference" icon="wrench" href="/sdk/job">
Detailed SDK reference for Jobs.
</Card>
<Card
title="Managing Jobs Dashboard"
icon="globe"
href="/documentation/guides/managing-jobs"
>
<Card title="Managing Jobs Dashboard" icon="globe" href="/documentation/guides/managing-jobs">
Viewing and managing your Jobs in the Dashboard.
</Card>
</CardGroup>
+2 -6
View File
@@ -10,7 +10,7 @@ description: "When a [Job](/documentation/concepts/jobs) is [Triggered](/documen
A Run is a record of the execution of a Job. It is created from `run()` function of a Job.
```ts
new Job(client, {
client.defineJob({
id: "event-1",
name: "Run when the foo.bar event happens",
version: "0.0.1",
@@ -64,11 +64,7 @@ The `context` object gives you access to information about the current Run, Job,
## References
<CardGroup cols={2}>
<Card
title="Viewing Runs Dashboard"
icon="globe"
href="/documentation/guides/viewing-runs"
>
<Card title="Viewing Runs Dashboard" icon="globe" href="/documentation/guides/viewing-runs">
View all Runs for a Job, all the way down to individual Tasks.
</Card>
<Card title="`io` SDK Reference" icon="wrench" href="/sdk/io">
+7 -23
View File
@@ -10,7 +10,7 @@ description: "Tasks are individual building blocks of a Run."
In the `run()` function you can use regular code and you can use Tasks.
```ts
new Job(client, {
client.defineJob({
id: "new-user",
name: "Run when a new user signs up",
version: "0.0.1",
@@ -44,13 +44,9 @@ new Job(client, {
await io.wait("wait", 60 * 60 * 3); // wait for 3 hours
// You can wrap your own code in a Task, for retrying, resumability and logging
const response = await io.runTask(
"my-task",
{ name: "My Task" },
async () => {
return await longRunningCode(payload.userId);
}
);
const response = await io.runTask("my-task", { name: "My Task" }, async () => {
return await longRunningCode(payload.userId);
});
return response;
},
@@ -76,28 +72,16 @@ The first param of all Tasks is a `key`. This is a unique identifier for the Tas
## References
<CardGroup cols={2}>
<Card
title="Resumability"
icon="clock"
href="/documentation/concepts/resumability"
>
<Card title="Resumability" icon="clock" href="/documentation/concepts/resumability">
Runs can be very long-running. Learn how we handle this.
</Card>
<Card
title="Integrations"
icon="grid-2"
href="/documentation/concepts/integrations"
>
<Card title="Integrations" icon="grid-2" href="/documentation/concepts/integrations">
Integrations utilize Tasks.
</Card>
<Card title="`io` SDK Reference" icon="wrench" href="/sdk/io">
The `io` object allows you to easily run a Task yourself.
</Card>
<Card
title="Viewing Runs Dashboard"
icon="globe"
href="/documentation/guides/viewing-runs"
>
<Card title="Viewing Runs Dashboard" icon="globe" href="/documentation/guides/viewing-runs">
View all Runs for a Job, all the way down to individual Tasks.
</Card>
</CardGroup>
@@ -17,7 +17,7 @@ const dynamicSchedule = new DynamicSchedule(client, {
});
//2. create a Job that is attached to the dynamic schedule
new Job(client, {
client.defineJob({
id: "user-dynamicinterval",
name: "User Dynamic Interval",
version: "0.1.1",
@@ -41,7 +41,7 @@ async function registerUserCronJob(userId: string, userSchedule: string) {
}
//5. Register inside other Jobs
new Job(client, {
client.defineJob({
id: "register-dynamicinterval",
name: "Register Dynamic Interval",
version: "0.1.1",
@@ -77,7 +77,7 @@ const dynamicOnIssueOpenedTrigger = new DynamicTrigger(client, {
});
//2. create a Job that is attached to the dynamic trigger
new Job(client, {
client.defineJob({
id: "listen-for-dynamic-trigger",
name: "Listen for dynamic trigger",
version: "0.1.1",
@@ -87,9 +87,7 @@ new Job(client, {
},
run: async (payload, io, ctx) => {
await io.slack.postMessage("Slack 📝", {
text: `New Issue opened on repo: ${
payload.issue.html_url
}. \n\n${JSON.stringify(ctx)}`,
text: `New Issue opened on repo: ${payload.issue.html_url}. \n\n${JSON.stringify(ctx)}`,
channel: "C04GWUTDC3W",
});
},
@@ -105,7 +103,7 @@ async function registerRepo(owner: string, repo: string) {
}
//4. Register inside other Jobs
new Job(client, {
client.defineJob({
id: "new-repo",
name: "New repo",
version: "0.1.1",
@@ -23,7 +23,7 @@ You can always start out by using `z.any()` as your schema, and then later on yo
## Example
```ts
new Job(client, {
client.defineJob({
id: "new-user-slack",
name: "New user slack message",
version: "0.1.0",
@@ -58,9 +58,8 @@ new Job(client, {
```
<Note>
You can subscribe to the same event from multiple different Jobs. This is
useful if you want to send an event to multiple different services or if you
want to keep each Job small and simple.
You can subscribe to the same event from multiple different Jobs. This is useful if you want to
send an event to multiple different services or if you want to keep each Job small and simple.
</Note>
## Sending events
@@ -84,7 +83,7 @@ await client.sendEvent({
You can use `io.sendEvent()` to send events from inside a Job run, to trigger another. [View the SDK reference](/sdk/io/sendevent).
```ts
new Job(client, {
client.defineJob({
id: "event-1",
name: "Run when the foo.bar event happens",
version: "0.0.1",
@@ -15,7 +15,7 @@ This job will run every 60 seconds, starting 60 seconds after this Job is first
```ts
import { Job, intervalTrigger } from "@trigger.dev/sdk";
new Job(client, {
client.defineJob({
id: "scheduled-job-1",
name: "Scheduled Job 1",
version: "0.1.1",
@@ -43,7 +43,7 @@ This job will run at 2:30pm every Monday. You can get help with [CRON syntax](ht
```ts
import { Job, cronTrigger } from "@trigger.dev/sdk";
new Job(client, {
client.defineJob({
id: "scheduled-job-2",
name: "Scheduled Job 2",
version: "0.1.1",
@@ -32,7 +32,7 @@ const github = new Github({
token: process.env.GITHUB_API_KEY!,
});
new Job(client, {
client.defineJob({
id: "critical-issue-alert",
name: "Critical Issue Alert",
version: "0.1.0",
@@ -42,7 +42,7 @@ const slack = new Slack({
id: "slack",
});
new Job(client, {
client.defineJob({
id: "critical-issue-alert",
name: "Critical Issue Alert",
version: "0.1.0",
@@ -46,7 +46,7 @@ There are two way to use Integrations in a Job:
This example automatically assigns "matt-aitken" to any new issue in the `trigger.dev` repo (lucky him).
```ts
new Job(client, {
client.defineJob({
id: "assign-on-issue-opened",
name: "Assign on Issue Opened",
version: "0.1.0",
@@ -35,7 +35,7 @@ There are two way to use Integrations in a Job:
This example send a Slack message when someone stars the `trigger.dev` GitHub repo 🤩.
```ts
new Job(client, {
client.defineJob({
id: "star-slack-notification",
name: "New Star Slack Notification",
version: "0.1.0",
+3 -3
View File
@@ -13,7 +13,7 @@ We use it [extensively](https://github.com/search?q=repo%3Atriggerdotdev%2Ftrigg
But there are a few places where we ask you to provide us with a Zod schema, for example when defining your own [events](/documentation/concepts/triggers/events):
```ts
new Job(client, {
client.defineJob({
id: "new-user",
name: "New user",
version: "0.1.0",
@@ -36,8 +36,8 @@ new Job(client, {
So it will help to know a little about Zod and how to use it. We definitely recommend the well written [Zod README](https://github.com/colinhacks/zod#readme) but we've included a short primer below.
<Tip>
Wherever we require you to pass in a Zod schema, you can always start with
`z.any()` which accepts `any` type and then add more strict validations later.
Wherever we require you to pass in a Zod schema, you can always start with `z.any()` which accepts
`any` type and then add more strict validations later.
</Tip>
## Basic Usage
+1 -1
View File
@@ -167,7 +167,7 @@ In there is this Job:
```typescript
//Job definition uses the client
new Job(client, {
client.defineJob({
// 1. Metadata
id: "example-job",
name: "Example Job",
+1 -1
View File
@@ -23,7 +23,7 @@ title: Tasks
## Usage
```ts
new Job(client, {
client.defineJob({
id: "github-integration-on-issue-opened",
name: "GitHub Integration - On Issue Opened",
version: "0.1.0",
+1 -1
View File
@@ -29,7 +29,7 @@ const github = new Github({
token: process.env.GITHUB_TOKEN!,
});
new Job(client, {
client.defineJob({
id: "github-integration-on-issue",
name: "GitHub Integration - On Issue",
version: "0.1.0",
+11 -16
View File
@@ -45,8 +45,7 @@ const github2 = new Github({
<CardGroup cols={2}>
<Card title="Triggers" icon="stars" href="/integrations/apis/github-triggers">
Trigger Jobs when events happen in GitHub, such as a new commit or a new
issue.
Trigger Jobs when events happen in GitHub, such as a new commit or a new issue.
</Card>
<Card title="Tasks" icon="sparkles" href="/integrations/apis/github-tasks">
Perform tasks such as creating a new issue or a new comment.
@@ -58,8 +57,8 @@ const github2 = new Github({
You can use the underlying client to do anything Octokit supports. In this example we create a project card when a new issue is opened..
<Info>
View [the official GitHub docs](https://docs.github.com/en/rest) for
everything that is supported{" "}
View [the official GitHub docs](https://docs.github.com/en/rest) for everything that is
supported{" "}
</Info>
```ts
@@ -70,7 +69,7 @@ const github = new Github({
token: process.env.GITHUB_TOKEN!,
});
new Job(client, {
client.defineJob({
id: "alert-on-new-github-issues",
name: "Alert on new GitHub issues",
version: "0.1.1",
@@ -84,17 +83,13 @@ new Job(client, {
},
run: async (payload, io, ctx) => {
//wrap the SDK call in runTask
const { data } = await io.runTask(
"create-card",
{ name: "Create card" },
async () => {
//create a project card using the underlying client
return io.github.client.rest.projects.createCard({
column_id: 123,
note: "test",
});
}
);
const { data } = await io.runTask("create-card", { name: "Create card" }, async () => {
//create a project card using the underlying client
return io.github.client.rest.projects.createCard({
column_id: 123,
note: "test",
});
});
//log the url of the created card
await io.logger.info(data.url);
+26 -29
View File
@@ -50,7 +50,7 @@ export const plain = new Plain({
apiKey: process.env.PLAIN_API_KEY!,
});
new Job(client, {
client.defineJob({
id: "plain-playground",
name: "Plain Playground",
version: "0.1.1",
@@ -87,37 +87,34 @@ new Job(client, {
customerId: customer.id,
});
const timelineEntry = await io.plain.upsertCustomTimelineEntry(
"upsert-timeline-entry",
{
customerId: customer.id,
title: "My timeline entry",
components: [
{
componentText: {
text: `This is a nice title`,
},
const timelineEntry = await io.plain.upsertCustomTimelineEntry("upsert-timeline-entry", {
customerId: customer.id,
title: "My timeline entry",
components: [
{
componentText: {
text: `This is a nice title`,
},
{
componentDivider: {
dividerSpacingSize: ComponentDividerSpacingSize.M,
},
},
{
componentDivider: {
dividerSpacingSize: ComponentDividerSpacingSize.M,
},
{
componentText: {
textSize: ComponentTextSize.S,
textColor: ComponentTextColor.Muted,
text: "External id",
},
},
{
componentText: {
textSize: ComponentTextSize.S,
textColor: ComponentTextColor.Muted,
text: "External id",
},
{
componentText: {
text: foundCustomer?.externalId ?? "",
},
},
{
componentText: {
text: foundCustomer?.externalId ?? "",
},
],
}
);
},
],
});
},
});
```
@@ -145,7 +142,7 @@ export const plain = new Plain({
apiKey: process.env.PLAIN_API_KEY!,
});
new Job(client, {
client.defineJob({
id: "plain-client",
name: "Plain Client",
version: "0.1.0",
+1 -1
View File
@@ -51,7 +51,7 @@ const resend = new Resend({
apiKey: process.env.RESEND_API_KEY!,
});
new Job(client, {
client.defineJob({
id: "send-resend-email",
name: "Send Resend Email",
version: "0.1.0",
+1 -1
View File
@@ -37,7 +37,7 @@ const slack = new Slack({
## Example
```ts
new Job(client, {
client.defineJob({
id: "slack-test",
name: "Slack test",
version: "0.0.1",
+6 -9
View File
@@ -50,7 +50,7 @@ export const typeform = new Typeform({
token: process.env.TYPEFORM_API_KEY!,
});
new Job(client, {
client.defineJob({
id: "do-something-on-new-responses",
name: "Send a message to slack on new responses",
version: "0.1.1",
@@ -90,7 +90,7 @@ const typeform = new Typeform({
token: process.env.TYPEFORM_PAT!,
});
new Job(client, {
client.defineJob({
id: "typeform-tasks",
name: "Typeform Tasks",
version: "0.1.0",
@@ -110,12 +110,9 @@ new Job(client, {
pageSize: 50,
});
const allResponses = await io.typeform.getAllResponses(
"get-all-responses",
{
uid: payload.formId,
}
);
const allResponses = await io.typeform.getAllResponses("get-all-responses", {
uid: payload.formId,
});
},
});
```
@@ -133,7 +130,7 @@ const typeform = new Typeform({
token: process.env.TYPEFORM_PAT!,
});
new Job(client, {
client.defineJob({
id: "typeform-client",
name: "Typeform Client",
version: "0.1.0",
+22 -31
View File
@@ -71,9 +71,9 @@ Once you've created your Integration package, you can start developing it. In th
This is the entry point of the Integration package. It exports a main "integration" class that implements the `TriggerIntegration` interface. For example, the `@trigger.dev/github` Integration exports a `Github` class that implements.
<Tip>
We're adopting the naming convention of naming the class after the service,
without a suffix or prefix. We prefer the exported name be `Slack` instead of
something like `SlackIntegration` or `SlackConnector`
We're adopting the naming convention of naming the class after the service, without a suffix or
prefix. We prefer the exported name be `Slack` instead of something like `SlackIntegration` or
`SlackConnector`
</Tip>
<Accordion title="Example: OpenAI">
@@ -84,9 +84,7 @@ import { Configuration, OpenAIApi } from "openai";
import * as tasks from "./tasks";
import { OpenAIIntegrationOptions } from "./types";
export class OpenAI
implements TriggerIntegration<IntegrationClient<OpenAIApi, typeof tasks>>
{
export class OpenAI implements TriggerIntegration<IntegrationClient<OpenAIApi, typeof tasks>> {
client: IntegrationClient<OpenAIApi, typeof tasks>;
constructor(private options: OpenAIIntegrationOptions) {
@@ -121,19 +119,18 @@ export class OpenAI
The `TriggerIntegration` interface requires three properties to be implemented:
<ParamField body="id" type="string" required>
The `id` that uniquely identifies the Integration. This should always be
passed through the constructor options.
The `id` that uniquely identifies the Integration. This should always be passed through the
constructor options.
</ParamField>
<ParamField body="metadata" type="object" required>
<Expandable title="properties">
<ParamField body="id" type="string" required>
A unique identifier for the Integration. For example, the OpenAI
Integration has an id of `"openai"`.
A unique identifier for the Integration. For example, the OpenAI Integration has an id of
`"openai"`.
</ParamField>
<ParamField body="name" type="string" required>
The name of the Integration. For example, the OpenAI Integration has a
name of `"OpenAI"`.
The name of the Integration. For example, the OpenAI Integration has a name of `"OpenAI"`.
</ParamField>
</Expandable>
</ParamField>
@@ -194,11 +191,7 @@ For example, here is the `getForm` authenticated task defined in the `@trigger.d
import type { AuthenticatedTask } from "@trigger.dev/sdk";
import type { GetFormParams, GetFormResponse, TypeformSDK } from "./types";
export const getForm: AuthenticatedTask<
TypeformSDK,
GetFormParams,
GetFormResponse
> = {
export const getForm: AuthenticatedTask<TypeformSDK, GetFormParams, GetFormResponse> = {
init: (params) => {
return {
name: "Get Form",
@@ -232,7 +225,7 @@ export type GetFormResponse = Prettify<Typeform.Form>;
```
```ts usage.ts
new Job(client, {
client.defineJob({
id: "typeform-playground",
name: "Typeform Playground",
version: "0.1.1",
@@ -258,9 +251,9 @@ The first thing to notice is the explicit typing of the `getForm` export as an `
If you take a look at the `usage.ts` file above, you can see how this task is used in a job. The `io.typeform.getForm` function is typed as returning `Promise<GetFormResponse>` and the `params` argument is typed as `GetFormParams`.
<Note>
Notice how the params are the _second_ argument to `getForm`, that's because
the first argument is always the task key. See our [Keys and Resumability
docs](/documentation/concepts/resumability) for more on why this is important
Notice how the params are the _second_ argument to `getForm`, that's because the first argument is
always the task key. See our [Keys and Resumability docs](/documentation/concepts/resumability)
for more on why this is important
</Note>
#### `run` function
@@ -268,8 +261,8 @@ If you take a look at the `usage.ts` file above, you can see how this task is us
The `run` function is the main function that will be called when the task is run. It's an async function that takes up to 5 arguments:
<ParamField body="params" type="type parameter" required>
The input params that were passed to the task. This is the second argument to
the `getForm` function in the example above.
The input params that were passed to the task. This is the second argument to the `getForm`
function in the example above.
</ParamField>
<ParamField body="client" type="type parameter" required>
@@ -286,9 +279,9 @@ The `run` function is the main function that will be called when the task is run
</ParamField>
<ParamField body="auth" type="ConnectionAuth">
If for some reason you need to access the auth object that was used to seed
the SDK client, you can access it here. The `AuthenticatedTask` generic type
takes an optional 4th type parameter that allows you to specify the auth type
If for some reason you need to access the auth object that was used to seed the SDK client, you
can access it here. The `AuthenticatedTask` generic type takes an optional 4th type parameter that
allows you to specify the auth type
</ParamField>
#### `init` function
@@ -296,8 +289,8 @@ The `run` function is the main function that will be called when the task is run
The `init` function is used to initialize the task. It's a synchronous function that takes a single argument:
<ParamField body="params" type="type parameter" required>
The input params that were passed to the task. This is the second argument to
the `getForm` function in the example above.
The input params that were passed to the task. This is the second argument to the `getForm`
function in the example above.
</ParamField>
#### `onError` function
@@ -466,9 +459,7 @@ export const backgroundCreateCompletion: AuthenticatedTask<
headers: {
"Content-Type": "application/json",
Authorization: redactString`Bearer ${auth.apiKey}`,
...(auth.organization
? { "OpenAI-Organization": auth.organization }
: {}),
...(auth.organization ? { "OpenAI-Organization": auth.organization } : {}),
},
body: JSON.stringify(params),
}
+3 -4
View File
@@ -23,8 +23,7 @@ A useful tool when writing CRON expressions is [crontab guru](https://crontab.gu
<ResponseField name="options" type="object" required>
<Expandable title="options" defaultOpen>
<ResponseField name="cron" type="string" required>
A CRON expression that defines the schedule. Note that the timezone used
is always UTC.
A CRON expression that defines the schedule. Note that the timezone used is always UTC.
</ResponseField>
</Expandable>
</ResponseField>
@@ -32,7 +31,7 @@ A useful tool when writing CRON expressions is [crontab guru](https://crontab.gu
<RequestExample>
```typescript 9am UTC everyday
new Job(client, {
client.defineJob({
id: "scheduled-job-1",
name: "Scheduled Job 1",
version: "0.1.1",
@@ -51,7 +50,7 @@ new Job(client, {
```
```typescript First day of month
new Job(client, {
client.defineJob({
id: "scheduled-job-2",
name: "Scheduled Job 2",
version: "0.1.1",
+2 -2
View File
@@ -39,7 +39,7 @@ const dynamicSchedule = new DynamicSchedule(client, {
});
//2. create a Job that is attached to the dynamic schedule
new Job(client, {
client.defineJob({
id: "user-dynamicinterval",
name: "User Dynamic Interval",
version: "0.1.1",
@@ -63,7 +63,7 @@ async function registerUserCronJob(userId: string, userSchedule: string) {
}
//5. Register inside other Jobs
new Job(client, {
client.defineJob({
id: "register-dynamicinterval",
name: "Register Dynamic Interval",
version: "0.1.1",
+3 -5
View File
@@ -40,7 +40,7 @@ const dynamicOnIssueOpenedTrigger = new DynamicTrigger(client, {
});
//2. create a Job that is attached to the dynamic trigger
new Job(client, {
client.defineJob({
id: "listen-for-dynamic-trigger",
name: "Listen for dynamic trigger",
version: "0.1.1",
@@ -50,9 +50,7 @@ new Job(client, {
},
run: async (payload, io, ctx) => {
await io.slack.postMessage("Slack 📝", {
text: `New Issue opened on repo: ${
payload.issue.html_url
}. \n\n${JSON.stringify(ctx)}`,
text: `New Issue opened on repo: ${payload.issue.html_url}. \n\n${JSON.stringify(ctx)}`,
channel: "C04GWUTDC3W",
});
},
@@ -68,7 +66,7 @@ async function registerRepo(owner: string, repo: string) {
}
//4. Register inside other Jobs
new Job(client, {
client.defineJob({
id: "new-repo",
name: "New repo",
version: "0.1.1",
+1 -1
View File
@@ -53,7 +53,7 @@ You can have multiple Jobs that subscribe to the same event, they will all trigg
```typescript eventTrigger()
//this Job subscribes to an event called new.user
new Job(client, {
client.defineJob({
id: "job-2",
name: "Second job",
version: "0.0.1",
+1 -1
View File
@@ -30,7 +30,7 @@ If you wish to Run a Job at an exact time or less frequently than once pr day yo
<RequestExample>
```typescript Every 5 minutes
new Job(client, {
client.defineJob({
id: "scheduled-job-1",
name: "Scheduled Job 1",
version: "0.1.1",
+6 -10
View File
@@ -20,9 +20,8 @@ This is used inside the OpenAI Integration for Tasks like `backgroundCreateChatC
The HTTP method to use for the request.
</ResponseField>
<ResponseField name="headers" type="object">
Any headers to send with the request. Note that you can use
[redactString](sdk/redactString) to prevent sensitive information from being
stored (e.g. in the logs), like API keys and tokens.
Any headers to send with the request. Note that you can use [redactString](sdk/redactString) to
prevent sensitive information from being stored (e.g. in the logs), like API keys and tokens.
</ResponseField>
<ResponseField name="body" type="string | ArrayBuffer">
The body of the request.
@@ -84,19 +83,16 @@ An individual retrying strategy can be one of two types:
<Expandable title="headers strategy">
<ResponseField name="type" type="headers" required>
The `headers` strategy retries the request using info from the response
headers.
The `headers` strategy retries the request using info from the response headers.
</ResponseField>
<ResponseField name="limitHeader" type="string">
The header to use to determine the maximum number of times to retry the
request.
The header to use to determine the maximum number of times to retry the request.
</ResponseField>
<ResponseField name="remainingHeader" type="string">
The header to use to determine the number of remaining retries.
</ResponseField>
<ResponseField name="resetHeader" type="string">
The header to use to determine the time when the number of remaining retries
will be reset.
The header to use to determine the time when the number of remaining retries will be reset.
</ResponseField>
</Expandable>
@@ -111,7 +107,7 @@ A `Promise` that resolves after the specified amount of time.
<RequestExample>
```typescript backgroundFetch example
new Job(client, {
client.defineJob({
id: "background-fetch-job",
name: "Background fetch Job",
version: "0.0.1",
+4 -6
View File
@@ -19,9 +19,8 @@ description: "`io.registerCron()` allows you to register a [DynamicSchedule](/sd
<Expandable title="options" defaultOpen>
<ResponseField name="cron" type="string" required>
A CRON expression that defines the schedule. A useful tool when writing CRON
expressions is [crontab guru](https://crontab.guru). Note that the timezone
used is UTC.
A CRON expression that defines the schedule. A useful tool when writing CRON expressions is
[crontab guru](https://crontab.guru). Note that the timezone used is UTC.
</ResponseField>
</Expandable>
@@ -32,8 +31,7 @@ description: "`io.registerCron()` allows you to register a [DynamicSchedule](/sd
A Promise that resolves to an object with the following fields:
<ResponseField name="id" type="string" required>
A unique id for the interval. This is used to identify and unregister the
interval later.
A unique id for the interval. This is used to identify and unregister the interval later.
</ResponseField>
<ResponseField name="metadata" type="any" required>
Any additional metadata about the interval.
@@ -63,7 +61,7 @@ A Promise that resolves to an object with the following fields:
<RequestExample>
```typescript
new Job(client, {
client.defineJob({
id: "my-job",
name: "My job",
version: "0.1.1",
+2 -3
View File
@@ -30,8 +30,7 @@ description: "`io.registerInterval()` allows you to register a [DynamicSchedule]
A Promise that resolves to an object with the following fields:
<ResponseField name="id" type="string" required>
A unique id for the interval. This is used to identify and unregister the
interval later.
A unique id for the interval. This is used to identify and unregister the interval later.
</ResponseField>
<ResponseField name="metadata" type="any" required>
Any additional metadata about the interval.
@@ -61,7 +60,7 @@ A Promise that resolves to an object with the following fields:
<RequestExample>
```typescript
new Job(client, {
client.defineJob({
id: "my-job",
name: "My job",
version: "0.1.1",
+7 -13
View File
@@ -8,16 +8,13 @@ description: "`io.registerTrigger()` allows you to register a [DynamicTrigger](/
<Snippet file="stable-key-param.mdx" />
<ResponseField name="dynamicTrigger" type="DynamicTrigger" required>
A [DynamicTrigger](/sdk/dynamictrigger) that will trigger any Jobs it's
attached.
A [DynamicTrigger](/sdk/dynamictrigger) that will trigger any Jobs it's attached.
</ResponseField>
<ResponseField name="id" type="string" required>
A unique id for the registration. This is used to identify and unregister
later.
A unique id for the registration. This is used to identify and unregister later.
</ResponseField>
<ResponseField name="params" type="object" required>
The params for the DynamicTrigger. These will vary depending on the type of
the DynamicTrigger.
The params for the DynamicTrigger. These will vary depending on the type of the DynamicTrigger.
</ResponseField>
## Returns
@@ -25,8 +22,7 @@ description: "`io.registerTrigger()` allows you to register a [DynamicTrigger](/
A Promise that resolves to an object with the following fields:
<ResponseField name="id" type="string" required>
A unique id for the registration. This is used to identify and unregister
later.
A unique id for the registration. This is used to identify and unregister later.
</ResponseField>
<ResponseField name="key" type="string" required>
The key of the registration.
@@ -43,7 +39,7 @@ const dynamicOnIssueOpenedTrigger = new DynamicTrigger(client, {
});
//2. create a Job that is attached to the dynamic trigger
new Job(client, {
client.defineJob({
id: "listen-for-dynamic-trigger",
name: "Listen for dynamic trigger",
version: "0.1.1",
@@ -53,15 +49,13 @@ new Job(client, {
},
run: async (payload, io, ctx) => {
await io.slack.postMessage("Slack 📝", {
text: `New Issue opened on repo: ${
payload.issue.html_url
}. \n\n${JSON.stringify(ctx)}`,
text: `New Issue opened on repo: ${payload.issue.html_url}. \n\n${JSON.stringify(ctx)}`,
channel: "C04GWUTDC3W",
});
},
});
new Job(client, {
client.defineJob({
id: "new-repo",
name: "New repo",
version: "0.1.1",
+2 -2
View File
@@ -122,7 +122,7 @@ A Promise that resolves with the returned value of the callback.
<RequestExample>
```typescript Run a task
new Job(client, {
client.defineJob({
id: "alert-on-new-github-issues",
name: "Alert on new GitHub issues",
version: "0.1.1",
@@ -155,7 +155,7 @@ new Job(client, {
```
```typescript onError callback
new Job(client, {
client.defineJob({
id: "custom-error-handling",
name: "Custom Error handling",
version: "0.1.1",
+3 -4
View File
@@ -13,8 +13,7 @@ Use [eventTrigger()](/sdk/eventtrigger) on a Job to listen for events.
<Snippet file="stable-key-param.mdx" />
<ResponseField name="seconds" type="number" required>
The number of seconds to wait. This can be very long, serverless timeouts are
not an issue.
The number of seconds to wait. This can be very long, serverless timeouts are not an issue.
</ResponseField>
<Snippet file="send-event-params.mdx" />
@@ -27,7 +26,7 @@ Use [eventTrigger()](/sdk/eventtrigger) on a Job to listen for events.
```typescript Send an event
//this Job sends an event that triggers the second job
new Job(client, {
client.defineJob({
id: "job-1",
name: "First job",
version: "0.0.1",
@@ -45,7 +44,7 @@ new Job(client, {
},
});
new Job(client, {
client.defineJob({
id: "job-2",
name: "Second job",
version: "0.0.1",
+1 -1
View File
@@ -32,7 +32,7 @@ You have two options:
<RequestExample>
```typescript Using io.try()
new Job(client, {
client.defineJob({
id: "get-repo-info",
name: "GitHub get repo info",
version: "0.1.0",
+4 -5
View File
@@ -8,12 +8,11 @@ description: "`io.unregisterCron()` allows you to unregister a [DynamicSchedule]
<Snippet file="stable-key-param.mdx" />
<ResponseField name="dynamicSchedule" type="DynamicSchedule" required>
A [DynamicSchedule](/sdk/dynamicschedule) that will trigger any Jobs it's
attached to on a regular interval.
A [DynamicSchedule](/sdk/dynamicschedule) that will trigger any Jobs it's attached to on a regular
interval.
</ResponseField>
<ResponseField name="id" type="string" required>
A unique id for the schedule. This is used to identify and unregister the
schedule later.
A unique id for the schedule. This is used to identify and unregister the schedule later.
</ResponseField>
## Returns
@@ -27,7 +26,7 @@ A Promise with the following shape:
<RequestExample>
```typescript
new Job(client, {
client.defineJob({
id: "unregister-job",
name: "Unregister dynamic schedule",
version: "0.1.1",
+4 -5
View File
@@ -8,12 +8,11 @@ description: "`io.unregisterInterval()` allows you to unregister a [DynamicSched
<Snippet file="stable-key-param.mdx" />
<ResponseField name="dynamicSchedule" type="DynamicSchedule" required>
A [DynamicSchedule](/sdk/dynamicschedule) that will trigger any Jobs it's
attached to on a regular interval.
A [DynamicSchedule](/sdk/dynamicschedule) that will trigger any Jobs it's attached to on a regular
interval.
</ResponseField>
<ResponseField name="id" type="string" required>
A unique id for the interval. This is used to identify and unregister the
interval later.
A unique id for the interval. This is used to identify and unregister the interval later.
</ResponseField>
## Returns
@@ -27,7 +26,7 @@ A Promise with the following shape:
<RequestExample>
```typescript
new Job(client, {
client.defineJob({
id: "unregister-job",
name: "Unregister dynamic schedule",
version: "0.1.1",
+2 -3
View File
@@ -8,8 +8,7 @@ description: "`io.unregisterTrigger()` allows you to unregister a [DynamicTrigge
<Snippet file="stable-key-param.mdx" />
<ResponseField name="dynamicTrigger" type="DynamicTrigger" required>
A [DynamicTrigger](/sdk/dynamictrigger) that will trigger any Jobs it's
attached to.
A [DynamicTrigger](/sdk/dynamictrigger) that will trigger any Jobs it's attached to.
</ResponseField>
<ResponseField name="id" type="string" required>
A unique id for the trigger. This is used to identify and unregister it later.
@@ -26,7 +25,7 @@ A Promise with the following shape:
<RequestExample>
```typescript
new Job(client, {
client.defineJob({
id: "unregister-job",
name: "Unregister dynamic trigger",
version: "0.1.1",
+1 -1
View File
@@ -32,7 +32,7 @@ You must rethrow the error if this function returns `true`.
<RequestExample>
```typescript Using io.try()
new Job(client, {
client.defineJob({
id: "get-repo-info",
name: "GitHub get repo info",
version: "0.1.0",
+13 -17
View File
@@ -12,7 +12,7 @@ By far the most important thing to understand is the constructor.
<RequestExample>
```ts cronTrigger
new Job(client, {
client.defineJob({
id: "slack-kpi-summary",
name: "Slack kpi summary",
version: "0.1.1",
@@ -35,7 +35,7 @@ new Job(client, {
```
```ts webhook
new Job(client, {
client.defineJob({
id: "github-integration-on-issue",
name: "GitHub Integration - On Issue",
version: "0.1.0",
@@ -52,7 +52,7 @@ new Job(client, {
```
```ts event
new Job(client, {
client.defineJob({
id: "openai-joke",
name: "OpenAI Joke",
version: "0.0.1",
@@ -66,18 +66,15 @@ new Job(client, {
openai,
},
run: async (payload, io, ctx) => {
const joke = await io.openai.backgroundCreateChatCompletion(
"generate-jokes",
{
model: "gpt-3.5-turbo",
messages: [
{
role: "user",
content: payload.jokePrompt,
},
],
}
);
const joke = await io.openai.backgroundCreateChatCompletion("generate-jokes", {
model: "gpt-3.5-turbo",
messages: [
{
role: "user",
content: payload.jokePrompt,
},
],
});
return joke.choices;
},
@@ -89,8 +86,7 @@ new Job(client, {
## Parameters
<ParamField body="client" type="object" required>
An instance of [TriggerClient](/sdk/triggerclient) that is used to send events
to the Trigger API.
An instance of [TriggerClient](/sdk/triggerclient) that is used to send events to the Trigger API.
</ParamField>
<ParamField body="options" type="object" required>