首页 > 其他 > 详细

node定时任务

时间:2019-11-18 19:36:52      阅读:79      评论:0      收藏:0      [点我收藏+]

1.新建一个schedule文件

2.如下代码举例

const Subscription = require(egg).Subscription;
const report = require(../utils/report);

class SendMail extends Subscription {
  // 通过 schedule 属性来设置定时任务的执行间隔等配置
  static get schedule() {
    return {
      // interval: ‘1h‘, // 1 小时间隔
      type: worker, // 每台机器上只有一个 worker 会执行这个定时任务
      // immediate: true, // app ready 后立即跑一次(上线注释掉)
      cron: 0 30 10 ? * MON, // 每周一早上10点
      // cron: ‘0 */1 * * * ?‘, // 每1分钟一次
      disable: require(os).hostname() !== lbs-dev-monitor011015250024.center.na62,
    };
  }

  // subscribe 是真正定时任务执行时被运行的函数
  *subscribe() {
    const { logger, diamond } = this.ctx;
    logger.info(monitor check schedule);
    const monitorData = yield diamond.getConfig(monitor, lbs);
    logger.info(get cc from diamond);

    const feedbackMailCC = JSON.parse(monitorData).feedbackMailCC;
    yield report(new Date().getTime(), 1,feedbackMailCC, logger);
  }
}

module.exports = SendMail;

技术分享图片

 

node定时任务

原文:https://www.cnblogs.com/kaiqinzhang/p/11884203.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!