简体   繁体   English

使用议程进行作业计划未运行

[英]Using Agenda for job schedule not running

I am using Agenda to schedule my job at a certain time.我正在使用 Agenda 在某个时间安排我的工作。 The job will contain a certain array of emails that will be sent automated emails in the interval of 10 seconds, but the issue is the job is not executing as expected.该作业将包含一定的电子邮件数组,这些电子邮件将在 10 秒的间隔内自动发送电子邮件,但问题是该作业未按预期执行。

The scheduler runs immediately regardless of the scheduled time set as 'after 2 minutes'.无论计划时间设置为“2 分钟后”,调度程序都会立即运行。 On starting the server, the job runs immediately and continue to do so.在启动服务器时,作业会立即运行并继续运行。 Along with that, it prints out all the available entries in the database.除此之外,它还会打印出数据库中所有可用的条目。

This is how I am scheduling the job:这就是我安排工作的方式:

let Agenda = require("agenda");

let agenda = new Agenda();
agenda.database(`localhost:27017/${process.env.DB_NAME}`, "schedulers", {
  useNewUrlParser: true,
  useUnifiedTopology: true,
});

var i = 0;
agenda.define("campaignScheduler", function (job) {
  i++;
  console.log(
    i +
      " Run at " +
      new Date().getHours() +
      ":" +
      new Date().getMinutes() +
      ":" +
      new Date().getSeconds()
  );
});
const testData = {
  name: "John Snow",
  userId: "j0dn.j213b455b.bchds0",
};

agenda.start();
agenda.on("ready", () => {
  agenda
    .create("campaignScheduler", testData)
    .unique({ "data.unique_id": testData.userId })
    .repeatEvery("*/10 * * * * *", {
      timezone: "America/Los_Angeles",
    })
    .schedule("in 2 minutes")
    .save();

  agenda.on("complete", (job) => {
    console.log(`finished job`, job.attrs);
  });

  agenda.on("job success", (job) => {
    console.log(`Successfull`);
  });

  agenda.on("job fail", (err, job) => {
    console.log(`Job failed with error: ${err.message}`);
  });
});

let graceful = () => {
  agenda.stop(() => process.exit(0));
};

process.on("SIGTERM", graceful);
process.on("SIGINT", graceful);

Results on the console:控制台上的结果:

在此处输入图像描述

Results on db: db 上的结果:

在此处输入图像描述

The results saving to the database are quite confusing.保存到数据库的结果相当混乱。 Please help around to resolve this issue.请帮助解决此问题。

The scheduler runs immediately regardless of the scheduled time set as 'after 2 minutes'.无论计划时间设置为“2 分钟后”,调度程序都会立即运行。

It looks like agenda is doing what it's supposed to, as per docs :根据文档,议程似乎正在做它应该做的事情:

repeatEvery(interval, [options]) repeatEvery(间隔,[选项])

Specifies an interval on which the job should repeat.指定作业应重复的时间间隔。 The job runs at the time of defining as well in configured intervals, that is "run now and in intervals" .作业在定义时也以配置的时间间隔运行,即"run now and in interval"

Additionally you're trying to schedule a run every X seconds which isn't possible with a cron expression - as explained here .此外,您正在尝试每X秒安排一次运行,这对于 cron 表达式是不可能的 - 如此处所述 If you want to spread the emails in time may I suggest queueing them elsewhere in your codebase, the same way you would rate-limit HTTP requests.如果您想及时分发电子邮件,我建议您将它们排在代码库的其他位置,就像您对 HTTP 请求进行速率限制一样。

Along with that, it prints out all the available entries in the database.除此之外,它还会打印出数据库中所有可用的条目。

You are printing job.attrs , and that's what it looks like, since what you get with the event is the whole job instance.您正在打印job.attrs ,这就是它的样子,因为您从事件中得到的是整个job实例。 If you want to print just the supplied data then change your code to:如果您只想打印提供的数据,请将代码更改为:

agenda.on("complete", (job) => {
  console.log(`finished job`, job.attrs.data); // <- add .data
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM