Skip to content

reg2005/adonis5-bullmq

Repository files navigation

Table of contents

adonis5-bullmq

Tagline

npm-image license-image typescript-image

BullMQ provider for Adonis5

Installation

npm i adonis5-bullmq
node ace invoke adonis5-bullmq

Usage

Create your props interface and enums

// file: Contracts/QueueInterfaces.ts
export interface TestProps {
  name: string
}
export enum QueueNamesEnum {
  'TestJob' = 'TestJob'
}

Create queue listener

// file commands/QueueListener.ts
import BullMQ from '@ioc:Adonis/Addons/BullMQ'
import {TestProps, QueueNamesEnum} from 'Contracts/QueueInterfaces'

export default class QueueListener extends BaseCommand {
  /**
   * Command Name is used to run the command
   */
  public static commandName = 'queue:listener'

  public static settings = {
    loadApp: true,
  }

  run(){
    BullMQ.worker<TestProps, TestProps>(QueueNamesEnum.TestJob, async (job) => {
      console.log(job.data)
      // handle your job
      return job
  })
  }
}

Emit job

// anyAppFile.ts
import BullMQ from '@ioc:Adonis/Addons/BullMQ'
import {TestProps, QueueNamesEnum} from 'Contracts/QueueInterfaces'

const queue = BullMQ.queue<TestProps, TestProps>(QueueNamesEnum.TestJob)
export default class IndexController {
  async send(){
    await queue.add('mytestJob', { name: 'anyName' })
  }
}