Skip to content

Refactor: Modify API call parameters and response handling #1

@coder-tejas

Description

@coder-tejas

Issue Description

When using Inngest to trigger background jobs, you originally fetched run results using the Inngest API (/api/inngest/${eventId}), but the API call failed due to authentication issues (Bearer undefined) and Inngest returning metadata instead of actual data.

To simplify the process and remove dependency on Inngest’s result-fetching mechanism, you’ve decided to store the job results directly in your database (Neon + Drizzle ORM).

However, a challenge arises:

For successful runs, results are saved to the DB via the success handler.

For failed runs, no result is saved (since the failure might occur before the DB write).

Root Cause

Failed runs never hit the part of your code that saves the result because:

The job throws an error before db.insert() runs.

The Inngest API isn't reliable (auth issues, limited access to run state).

Proposed Solution

Implement a status-tracked job record in your DB.

Example schema
import { pgTable, text, timestamp, uuid } from "drizzle-orm/pg-core";

export const jobResults = pgTable("job_results", {
id: uuid("id").defaultRandom().primaryKey(),
eventId: text("event_id").notNull(),
status: text("status").default("pending"), // "pending", "success", "failed"
result: text("result"),
error: text("error"),
createdAt: timestamp("created_at").defaultNow(),
updatedAt: timestamp("updated_at").defaultNow(),
});

Workflow

When you trigger an Inngest function:

await db.insert(jobResults).values({
eventId,
status: "pending",
});

Inside the Inngest handler:

try {
const result = await doHeavyProcessing();
await db.update(jobResults)
.set({ status: "success", result: JSON.stringify(result) })
.where(eq(jobResults.eventId, eventId));
} catch (error) {
await db.update(jobResults)
.set({ status: "failed", error: String(error) })
.where(eq(jobResults.eventId, eventId));
throw error; // still let Inngest mark it failed
}

In your frontend:

Poll /api/result?eventId=...

Fetch the record from job_results

Display status accordingly (pending, success, or failed

Metadata

Metadata

Assignees

Labels

No labels
No labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions