2017-05-27 93 views
0

我正在嘗試使用heroku作出調度作業。使用heroku調度作業節點js

我用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: '[email protected]', 
     pass: 'xxxxxx' 
    } 
}); 

// setup e-mail data 
var mailOptions = { 
    from: '"MY MEDICAL " <[email protected]>', // 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
  1. 把你的函數放在一個public url中。

  2. 添加時勢調度附加在Heroku上運行cron作業。 (定期API調用)。

  3. 配置拖延打電話給你的網址,每天下午1時發送電子郵件多少次你要檢查的日子,例如。 (使用cron作業sintax)

  4. 如果你想讓它保密,請從Temporize生成一個登錄令牌並在你的函數中驗證該令牌。