When you schedule work for the future there's always a chance that you might need to cancel or reschedule these jobs. For example, if you're scheduling a product to go live at a specific time, you might end up changing the go-live time multiple times. You need a way to cancel work that's already scheduled.
Rescheduling is very similar. First, you cancel the current work. Then, you schedule a new function.
How to implement this pattern
Inngest allows you to automatically cancel running or paused/sleeping functions via events:
typescriptimport { inngest } from "./client";const func = inngest.createFunction({id: "schedules-and-cancels-automatically",cancelOn: [// The data.postID field in both events must match.{ event: "blog/post.cancelled", match: "data.postID" },],},{ event: "blog/post.scheduled" },({ event, step }) => {// Sleep until the scheduleAt time in the event.await step.sleepUntil("wait-for-scheduleAt", event.data.scheduleAt);await step.run("publish-post", () => {// Publish the post.publishBlogPost(event.data.postID);});});
In the above example, we define a function that runs every time a blog/post.scheduled
event is received. This functionwaits until the post schedule time, then runs a function to publish the post.
The function also defines a cancellation policy: any time the blog/post.cancelled
event is received and the event has the same data.postID values, the function will be cancelled.
You can send a blog/post.cancelled
event at any time before the scheduledAt
time to prevent the original function from running. To reschedule, send another blog/post.scheduled
event after the cancellation event.
Alternative systems
In typical job systems, you'll need to record the job ID each time you schedule the job, then use the system's API to cancel the work. If the job is already running, you might not be able to cancel the function.