{ sailsit }

Sails Js Tutorials for Beginners and Forum

Have a Question?

If you have any question you can ask below or enter what you are looking for!

Schedule Jobs

1. Creating SailsJs Project

Follow the below tutorial for setting up SailsJs
Setting up sails project

2. Installing Packages

For creating jobs we need to install sails-hook-jobs package, this package is available in npm.
To install the package use the below command

npm install sails-hook-jobs --save

 

3. Database Setup

We will also need mongoDB for sails-jobs as, sails-hook-jobs uses mongoDB to store job configuration
You can follow the below tutorial for setting up mongoDB
Setting up mongoDB

4. Configuring Jobs

Create a file jobs.js (if does not exist) in config directory
Paste the below configuration in config/jobs.js file

module.exports.jobs = {

  	// Define the directory where jobs are stored
  	"jobsDirectory": "api/jobs",

  	"db": {
           "address"	 : "localhost:27017", //monogDB details
    	   "collection" : "sailsjobs"
  	},
  	"name": "SailsJs Jobs",
  	"processEvery": "10 seconds",
  	"maxConcurrency": 20,
  	"defaultConcurrency": 10,
  	"defaultLockLifetime": 10000
};

As sails-hook-jobs uses agenda package, for more configuration details you can check agenda configuration at below URL
https://github.com/rschmukler/agenda

5. Creating Jobs

Create a directory jobs in api directory (if does not exist)
Note: This path should be defined in the job configuration file config/jobs.js

Create a file SailsitJob.js in api/jobs directory
Note: Jobs file name should end with Job, otherwise the package will not be able to access the job

Paste the below code in your SailsitJob.js file

module.exports = function(agenda) {
    var job = {

        // job name (optional) if not set,
        // Job name will be the file name or subfolder.filename (without .js)
        //name: 'Foo',

        // set true to disabled this job
        //disabled: false,

        // method can be 'every <interval>', 'schedule <when>' or now
        //frequency supports cron strings
        frequency: '*/2 * * * * *', //running every 2 seconds

        // Jobs options
        //options: {
            // priority: highest: 20, high: 10, default: 0, low: -10, lowest: -20
            //priority: 'highest'
        //},

        // Jobs data
        //data: {},

        // execute job
        run: function(job, done) {
	    SailsitJobServices.triggerJob();
            done();
        },
    };
    return job;
}

 

6. Creating Services

Create a service SailsitJobServices.js in api/services directory and copy the below code in it

module.exports = {

    triggerJob: function () {
        sails.log.info("=====Job Started=====");
        sails.log.debug(new Date());
        sails.log.info("=====Job Started=====\n\n");
    }
	
};

This function of the service will get triggered every 2 seconds, as we have defined it in out SailsitJob.js file

7. Running Sails

Now you can start your sails project using below command

sails lift

Once your sails application is loaded, the job will start automatically.
As per our code, it will start logging in console at every 2 seconds interval.

You can check the video above showing the running job service

You can find the source code for the tutorial at below URL
SailsScheduleJobs