Open mattstrayer opened 5 months ago
Yup, it could be doable, same with https://github.com/RomainLanz/adonis-bull-queue/issues/20
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.
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)
I like that approach! Easily configurable per environment which would be great :)
The option to run the jobs synchronously in development would be really nice.
Is this possible with the current state of the package?