An Ironium provider for the Adonis 4.x framework.
Easy integration of job queues backed by beanstalkd, ironMQ, and Amazon SQS
See instructions.md
adonis install adonis-ironium
Command | Description |
---|---|
adonis make:job |
Make a new Job Queue |
adonis ironium:listen |
Start the queue listener |
At it's most basic, a job queue provides an async handle
method that job data is passed into, upon which it
operates. This could be anything from simply the user id for a new user we want to send a welcome email,
or a collection of data needing further processing that doesn't make sense to occur during a regular
user request.
The handle
method is the only requirement!
Name | Required | Type | Static | Description |
---|---|---|---|---|
handle | true | function | false | An async function that is called for this job. |
The only real limitation is job payload size, which is dictated by the queue backend:
- AWS and IronMQ: 256k
- beanstalkd: 64k by default, but configurable
NOTE:
adonis make:job MyQueue
will create a job class in app/Jobs
, these are automatically registered and accessibly simply by specifying the class name as the queue name when dispatching a job.
ie: ironium.dispatch('MyQueue', { payload_data: 'goes here' })
Dispatching jobs is pretty straightforward...
const ironium = use('Ironium')
const queueName = 'Example'
const job = {
message: 'This is my test job!'
}
const jobID = ironium.dispatch(queueName, job)
It will return a job ID, and process your job behind the scenes.
You can also pass in an array of jobs, and optionally specify a delay in a format parsable by ms:
const ironium = use('Ironium')
const queueName = 'Example'
const delay = '2hr'
const job = {
message: 'This is my test job!'
}
const jobID = ironium.dispatch(queueName, job, delay)
const ironium = use('Ironium')
const queueName = 'Example'
const jobs = [
{ message: 'This is my test job!' },
{ message: 'Another test job!' },
]
const jobID = ironium.dispatch(queueName, jobs)
Queued jobs won't process until you fire up one or more queue workers with the ironium:listen
command.
If you wish to dispatch jobs from within jobs, use
does not appear to behave as one might expect,
so instead you should use ioc.make()
like so:
const { ioc } = require('@adonisjs/fold')
class Example {
async handle (job) {
const Ironium = ioc.make('Ironium')
const Logger = ioc.make('Logger')
const jobId = await Ironium.dispatch('AnotherJob', anotherPayload)
Logger.info('Dispatching Job(s): ', jobId)
return
}
}
Heavily inspired by Adonis Kue, thanks Nick Rempel for that!
Also to Harminder Virk for creating Adonis.