/adonis-queue

An addon/plugin package to provide driver-based job queueing services in AdonisJS 4.0+

Primary LanguageJavaScriptMIT LicenseMIT

adonis-queue

An addon/plugin package to provide driver-based job queueing services in AdonisJS 4.0+

NPM Version Build Status Coveralls

Getting Started

    adonis install adonisjs-queue

Usage

Add a job file to the jobs folder using the command. The command below creates the file app/Jobs/SendEmail.js

    $ adonis make:job SendEmail
const Job = use('Job')
const Mail = use('Mail')

class SendEmail extends Job {

	static get driver() {
		return 'redis'
	}
	
    	static get queue(){
		return 'low'
    	}
    
	constructor(emailAddress, emailFrom, emailSubject, emailBody) {
		super(arguments)

		this.timeOut = 50; // seconds
		this.retryCount = 3;
		this.retryUntil = 200; // seconds
	}

	async handle(link, done) {
		//....
		console.log(`Job [${this.constructor.name}] - handler called: status=running; id=${this.id} `)
    
		link.reportProgress(10)

		let _data = this

		await Mail.send(_data.emailBody, {gender:'F', fullname:"Aisha Salihu"}, (message) => {
			message.to(_data.emailAddress) 
			message.from(_data.emailFrom) 
			message.subject(_data.emailSubject)
		})

		//...
		link.reportProgress(100)
	}

	async failed(error) {
    
		console.log(`Job [${this.constructor.name}] - status:failed; id=${this.id} `, error)
	}
}

module.exports = SendEmail

Open the start/events.js file of an AdonisJS Framework installation and add the following code to it (This package encourages the use of the standard event bus for AdonisJS)

'use strict'

const Event = use('Event')
const Queue = use('Queue')

const SendEmail = use('App/Jobs/SendEmail')

Event.on('user_registered', async () => {
    let job = await Queue.dispatch(new SendEmail(
    	'queensaisha04@gmail.com',
	'support@example.com',
	'YOU ARE WELCOME',
	'emails.template' // AdonisJS view template file in "resources/views"
    ))
})

License

MIT

Running Tests

    npm i
    npm run lint
    
    npm run test

Credits

Contributing

See the CONTRIBUTING.md file for info