将作业保存到节点中的议程时防止立即执行
Preventing Immediate Execution When Saving Job to Agenda in Node
我正在使用 agenda.js 和 Node,在 MongoDB 的支持下处理批处理作业。对于我正在使用的当前语法,我 运行 遇到的一个问题是安排重复事件,但没有立即执行。我知道 "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
块中,但它似乎不起作用。如何防止使用当前配置立即执行?
我认为你可以使用 mongoose 事务来避免立即执行。
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();
}
}
尝试参考 mongoose transaction documentation 了解更多详情
我认为你使任务复杂化,这可能适用于你的情况
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
并启动它。它 returns Job
你可以使用 repeatEvery
重复它
我正在使用 agenda.js 和 Node,在 MongoDB 的支持下处理批处理作业。对于我正在使用的当前语法,我 运行 遇到的一个问题是安排重复事件,但没有立即执行。我知道 "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
块中,但它似乎不起作用。如何防止使用当前配置立即执行?
我认为你可以使用 mongoose 事务来避免立即执行。
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();
}
}
尝试参考 mongoose transaction documentation 了解更多详情
我认为你使任务复杂化,这可能适用于你的情况
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
并启动它。它 returns Job
你可以使用 repeatEvery