我做类似事情的方式是,当前一个作业的主要功能完成时,我触发下一个作业,这样我可以将数据从一个作业提供给另一个作业并按顺序运行它们。
所以我job3
在定义中触发并在定义中job2
触发:job2
job1
// DEFINE JOBS
agenda.define('job1', (job) => {
return myMainJob1Function(job.attrs.data)
.then((data) => {
for (const element of data) {
agenda.now('job2', { // runs many instances of 'job2' job with distinct data
// use output data from the "parent" job
arg1: element.arg1,
arg2: element.arg2,
arg3: element.arg3,
arg4: job.attrs.data.someArg, // this will propagate an argument we set for the "parent" job
});
}
});
});
agenda.define('job2', (job) => {
return myMainJob2Function(job.attrs.data) // data supplied from 'job1'
.then((data) => {
for (const element of data) {
agenda.now('job3', { // runs many instances of 'job3' job with distinct data
// use output data from the "parent" job
arg1: element.arg1,
arg2: element.arg2,
arg3: element.arg3,
arg4: job.attrs.data.someArg, // this will propagate an argument we set for the "parent" job
});
}
});
});
agenda.define('job3', (job) => {
return myMainJob3Function(job.attrs.data) // data supplied from 'job2'
.then((data) => {
// save data to database or do something else
});
});
// TRIGGER 'job1', WHICH TRIGGERS CHILD JOBS
agenda.now('job1', { someArg: 5 }); // we're providing some initial argument, that gets passed onto child jobs
const schedule = '0 0/5 0 ? * * *' // cron expression for every 5 minutes
agenda.every(schedule, 'job1', { someArg: 5 });
想到的另一件事是使用触发器 for complete
or success
,job1
但是当您使用不同的输入数据运行许多相同的作业时,您不能(我还没有找到方法)监听与特定作业实例相关的那些事件你要的那个。
话虽如此,您可以这样做:
agenda.on('success:job1', (job) => {
agenda.now('job2', { someArg: job.attrs.data.someArg });
});