DataQueueDataQueue
Usage

Cancel Jobs

You can cancel a job by its ID, but only if it is still pending (not yet started or scheduled for the future).

@/app/api/cancel-job/route.ts
import { NextRequest, NextResponse } from 'next/server';
import { getJobQueue } from '@/lib/queue';

export async function POST(request: NextRequest) {
  try {
    const { jobId } = await request.json();
    const jobQueue = getJobQueue();
    await jobQueue.cancelJob(jobId);
    return NextResponse.json({ message: 'Job cancelled' });
  } catch (error) {
    console.error('Error cancelling job:', error);
    return NextResponse.json(
      { message: 'Failed to cancel job' },
      { status: 500 },
    );
  }
}

Cancel All Pending Jobs

DataQueue also lets you cancel all pending jobs at once. This is useful if you want to stop all jobs that haven't started yet or are scheduled for the future.

@/app/api/cancel-all-jobs/route.ts
import { NextRequest, NextResponse } from 'next/server';
import { getJobQueue } from '@/lib/queue';

export async function POST(request: NextRequest) {
  try {
    const jobQueue = getJobQueue();
    const cancelledCount = await jobQueue.cancelAllUpcomingJobs();
    return NextResponse.json({ message: `Cancelled ${cancelledCount} jobs` });
  } catch (error) {
    console.error('Error cancelling jobs:', error);
    return NextResponse.json(
      { message: 'Failed to cancel jobs' },
      { status: 500 },
    );
  }
}

Cancel Jobs by Filter

You can also cancel only the pending jobs that match certain criteria:

// Cancel only email jobs
await jobQueue.cancelAllUpcomingJobs({ jobType: 'email' });

// Cancel only jobs with priority 2
await jobQueue.cancelAllUpcomingJobs({ priority: 2 });

// Cancel only jobs scheduled for a specific time (exact match)
const runAt = new Date('2024-06-01T12:00:00Z');
await jobQueue.cancelAllUpcomingJobs({ runAt });

// Cancel jobs scheduled after a certain time
await jobQueue.cancelAllUpcomingJobs({
  runAt: { gt: new Date('2024-06-01T12:00:00Z') },
});

// Cancel jobs scheduled on or after a certain time
await jobQueue.cancelAllUpcomingJobs({
  runAt: { gte: new Date('2024-06-01T12:00:00Z') },
});

// Cancel jobs scheduled before a certain time
await jobQueue.cancelAllUpcomingJobs({
  runAt: { lt: new Date('2024-06-01T12:00:00Z') },
});

// Cancel jobs scheduled on or before a certain time
await jobQueue.cancelAllUpcomingJobs({
  runAt: { lte: new Date('2024-06-01T12:00:00Z') },
});

// Cancel jobs scheduled exactly at a certain time
await jobQueue.cancelAllUpcomingJobs({
  runAt: { eq: new Date('2024-06-01T12:00:00Z') },
});

// Cancel jobs scheduled between two times (inclusive)
await jobQueue.cancelAllUpcomingJobs({
  runAt: {
    gte: new Date('2024-06-01T00:00:00Z'),
    lte: new Date('2024-06-01T23:59:59Z'),
  },
});

// Combine runAt with other filters
await jobQueue.cancelAllUpcomingJobs({
  jobType: 'email',
  runAt: { gt: new Date('2024-06-01T12:00:00Z') },
});

// Cancel all jobs with both 'welcome' and 'user' tags. The jobs can have other tags.
await jobQueue.cancelAllUpcomingJobs({
  tags: { values: ['welcome', 'user'], mode: 'all' },
});

// Cancel all jobs with any of the tags. The jobs can have other tags.
await jobQueue.cancelAllUpcomingJobs({
  tags: { values: ['foo', 'bar'], mode: 'any' },
});

// Cancel all jobs with exactly the given tags. The jobs cannot have other tags.
await jobQueue.cancelAllUpcomingJobs({
  tags: { values: ['foo', 'bar'], mode: 'exact' },
});

// Cancel all jobs with none of the given tags
await jobQueue.cancelAllUpcomingJobs({
  tags: { values: ['foo', 'bar'], mode: 'none' },
});

// Combine filters
await jobQueue.cancelAllUpcomingJobs({
  jobType: 'email',
  tags: { values: ['welcome', 'user'], mode: 'all' },
  runAt: { lt: new Date('2024-06-01T12:00:00Z') },
});

runAt filter details:

  • You can pass a single Date for an exact match, or an object with any of the following keys:
    • gt: Greater than
    • gte: Greater than or equal to
    • lt: Less than
    • lte: Less than or equal to
    • eq: Equal to
  • All filters (jobType, priority, runAt, tags) can be combined for precise cancellation.

This will set the status of all jobs that are still pending (not yet started or scheduled for the future) and match the filters to cancelled.