Cron 我想在我的sails中包含节点调度代码

Cron 我想在我的sails中包含节点调度代码,cron,sails.js,Cron,Sails.js,我想在我的sail中包含节点调度代码,但我不知道我把代码放在了我的sail中的什么地方。 但是我试着把代码放在我的config/bootstrap.js中,但是它没有运行。代码是 sails.on('lifted', function() { var schedule = require('node-schedule'); var j = schedule.scheduleJob({hour: 0, minute: 1, dayOfWeek: 0}, func

我想在我的sail中包含节点调度代码,但我不知道我把代码放在了我的sail中的什么地方。 但是我试着把代码放在我的config/bootstrap.js中,但是它没有运行。代码是

sails.on('lifted', function() {
var schedule = require('node-schedule');

                 var j = schedule.scheduleJob({hour: 0, minute: 1, dayOfWeek: 0}, function(){


                            console.log('Time for tea!');
                            });

  });

我想知道,我把代码放在哪里了。主要条件是,每当我的sails服务器提升时,该文件都会执行。

很抱歉,因为我在平板电脑上,所以它没有格式化代码。这是出人意料的简单。我使用了节点cron,并将以下代码作为一个名为cron.js的文件放在我的services文件夹中

我不相信我必须在其他地方放置任何代码才能开始工作

/*
https://github.com/ncb000gt/node-cron
Read up on cron patterns here.  http://crontab.org/


When specifying your cron values you'll need to make sure that your values fall within the ranges. For instance, some cron's use a 0-7 range for the day of week where both 0 and 7 represent Sunday. We do not.

Seconds: 0-59
Minutes: 0-59
Hours: 0-23
Day of Month: 1-31
Months: 0-11
Day of Week: 0-6



How to check if a cron pattern is valid:
    try {
        new CronJob('invalid cron pattern', function() {
            console.log('this should not be printed');
        })
    } catch(ex) {
        console.log("cron pattern not valid");
    }

constructor(cronTime, onTick, onComplete, start, timezone, context) - Of note, the first parameter here can be a JSON object that has the below names and associated types (see examples above).
cronTime - [REQUIRED] - The time to fire off your job. This can be in the form of cron syntax or a JS Date object.
onTick - [REQUIRED] - The function to fire at the specified time.
onComplete - [OPTIONAL] - A function that will fire when the job is complete, when it is stopped.
start - [OPTIONAL] - Specifies whether to start the job just before exiting the constructor. By default this is set to false. If left at default you will need to call job.start() in order to start the job (assuming job is the variable you set the cronjob to).
timeZone - [OPTIONAL] - Specify the timezone for the execution. This will modify the actual time relative to your timezone.
context - [OPTIONAL] - The context within which to execute the onTick method. This defaults to the cronjob itself allowing you to call this.stop(). However, if you change this you'll have access to the functions and values within your context object.
start - Runs your job.
stop - Stops your job.

*/

var CronJob = require('cron').CronJob;
var job = new CronJob({
  cronTime: '00 30 11 * * 1-5',
  onTick: function() {
    // Runs every weekday (Monday through Friday)
    // at 11:30:00 AM. It does not run on Saturday
    // or Sunday.
  },
  start: false,
  timeZone: "America/Los_Angeles"
});
job.start()

)

正如您正确地说的,config/bootstrap.js是一个可以使用作业调度器的地方

下面的cron将在每天00小时00分00秒执行 从左边

  • 前00秒为可容纳值的秒数(00-59)

  • 第二个00是可以保存值的分钟数(00-59)

  • 第三个00是可以保存值的小时数(00-23)

  • 第四个位置*是日可容纳值(00-30)

  • 第五个位置*是可以保存值的月份(00-11)

  • 第六个位置*是星期几可以保存的值(0-6)

  • 注意:您正在使用的函数不应该有任何res.json或res.something

    module.exports.bootstrap = function (cb) {
      try {
        var CronJob = require('cron').CronJob;
        new CronJob('00 00 00 * * *', function() {
          sails.controllers.controllerName.functionName(sails.request, sails.response, sails.next, function(err,data){
            console.log(err,"err");
          });
        }, null, true);
      } 
      catch(ex) {
        console.log("cron pattern not valid");
      } 
      cb();
    };