Application
Queues
Background jobs with delays, retries, priorities, repeatable and cron jobs, a rate limiter and Redis for sharing.
queue is @nestjs/bullmq: jobs are added to a named queue and processed by workers, here one virtual thread each.
@Injectable
@Processor("emails", concurrency = 4, limitMax = 50, limitPerMillis = 1000) // 4 at a time, at most 50 starts per second
class EmailProcessor(private val mailer: MailerService) {
@Process("welcome") fun welcome(job: WelcomeJob) { mailer.sendView(listOf(job.to), "Welcome", "welcome", mapOf("name" to job.name)) }
@Process("*") fun other(data: Map<String, Any?>, record: JobRecord) { /* every name not claimed above */ }
}
@Injectable
class Signup(private val queues: QueueService) {
fun done(user: User) { queues.queue("emails").add("welcome", WelcomeJob(user.email, user.name), JobOptions(attempts = 3, backoff = Duration.ofSeconds(5), exponentialBackoff = true)) }
}Kest.run<AppModule> { import(QueueModule.forRoot { store = RedisQueueStore("localhost", 6379) }) } // memory by defaultJob options
| Option | Meaning |
|---|---|
delay | wait before the job may run |
attempts, backoff, exponentialBackoff | retries; the wait doubles each time when exponential |
priority | higher runs first among waiting jobs (-100 to 100 are ordered in Redis; the same in memory) |
jobId | your own id |
repeat | run again and again, below |
A @Process method's first parameter is the job data (decoded from JSON); a parameter typed JobRecord gets the job itself. queue.counts() gives waiting, delayed, active, completed and failed; failedJobs() lists failures with their message; queue.on("completed") { job, result -> } listens.
Repeatable jobs
val key = queue.add("cleanup", null, JobOptions(repeat = Repeat.every(Duration.ofMinutes(10))))
val daily = queue.add("report", null, JobOptions(repeat = Repeat.cron("0 0 9 * * MON-FRI", zone = "Europe/London")))
queue.removeRepeatable(key)The next run is scheduled as soon as one starts, so a slow run does not push the schedule back. The id of each run is fixed by its time, so several instances never run the same occurrence twice, and adding the same repeatable job at every start-up (every instance does) schedules it once. add returns the key to give to removeRepeatable. Cron expressions have 5 or 6 fields.
Rate limiter
limitMax and limitPerMillis on @Processor cap how many jobs start per window across all workers of the queue, and across every instance when the store is shared (Redis keeps the counter). Workers wait for a slot instead of failing.
Stores
| Store | Notes |
|---|---|
InMemoryQueueStore (default) | this process only; lost on restart |
RedisQueueStore (queue-redis) | survives restarts and is shared: waiting jobs in a sorted set ordered by priority then arrival, delayed jobs in another, atomic claims, a shared rate limit and repeat schedule |
Delivery is at least once: a crash during a job can run it again. Make jobs idempotent.
Not built
Job dependencies and flows, pausing a queue, progress reporting, a dashboard, and moving a failed job back with one call.