client.defineJob({
  id: "alert-on-new-github-issues",
  name: "Alert on new GitHub issues",
  version: "1.0.0",
  trigger: github.triggers.repo({
    event: events.onIssueOpened,
    owner: "triggerdotdev",
    repo: "trigger.dev",
  }),
  run: async (payload, io, ctx) => {
    const record = await io.runTask(
      "sync-github-issue",
      async (task) => {
        return prisma.githubIssues.create({
          data: {
            number: payload.issue.number,
            title: payload.issue.title,
            body: payload.issue.body,
            url: payload.issue.html_url,
            repo: payload.repository.full_name,
            owner: payload.repository.owner.login,
          },
        });
      },
      //this is optional
      { name: "Sync GitHub Issue", icon: "github" }
    );
  },
});

A Task is a cached unit of work in a Job Run that are logged to the Trigger.dev UI. You can use io.runTask() to create and run a Task inside a Run.

Any interaction with an external service (database or API) should be wrapped in a Task. Failing to do so could result in repeated work when runs are resumed.

Parameters

cacheKey
string
required

Should be a stable and unique cache key inside the run(). See resumability for more information.

callback
function
required

The callback that will be called when the Task is run, this is where your logic should go. The callback receives the Task and the IO as parameters.

options
object

The options of how you’d like to run and log the Task.

onError
function

An optional callback that will be called when the Task fails. You can perform logic in here and optionally return a custom error object. Returning an object with { retryAt: Date, error?: Error } will retry the Task at the specified Date. You can also just return a new Error object to throw a new error. Returning null or undefined will rethrow the original error. If you want to force retrying to be skipped, return { skipRetrying: true }.

Returns

A Promise that resolves with the returned value of the callback.

If the remote callback feature options.callback is enabled, the Promise will instead resolve with the body of the first request sent to task.callbackUrl.