A Kue provider for the Adonis framework.
This library provides an easy way to get started with an asynchronous job queue for AdonisJS.
npm install --save adonis-kue
Register it in bootstrap/app.js
:
const providers = [
...
'adonis-kue/providers/KueProvider'
]
Also consider adding an alias to validation provider.
const aliases = {
...
Kue: 'Adonis/Addons/Kue'
}
Register the commands:
const aceProviders = [
...
'adonis-kue/providers/CommandsProvider'
];
...
const commands = [
...
'Adonis/Commands/Kue:Listen'
];
Add a configuration file in config/kue.js
. For example:
'use strict';
const Env = use('Env');
module.exports = {
removeOnComplete: true,
connection: {
prefix: 'q',
redis: Env.get('REDIS_URL')
}
};
See the Kue Documentation for more connection options.
Starting an instance of the kue listener is easy with the included ace command. Simply run ./ace kue:listen
.
The provider looks for jobs in the app/Jobs
directory of your AdonisJS project and will automatically register a handler for any jobs that it finds.
Jobs are easy to create. They live in app/Jobs
and they are a simple class. They expose the following properties:
Name | Required | Type | Static | Description |
---|---|---|---|---|
concurrency | false | number | true | The number of concurrent jobs the handler will accept |
key | true | string | true | A unique key for this job |
handle | true | function | false | A function that is called for this job. |
Now that your job listener is running and ready to do some asynchronous work, you can start dispatching jobs.
const kue = use('Kue');
const Job = require('./app/Jobs/Example');
const data = { test: 'data' };
const job = kue.dispatch(Job.key, data);
// If you want to wait on the result, you can do this
const result = yield job.result;
Special thanks to the creator(s) of AdonisJS for creating such a great framework.