Queue
queue module built with appolo-queue
#
Installation#
Optionskey | Description | Type | Default |
---|---|---|---|
id | queue injection id | string | queue |
config | queue options | object | {} |
#
Queue optionskey | 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
#
UsageIn task handler the name of the handler must be the same as queue.create
#
Create Job#
createcreate(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
#
delaydelay(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
#
scheduleschedule(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 jobsEach job has it's own handler The handler will be called with the job instance and return promise with the job result
#
handlehandle(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 jobsyou can define one handler to handle multi jobs
#
Job#
lockTimelockTime(lockTime: number):Job
#
change job lock time default: 60000
#
repeatrepeat(value: number):Job
#
set the max number of time job will run the default fro schedule in unlimited and for delayed is 1
#
retryretry (value: number):Job
#
set the number of retries on job fail default :10 when the number is reached will reschedule the job
#
backoffbackoff(value: number) :Job
#
set interval in milisec for each retry backoff default:1000
#
handlerhandler(value: string | Function) :Job
#
set job handler id or function
#
execexec() :Promise<Job>
#
save the job to redis if the schedule changed the job will reschedule
#
locklock(interval:number) :Promise<Job>
#
lock job for given interval this method is called automatically when the handler is called
#
runrun(waitForResults:boolean) :Promise<Job> | Promise<any>
#
save the job to redis and run it immediately if waitForResults then promise returned with job result
#
cancelcancel() :Promise<void>
#
cancel the job and delete from redis
#
idget id():string
#
return job id
#
paramsget params():any
#
return job params
#
nextRunget nextRun(): number
#
return job next run unix milisec
#
intervalget interval(): number
#
return job next run interval milisec
#
optionsget options()
#
return job options
#
Job EventsJob 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.JobFail
job run is failed with errorEvents.JobComplete
job run is success or failed and the job is returned to the queue
#
onon(eventName,callback, [scope]):Job
#
register event listener
#
onceonce(eventName,callback, [scope]):Job
#
register event listener, will be removed after one call
#
unun(eventName,callback, [scope]):Job
#
remove event listener
#
Queue#
initializeinitialize():Promise<void>
#
initialize the queue and start pulling interval a promise returned when every thing is ready.
#
startstart()
#
start pulling jobs from the queue
#
stopstop()
#
stop pulling jobs from the queue
#
runrun(jobId: string,waitForResult:boolean): Promise<this | any>
#
run job by id return the instance or job result when waitForResult true
#
getJobgetJob(id: string): Promise<Job>
#
get job instance by id
#
getAllJobsgetAllJobs(): Promise<Job[]>
#
get all jobs in the queue
#
hasJobhasJob(id: string): Promise<boolean>
#
return true if job id exist in the queue
#
purgepurge()
#
delete all jobs in the queue
#
resetreset()
#
stop job pulling and purge the queue
#
Queue EventsJob 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.JobFail
job - 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
#
onon(eventName,callback, [scope]):Queue
#
register event listener
#
onceonce(eventName,callback, [scope]):Queue
#
register event listener, will be removed after one call
#
unun(eventName,callback, [scope]):Queue
#
remove event listener