Easiest Way to Create Job Queue in Sails with sails-hook-job-queue

Author - Nirav Adatiya

Queue in Sails with Kue and Redis

Kue based queue in sails v1.1.0+. It’s a wrapper around Kue for processing published jobs by using Redis as a queue engine.

Why and when to use?

Usually, we need to send emails and SMS to the newly registered users for verification in the queue. Sending email or SMS takes too long to complete so it’s always better to do that task in the background. I have created this plugin for myself but it was fun to publish in npm as contributing to open source community. If we search in google for a queue in sails, there is already one package sails-hook-queue, but I found it’s really old and very complex to set it up.

With our job-queue hook, it’s really easy to configure and use. All you need to do is just create jobs.js inside api/service and declare a function inside ‘_processor’ object, and that’s it. You don’t need to declare or require it anywhere, it will register as hook itself.

Here is the step by step guide to do it :-

What are the Dependencies?

Redis : you need to install globally, and make sure it’s running in background.
Kue : it will install itself.

Installation and Usage

$ npm i @logisticinfotech/sails-hook-job-queue

First we need to export this params globally. We can declare it inside our development.js or production.js (config/env)

Here I am assuming that Redis server is running at in your system.

 redis_url: 'redis://'

 // or you can add your own

 redis_url: 'redis://user:[email protected]:6379/testjob'

Now we need to create Jobs.js file inside api/services folder, then export _processors:{} object.
Inside _processors we will declare a function named demoJob. you can declare all functions here for example sendSMS, sendMail.

 module.exports = {
   _processors: {
     demoJob: function (job, cb) {
       sails.log.info("Job, job is done =>" , job.data.myParamKey);

After that create an action for testing a job and add the entry to our routes object.

$ sails generate action test-job

'GET /test-job': { action: 'test-job' }, // Add this in route.js

Here we will use ‘demoJob‘ that we already declared inside _processors to test  job .
Add following inside test-job.js. Now do Sails lift and browse localhost:1337/test-job,  you should find a log like this:
“Job ‘demoJob’ (ID: 20) completed successfully.” inside terminal window.
Additionally you can add priority and attempt count along with create.

    .save(function (err) {
       return exits.success(); 

// With attempt count & priority

 Jobs.create("demoJob", {myParamKey:'myParamValue'})
    .save(function (err) {
       return exits.success();

Free SEO Checker |
Test your website for free with OnAirSEO.com

Get Your SEO report!

Don’t miss the next post!


Related Posts