job-runner
v0.1.6
Published
Run several runner async but sync at all
Downloads
4
Readme
JOB RUNNER
For What
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.
How to use
Create runner
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())
})
}
})
Just run
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)
})
Options
source
this is the data source of you jobs, must be an array.
step
this is the number of you parallel jobs
runner
this is you job, for how to run
group
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.
update options
you can update options of exist runners with the setOptions function
runner.setOptions({
step: 5
})