Cancellation

Cancellation is a useful mechanism for preventing unnecessary actions based on previous actions (ex, skipping a report generation upon an account deletion) or stopping an unwanted function run composed of multiple steps (ex, deployment mistake, duplicates).

Inngest enables you to cancel running Functions via the API, Dashboard, or based on events:

Anatomy of a cancellation

Inngest cancellation mechanisms prevent a scheduled Function run from running or stop an ongoing Function run between some following steps (sleep or action steps).

Please note that:

  • Cancelling a function that has a currently executing step will not stop the step's execution. Any actively executing steps will run to completion.
  • Canceling a set of Function runs does not prevent new Function runs from being enqueued (ex, in case of loop issues). Consider using Functions Pausing instead.

Consider the below Inngest Function:

const scheduleReminder = inngest.createFunction(
  {
    id: "schedule-reminder",
    cancelOn: [{ event: "tasks/deleted", if: "event.data.id == async.data.id" }],
  }
  { event: "tasks/reminder.created" },
  async ({ event, step }) => {
    // Step 1
    await step.sleepUntil('sleep-until-remind-at-time', event.data.remindAt);
    // Step 2
    await step.run('send-reminder-push', async ({}) => {
      await pushNotificationService.push(event.data.userId, event.data.reminderBody)
    })
  }
  // ...
);

Let's now look at two different cancellations triggered from the Dashboard Bulk Cancellation UI:

We cancel the Function run before or after Step 1

  1. The Function Run gets picked up by Inngest
  2. The Step 1 is processed, triggering a sleep until the following week
  3. Three days after, a cancellation is received
  4. The Function run is canceled (Step 2 is skipped)

We cancel the Function run when Step 2 is running

  1. The Function Run gets picked up by Inngest
  2. The Step 1 is processed, triggering a sleep until a next week
  3. A week after, a cancellation is received but the Step 2 is already started
  4. The Step 2 runs until completion
  5. The Function run is marked as "canceled"

All canceled Function runs can be replay by using the Platform's Functions Replay UI.