0

我正在使用由 MongoDB 支持的带有 Node 的议程.js 来处理批处理作业。我正在使用的当前语法遇到的一个问题是安排重复事件,但没有立即执行。我知道“skipImmediate:true”标志,但我不清楚在我当前的配置中我需要在哪里应用它,我在哪里使用 IIFE:

  agenda.define('test job', {
    priority: 'high',
    concurrency: 10
  }, async job => {
    const {
      to
    } = job.attrs.data;
    job.repeatEvery('0 11 * * 1-5', {
      skipImmediate: true
    });
    await send(to);
  });

  function send(to) {
    const today = new Date();
    const target = to;
    const time = today.getHours() + ":" + today.getMinutes() + ":" + today.getSeconds();
    console.log(`Emailing to ${target} regarding second job, at ${time}.`);
  }

  (async function () {
    await agenda.start();
    agenda.create('test job', {
      to: 'someone@email.com',
      from: 'sample@email.com'
    }).save();
  })();
};

如你所见,我有...

{ skipImmediate: true }

...在repeatEvery块中,但它似乎不起作用。如何使用当前配置防止立即执行?

4

2 回答 2

2

我认为您使任务复杂化,这可能适用于您的情况

agenda.define('test job', {
priority: 'high',
concurrency: 10
}, async job => {
const {to} = job.attrs.data;
const today = new Date();
const target = to;
const time = today.getHours() + ":" + today.getMinutes() + ":" + today.getSeconds();
console.log(`Emailing to ${target} regarding second job, at ${time}.`);
});

这是为了打电话

(async function () {
  const returned= agenda.create('test job', {to:'someone@email.com',from:'sample@email.com'})
  await agenda.start();
  await returned.repeatEvery('0 11 * * 1-5',{skipImmediate: true}).save()
})();

这样做define将定义您的议程应该做什么,然后是您create的议程parameters并开始它。它返回Job,您可以使用重复它repeatEvery

于 2019-10-10T18:45:35.803 回答
0

我认为您可以使用猫鼬交易来避免立即执行。

const mongoose = require('mongoose');

Start a session

    const session = await mongoose.startSession();
    session.startTransaction();

    try{
      agenda.define('test job', {
        priority: 'high',
        concurrency: 10
      }, async job => {
        const {
          to
        } = job.attrs.data;
        job.repeatEvery('0 11 * * 1-5', {
          skipImmediate: true
        });
        await send(to);
      }).session(session);

     function send(to) {
        const today = new Date();
        const target = to;
        const time = today.getHours() + ":" + today.getMinutes() + ":" + today.getSeconds();
        console.log(`Emailing to ${target} regarding second job, at ${time}.`);
      }
     async () => {
        await agenda.start();
        agenda.create('test job', {
          to: 'someone@email.com',
          from: 'sample@email.com'
        }).session(session).save();
        await session.commitTransaction();
      };
    } catch (err) {
            await session.abortTransaction();
            throw err;
        } finally {
            session.endSession();
        }
    }

尝试参考猫鼬交易文档以获取更多详细信息

于 2019-10-10T18:11:19.577 回答