我正在尝试使用heroku制定计划工作。
我用cron npm这就是为什么
#!/usr/bin/env node
var mongoose = require('mongoose');
var nodemailer = require('nodemailer');
var myNoti = require('./models/notificationSchema');
var mongoose = require('mongoose');
var CronJob = require('cron').CronJob;
config = {
mongoUrl:'mongodb://<myuser>:<mypass>@XXXX.mlab.com:XXXX/XXX'
};
new CronJob('38 00 * * *', function() {
console.log('You will see this message every second');
function sendMail()
{
//The server option auto_reconnect is defaulted to true
var options = {
server: {
auto_reconnect:true,
}
};
mongoose.Promise = global.Promise;
mongoose.connect(config.mongoUrl, options);
db = mongoose.connection;// a global connection variable
// Event handlers for Mongoose
db.on('error', function (err) {
console.log('Mongoose: Error: ' + err);
});
db.on('open', function() {
console.log('Mongoose: Connection established');
});
db.on('disconnected', function() {
console.log('Mongoose: Connection stopped, recconect');
mongoose.connect(config.mongoUrl, options);
});
db.on('reconnected', function () {
console.info('Mongoose reconnected!');
});
var email,Recommendation;
var today = new Date();
var dd = today.getDate();
var mm = today.getMonth()+1; //January is 0!
var yyyy = today.getFullYear();
if(dd<10) {
dd='0'+dd
}
if(mm<10) {
mm='0'+mm
}
var mytoday = dd+'/'+mm+'/'+yyyy;
console.log(mytoday);
myNoti.find({},function(err, docs){
console.log('in the find function');
for(var i=0; i<docs.length; i++)
{
email = docs[i].email;
recommendation = docs[i].Recommendation;
var tempDate = new Date(docs[i].dateNoti);
dd = tempDate.getDate();
mm = tempDate.getMonth()+1; //January is 0!
yyyy = tempDate.getFullYear();
if(dd<10) {
dd='0'+dd
}
if(mm<10) {
mm='0'+mm
}
tempDate = dd+'/'+mm+'/'+yyyy;
if(mytoday === tempDate)
{
console.log("i'm in the if!");
console.log("docs[i].repeat " +docs[i].repeat);
today.setDate(today.getDate() + Number(docs[i].repeat));
newupdate = new Date(today);
myNoti.update({email: email,Recommendation: recommendation},{dateNoti: newupdate},
function(err, num) {
if(err)
console.log(err);
else
console.log("updated"+num);
});
sendit(email, recommendation)
}
}
});
}
function sendit(email, recommendation)
{
var transporter = nodemailer.createTransport({
host: 'smtp.gmail.com',
port: 465,
secure: true, // use SSL
auth: {
user: 'xxxxx@gmail.com',
pass: 'xxxxxx'
}
});
// setup e-mail data
var mailOptions = {
from: '"MY MEDICAL " <XXXX@gmail.com>', // sender address (who sends)
to: email, // receiver
subject: 'Notification', // Subject line
text: '', // plaintext body
html: recommendation
};
// send mail with defined transport object
transporter.sendMail(mailOptions, function(error, info){
if(error){
return console.log(error);
}
console.log('Message sent: ' + info.response);
});
}
sendMail();
}, null, true, 'America/Los_Angeles');
此代码适用于我的本地主机,我的意思是当我只运行我的服务器(没有运行此特定文件)它工作 - 我上传所有文件到heroku但这个cron不起作用,我没有收到我的电子邮件。 ..
我该怎么办?
答案 0 :(得分:0)
将您的功能放入公共网址。
在Heroku上添加Temporize Scheduler加载项以运行cron作业。 (定期api电话)。
配置Temporize以调用您的URL以发送电子邮件以及每天要检查的次数,例如每天下午1点。 (使用cron job sintax)
如果您想将其设为私有,请从Temporize生成登录令牌并在您的函数中验证该令牌。