Background jobs

This guide will walk you through creating background jobs with retries in a few minutes. Background tasks in inngest are easy because:

  • You don’t need to create queues, workers, or subscriptions
  • You can run background jobs on serverless functions without setting up infrastructure
  • You can enqueue jobs to run in the future, similar to a task queue, without any config.

How to create background jobs

There are two parts to background jobs:

  1. Creating the function that runs in the background
  2. Triggering the function

Let's get started on a background job that sends an email to new signups. This code creates a new Inngest function as outlined in the function documentation:

import { Inngest } from "inngest";

const inngest = new Inngest({ name: "Signup flow" });

export const fn = inngest.createStepFunction(
  "Send signup email",   // Function name
  "user/created",        // Trigger
  ({ event, tools }) => {

    // This runs in the background any time the `user/created` event is
    // sent to Inngest.
    //
    // Whatever event data you send is accessible within `event`.

    tools.run("send the user a signup email", async () => {
      // Reliably send an email.  Anything in `tools.run` automatically retries
      await sesclient.clientsendEmail({
        to: event.data.user_email,
        subject: "Welcome to Inngest!"
        message: "...",
      });
    });

    // You can schedule work for the future by sleeping within functions.
    // NOTE: This actually *****stops***** functions and continues execution automatically,
    // across server restarts or serverless functions.  You don't have to worry about
    // scale, memory leaks, connections, or restarts.
    tools.sleepUntil("2023-02-01T16:30:00");

    tools.run("Do some work in the future", async () => {
      // Code here runs in the future automatically.
    });
  }
);

You can trigger this background function by sending an event to Inngest:

inngest.send({
  name: "user/created", // This matches the event used in `createFunction`
  data: {
    email: "test@example.com",
  },
});

When you send an event to Inngest, we automatically find any functions that are triggered by the event name and automatically run those functions in the background. The entire JSON object you pass in to inngest.send will be available to your functions.