我正在与Sailsjs和Sails-Hook-Cron合作。
我想设置config/cron.js文件的"附表" var,其中来自MySQL查询到DB的变量。
可能吗?
谢谢。
不是真的,好吧,如果您想动态启动Cron作业,这可能是您希望从DB中启动信息的唯一原因。p>相反,您可以使用服务。一个非常基本的示例是在API/Services文件夹中创建一个名为Cronservice.js的文件。
// CronService.js
var CronJob = require('cron').CronJob;
module.exports = {
startJob : function(time) {
new CronJob(time, function() {
// does whatever you need to do.
}, null, true);
}
}
然后可以在控制器中调用此服务。例如
startCronService : function(req, res, next) {
// Call mysql db and get the schedule data.
// Set the time variable with the schedule data and start the job
var time = '10 * * * * *';
CronService.startJob(time);
res.ok();
},
有关使用CRON软件包的完整详细信息,请参见https://www.npmjs.com/package/cron