Guides / Monitoring

How to Monitor Background Jobs in Node.js

3 min read·Monitoring

Setup

bash
npm install logit

With BullMQ

ts
import { Worker } from "bullmq";
import { init } from "logit";

const logit = init({ token: process.env.LOGIT_API_KEY! });

const worker = new Worker("emails", async (job) => {
  const start = Date.now();

  try {
    await sendEmail(job.data as { to: string; subject: string });

    await logit.now("jobs", {
      event: "Email job completed",
      description: `${(job.data as { to: string }).to}`,
      icon: "✉️",
      notify: false,
      tags: { job: job.name, status: "success" },
      metadata: { jobId: job.id, duration: Date.now() - start },
    });
  } catch (err) {
    const error = err instanceof Error ? err : new Error(String(err));

    await logit.now("jobs", {
      event: `Job failed: ${job.name}`,
      description: error.message,
      icon: "❌",
      notify: true,
      tags: { job: job.name, status: "failed" },
      metadata: { jobId: job.id, error: error.message },
    });

    throw err;
  }
});

With Inngest

ts
import { inngest } from "@/inngest/client";
import { init } from "logit";

const logit = init({ token: process.env.LOGIT_API_KEY! });

export const sendWelcomeEmail = inngest.createFunction(
  { id: "send-welcome-email" },
  { event: "user/signup" },
  async ({ event }) => {
    await sendEmail({ to: event.data.email, subject: "Welcome!" });

    await logit.now("jobs", {
      event: "Welcome email sent",
      description: event.data.email,
      icon: "✉️",
      notify: false,
      tags: { job: "send-welcome-email" },
    });
  }
);

Tip

Log job duration in metadata.duration (milliseconds). After a week, you'll have a baseline — and you'll immediately notice when a job starts taking 3× longer.

Try LogIt free

7-day trial. No credit card required.

Start free