Queue
queue module built with appolo-queue
Installation#
Options#
| key | Description | Type | Default |
|---|---|---|---|
id | queue injection id | string | queue |
config | queue options | object | {} |
Queue options#
| key | Description | Type | Default |
|---|---|---|---|
redis | redis connection string | string | `` |
queueName | queue prefix in redis | string | appolo-queue |
checkInterval | queue check time in ms | number | 1000 |
maxConcurrency | max number of jobs to process in parallel per server | number | 1 |
lockTime | interval in ms of how long the job stays locked when pulled from the queue | number | 60000 |
in config/modules/all.ts
Usage#
In task handler the name of the handler must be the same as queue.create
Create Job#
create#
create(jobId, [params]):Job#
creates new job instance params optional object pasted to the handler
in the end call exec to save the job the task will be run Date.now()
each job must have uniq id
delay#
delay(time):Job#
create delayed job where time one of the following the job will run only once
- interval in milisec
- date object
- string in date syntax
schedule#
schedule(time):Job#
create scheduled job where time one of the following the job will run every interval
- interval in milisec
- cron job syntax
- date object
- string in date syntax
Handle jobs#
Each job has it's own handler The handler will be called with the job instance and return promise with the job result
handle#
handle(jobId,handler,[options]):Queue#
adds handler to queue by job id
options:
lockTime: interval in milisec lock the job while the handler is running
handler with multiple jobs#
you can define one handler to handle multi jobs
Job#
lockTime#
lockTime(lockTime: number):Job#
change job lock time default: 60000
repeat#
repeat(value: number):Job#
set the max number of time job will run the default fro schedule in unlimited and for delayed is 1
retry#
retry (value: number):Job#
set the number of retries on job fail default :10 when the number is reached will reschedule the job
backoff#
backoff(value: number) :Job#
set interval in milisec for each retry backoff default:1000
handler#
handler(value: string | Function) :Job#
set job handler id or function
exec#
exec() :Promise<Job>#
save the job to redis if the schedule changed the job will reschedule
lock#
lock(interval:number) :Promise<Job>#
lock job for given interval this method is called automatically when the handler is called
run#
run(waitForResults:boolean) :Promise<Job> | Promise<any>#
save the job to redis and run it immediately if waitForResults then promise returned with job result
cancel#
cancel() :Promise<void>#
cancel the job and delete from redis
id#
get id():string#
return job id
params#
get params():any#
return job params
nextRun#
get nextRun(): number#
return job next run unix milisec
interval#
get interval(): number#
return job next run interval milisec
options#
get options()#
return job options
Job Events#
Job events are fired on the Job instances via Redis pubsub all callbacks called with the job instance
Events.JobStartthe job is pulled from the queue and the handler is calledEvents.JobSuccessjob run is completed successfully result is added the callback argsEvents.JobFailjob run is failed with errorEvents.JobCompletejob run is success or failed and the job is returned to the queue
on#
on(eventName,callback, [scope]):Job#
register event listener
once#
once(eventName,callback, [scope]):Job#
register event listener, will be removed after one call
un#
un(eventName,callback, [scope]):Job#
remove event listener
Queue#
initialize#
initialize():Promise<void>#
initialize the queue and start pulling interval a promise returned when every thing is ready.
start#
start()#
start pulling jobs from the queue
stop#
stop()#
stop pulling jobs from the queue
run#
run(jobId: string,waitForResult:boolean): Promise<this | any>#
run job by id return the instance or job result when waitForResult true
getJob#
getJob(id: string): Promise<Job>#
get job instance by id
getAllJobs#
getAllJobs(): Promise<Job[]>#
get all jobs in the queue
hasJob#
hasJob(id: string): Promise<boolean>#
return true if job id exist in the queue
purge#
purge()#
delete all jobs in the queue
reset#
reset()#
stop job pulling and purge the queue
Queue Events#
Job events are fired on the Job instances via Redis pubsub all callbacks called with the job instance
Events.JobStart- the job is pulled from the queue and the handler is calledEvents.JobSuccess- job run is completed successfully result is added the callback argsEvents.JobFailjob - run is failed with errorEvents.JobComplete- job run is success or failed and the job is returned to the queueEvents.Ready- the queue finish initialize and start pull intervalEvents.Error- some error occurred during the job process
on#
on(eventName,callback, [scope]):Queue#
register event listener
once#
once(eventName,callback, [scope]):Queue#
register event listener, will be removed after one call
un#
un(eventName,callback, [scope]):Queue#
remove event listener