Skip to content

falcondev-oss/queue

Repository files navigation

@falcondev-oss/queue

Type-safe job queues built on BullMQ and Standard Schema.

Installation

npm add @falcondev-oss/queue

Usage

1. Define your job schemas and handlers

const jobs = {
  video: {
    process: defineJob({
      schema: z.object({
        path: z.string(),
        outputFormat: z.enum(['mp4', 'avi', 'mov']),
      }),
      async run(payload) {
        await processVideo(payload.path, payload.outputFormat)
      },
      workerOptions: {
        // only one video processed at a time
        concurrency: 1,
      },
    }),
  },
  sendEmail: defineJob({
    schema: z.object({
      to: z.email(),
      subject: z.string(),
      body: z.string(),
    }),
    async run(payload) {
      await sendEmail(payload.to, payload.subject, payload.body)
    },
  }),
}

2. Create queue client

You only need the type of your job definitions, so it's easier to use across your entire backend stack without any runtime dependencies.

const queue = createQueueClient<typeof jobs>()

2. Queue jobs

Single jobs:

await queue.video.process.add({
  outputFormat: 'mp4',
  path: '/path/to/video.mov',
})
await queue.sendEmail.add({
  to: '[email protected]',
  subject: 'Hello',
  body: 'This is a test email',
})

Bulk jobs:

await queue.sendEmail.addBulk([
  {
    payload: {
      to: '[email protected]',
      subject: 'Hello',
      body: 'This is a test email',
    },
  },
  {
    payload: {
      to: '[email protected]',
      subject: 'Hello 2',
      body: 'This is another test email',
    },
    opts: {
      attempts: 5, // override default job options
    },
  },
])

3. Start workers

await startWorkers(jobs)

About

Type-safe job queues built on BullMQ and Standard Schema.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 2

  •  
  •