【问题标题】:Cron job with node-cron will not console.log for a test使用 node-cron 的 Cron 作业不会使用 console.log 进行测试
【发布时间】:2018-04-03 10:39:34
【问题描述】:

我正在尝试在我的 node.js 应用程序中运行一个测试 cron 作业(一个仅用于此作业),但它不是应该的 console.log()。它应该每 10 秒console.log(),但它不会。

这是我的目录/应用程序中除 package.json 文件和 node_modules 目录之外的唯一文件。

代码在这里:

var express     = require("express"),
    app         = express(),
    bodyParser  = require("body-parser"),
    mongoose    = require("mongoose"),
    cron = require("node-cron");


mongoose.connect("mongodb://localhost/asset-management-v1");
// mongoose.connect("mongodb://localhost/yelp_camp_v10");
app.use(bodyParser.urlencoded({extended: true}));
app.set("view engine", "ejs");
app.use(express.static(__dirname + "/public"));


app.use(function(req, res, next){
   res.locals.currentUser = req.user;
   next();
});

var task = cron.schedule('10 * * * *', function() {
  console.log('immediately started');
}, false);

task.start();

我的最终目标是检查mongodb collection,然后查看每个objectID,如果objectID 时间戳超过10 天,则使用nodemailer 发送电子邮件。如果您发现除了导致console.log 失败的原因之外的任何其他内容,请指出,这样我就不必再回来打扰您了。

【问题讨论】:

    标签: node.js express cron node-cron


    【解决方案1】:

    10 * * * * - 表示“每 10 分钟”。你需要10 * * * * * 每 10 秒运行一次。

    您可以通过true 立即启动它:

    var task = cron.schedule('10 * * * *', function() {
      console.log('immediately started');
    }, true);
    

    【讨论】:

      猜你喜欢
      • 2020-08-29
      • 1970-01-01
      • 2021-06-18
      • 2015-07-29
      • 2014-08-07
      • 1970-01-01
      • 1970-01-01
      • 2015-08-31
      • 2022-10-08
      相关资源
      最近更新 更多