A queueing system to schedule jobs and have multiple rate-limited consumers.
API
Setting up a Job Queue
jobQueue = consumerslimitperiod
Parameter | Type | Description |
---|---|---|
consumers |
Function | A function that will accept a job as the only parameter. |
limit |
Integer | The maximum number of jobs the consumer should process in period milliseconds. |
period |
Integer | The number of milliseconds limit applies to. |
Adding Consumers
jobQueueaddConsumers consumerslimitperiod
Function signature for addConsumers
is same as the above constructor.
Enqueuing a Job
jobQueueenqueue job
Parameter | Type | Description |
---|---|---|
job |
Anything | The job can either be any type (Object , Function , Number , ...). Its type depends on what the consumer takes as its argument. |
Getting Count of Pending Jobs
pendingJobs = jobQueuependingJobs
Example Usage
JobQueue = require "job-queue" = consolelog "Consumer processing job " jobprocess consumerId # Create a process queue with 5 consumers where each supports up to 5 requests per second processQueue = 1..5mapmakeConsumer51000 # Add another 5 consumers to the process queue where each supports up to 80 jobs per minute processQueueaddConsumers 6..10mapmakeConsumer8060 * 1000 # Adding 10k jobs to the process queue for jobId in 1..10000then do processQueueenqueue id: jobId : # The rate limited section (e.g. some API call) consolelog "Job processed by consumer "