jobman
Controllable and resonable job queue manager
Install
npm install --save jobman
Usage
The example code:
const jobman = const man = forlet i=0;i<10;i++ man // job.prop = i man // the job timeout is 500ms { // dummy task function // async happen at 1 second}
The result:
all jobs will start
timeout: { id: 'insert to first!', timeout: 500 }
result: 0 ok
result: 1 ok
result: 2 ok
result: 3 bad
result: 4 ok
result: 6 ok
result: 7 ok
result: 8 ok
result: 9 ok
all end true
API
jobman(config) -> manObject
-
config
- config.max int
max number of concurrence jobs.
- config.timeout int
ms to wait for --A job--, trigger jobTimeout event after the time
- config.interval int
ms to pass into setInterval check internally
- config.jobTimeout fn(job, man)->boolean
callback function after --A job-- timeout, return false will book another timeout
- config.jobStart fn(job, man)->boolean
callback function before --A job-- will start, return false will cancel this job
- config.jobRun fn(job, man)->void
callback function after --A job-- did start, job.state become run
- config.jobEnd fn(job, man)->void
callback function after --A job-- callback invoked, with man.lastError set to result error
- config.allStart fn(info, man)->void
callback function before --ALL job-- start run, useful for init, info is user passed with man.start(info)
- config.allEnd fn(info, man)->void
callback function when --ALL job-- finished run, info is user passed with man.end(info)
- config.max int
-
manObject
- man.add([position:int], jobFn:cb=>{}, jobProp:any)
jobFn is with callback for one job, cb(err) have to be called for each job. jobProp will become job.prop
- man.start([info])
function to start current jobman, use man.stop to stop it, trigger man.allStart with first arg set to info
- man.stop()
function to stop current jobman, use man.start to start again
- man.end([info], [cancelPending])
trigger man.allEnd with first arg set to info, cancelPending will cancel pending jobs
- man.clear(cancelRunningJobs)
Remove all pending jobs, cancel running jobs if cancelRunningJobs is true
- man.config object
the config object passed into jobman
- man.jobs array
the jobs array internally, query for it for state, length etc.
- man.isRunning boolean
prop to get if the job monitor is is running (not stopped)
- man.isDone boolean
prop to get if all job ended
- man.pending [jobObject, ...]
prop to get pending jobs in queue
- man.running [jobObject, ...]
prop to get running jobs in process
- man.slot int
prop to get current available job runner slot
- man.startCount int
jobman start counter, from 0, after first start() will be 1, after next start() plus 1
- man.add([position:int], jobFn:cb=>{}, jobProp:any)
-
jobObject
- job.fn function
the jobFn function passed into man.add()
- job.error any
when job callback called with error, set to it with the error
- job.prop any
the prop to passed into man.add()
- job.prop.timeout int
set timeout for individual job
- job.state any
internal state for each job, value is run/done/error/timeout/cancel
- job.fn function