Running Scheduled Jobs
Learn how to create and manage background jobs using Inngest in your Indie Kit application
Let's learn how to create and manage background jobs using Inngest! 🚀
Creating Background Jobs 🛠️
-
Create a new function in
src/lib/inngest/functions/hello-world.ts
:import { inngest } from "@/lib/inngest/client"; // Define your function export const helloWorld = inngest.createFunction( { name: "Hello World" }, { event: "test/hello.world" }, async ({ event, step }) => { await step.sleep("wait-a-bit", "1m"); return { message: "Hello, World!" }; } );
-
Register your function in
src/lib/inngest/functions/index.ts
:import { helloWorld } from "./hello-world"; export const functions = [ helloWorld, // Add more functions here ];
Sending Events 📤
Trigger your background jobs by sending events:
import { inngest } from "@/lib/inngest/client";
// Send an event
await inngest.send({
name: "storefront/cart.checkout.completed",
data: {
cartId: "ed12c8bde",
itemIds: ["9f08sdh84", "sdf098487"],
account: {
id: 123,
email: "test@example.com",
},
},
});
Monitoring Jobs 📊
-
Local Development:
- Visit Inngest dashboard at
http://localhost:8288
- Monitor job executions
- Debug events and functions
- Visit Inngest dashboard at
-
Production:
- Use Inngest Cloud dashboard
- Set up alerts
- View execution history
Advanced Features 🎯
Events Guide
Learn about event structure, types, and best practices for sending events in Inngest.
Background Jobs
Create reliable background jobs with retries, error handling, and monitoring.
Fan Out Jobs
Process multiple items in parallel with fan-out pattern.
Multiple Triggers
Trigger functions from multiple events or conditions.
Rate Limiting
Control job execution rates and prevent overload.
Sleep Steps
Add delays and schedule future actions in your jobs.
Best Practices 💡
-
Function Organization
- One function per file
- Clear, descriptive names
- Register all functions in
index.ts
-
Event Design
- Use consistent naming
- Include necessary data
- Version events if needed
-
Error Handling
- Implement retries
- Log errors properly
- Set up monitoring
-
Testing
- Test locally first
- Use development environment
- Monitor execution times
Example Use Cases 🌟
- Send welcome emails
- Process uploads
- Generate reports
- Clean up old data
- Schedule notifications
- Sync external services
Now your Indie Kit application is ready to handle background jobs efficiently! 🎉