pp31630
pp31630

Reputation: 41

How do I run a service at a specific time in SailsJS

I'm looking for a way to fire a service at a specific time in SailsJS. In this case, I have an Event model which includes two attributes: a boolean which is default to be false and a datetime which is assigned whenever an event is created. Also, there is an endEvent service to be fired according to the datetime.

I want my whole process be like, for every created event:

  1. Initialize the boolean attribute to false.
  2. Set the datetime into a future time.
  3. Run the service according to the datetime which set the boolean attribute from false to true.

My model:

// Event.js
module.exports = {
  attributes: {
    end: {
      type: 'boolean'
    },
    deadline: {
      type: 'datetime'
    }
  }
}

My service:

// endEvent.js
module.exports = {

  setEnd: function(event) {
    Event.findOne(event).exec(function (err, result) {
      result.end = true;
      result.save();
    });
  }
};

However I cannot find the proper way to run a service in a specific time.

Thanks in advance for your help!

Upvotes: 0

Views: 157

Answers (1)

elssar
elssar

Reputation: 5871

sailsjs has no support for task scheduling out of the box. You will have to use a package like node-cron or agenda or kue and build your own solution. Also, there is a sails hook, sails-hook-schedule which you can try out.

Upvotes: 1

Related Questions