When you have to many jobs to run, but there is a limit of async jobs, you can try this, this runner can easily make jobs run of the max async number.
there are two method to create a runner, use a constractor:
let Runner = jobRunner.default;
let runner = new Runner({
source: [1,2,3],
step: 2,
runner: (sourceData, index) => {
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve(sourceData)
}, Math.random())
})
}
})
or use the create function:
let runner = jobRunner.create({
source: [1,2,3],
step: 2,
runner: (sourceData, index) => {
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve(sourceData)
}, Math.random())
})
}
})
When you created a runner, you can just call the run of the runner to start you jobs.
console.log('jobs start to run')
runner.run().then((res) => {
console.log('now jobs run finished')
console.log('the result of jobs is: ', res)
})
this is the data source of you jobs, must be an array.
this is the number of you parallel jobs
this is you job, for how to run
if the group is true, you jobs will run with group one by one. Otherwise, if one of the running jobs finished, there will fill another, until there was no left.
you can update options of exist runners with the setOptions function
runner.setOptions({
step: 5
})