RomainLanz / adonis-bull-queue

Queue system based on BullMQ for AdonisJS
MIT License
143 stars 25 forks source link

Dev-mode Suggestion #39

Open mattstrayer opened 5 months ago

mattstrayer commented 5 months ago

The option to run the jobs synchronously in development would be really nice.

Is this possible with the current state of the package?

RomainLanz commented 5 months ago

Yup, it could be doable, same with https://github.com/RomainLanz/adonis-bull-queue/issues/20

mattstrayer commented 5 months ago

For now I just made a BaseJob that all other jobs inherit from

import Application from '@ioc:Adonis/Core/Application'
import { JobHandlerContract, Queue, type Job } from '@ioc:Rlanz/Queue'

export default class BaseJob implements JobHandlerContract {
  // needs to be implemented in each job, imports cannot be inherited properly
  public static get $$filepath(): string {
    // must be implemented by subclass
    throw new Error('Must be implemented by subclass')
  }

  constructor(public job: Job) {
    this.job = job
  }

  public static async run(payload) {
    const job = new this(payload)

    if (Application.inDev) {
      job.handle(payload)
    } else {
      Queue.dispatch(this.$$filepath, payload)
    }
  }

  /**
   * Base Entry point
   */
  public async handle(payload: any) {}

  /**
   * This is an optional method that gets called if it exists when the retries has exceeded and is marked failed.
   */
  public async failed() {}
}

so I'll just call the class method of SomeJob.run() and that will either queue it up or run it synchronously.

RomainLanz commented 4 months ago

What do you think of having a flag in the configuration?

defineConfig({
  instant: true // or immediate or sync or ...
})

(I am not sure about the name)

mattstrayer commented 4 months ago

I like that approach! Easily configurable per environment which would be great :)