Job Management
Monque serves a comprehensive set of APIs for managing jobs throughout their lifecycle.
Quick Reference
Section titled “Quick Reference”| Method | Description |
|---|---|
cancelJob() | Cancel a single job |
retryJob() | Retry a job |
rescheduleJob() | Reschedule a job |
deleteJob() | Delete a job |
cancelJobs() | Bulk cancel |
retryJobs() | Bulk retry |
deleteJobs() | Bulk delete |
getJobsWithCursor() | Paginated list |
getQueueStats() | Queue statistics |
Single Job Operations
Section titled “Single Job Operations”Manage individual jobs using their unique ID.
Cancel a Job
Section titled “Cancel a Job”Use cancelJob() to prevent a pending or scheduled job from running.
- Effect: Status becomes
cancelled. - Event: Emits
job:cancelled. - Constraint: Cannot cancel
processing,completed, orfailedjobs (unless they return to pending).
Retry a Job
Section titled “Retry a Job”Use retryJob() to retry a failed or cancelled job immediately.
- Effect: Status becomes
pending,nextRunAtset to now, failure count reset. - Event: Emits
job:retried.
Reschedule a Job
Section titled “Reschedule a Job”Use rescheduleJob() to change the execution time of a pending job.
Delete a Job
Section titled “Delete a Job”Use deleteJob() to permanently remove a job from the queue.
- Effect: Document is removed from MongoDB.
- Event: Emits
job:deleted.
Bulk Operations
Section titled “Bulk Operations”Perform operations on multiple jobs matching a filter.
Bulk Cancel
Section titled “Bulk Cancel”Use cancelJobs() to cancel all pending jobs matching a criteria.
Bulk Retry
Section titled “Bulk Retry”Use retryJobs() to retry all failed jobs.
Bulk Delete
Section titled “Bulk Delete”Use deleteJobs() to clean up old jobs.
- Effect: Removing matching documents from MongoDB.
- Event: Emits
jobs:deletedwith count.
Pagination
Section titled “Pagination”Efficiently iterate through large sets of jobs using getJobsWithCursor().
Supports filtering:
Queue Statistics
Section titled “Queue Statistics”Get real-time insights into your queue health using getQueueStats().
You can also scope statistics to a specific job name:
Stats Caching
Section titled “Stats Caching”getQueueStats() runs a MongoDB aggregation pipeline. To avoid re-running that pipeline on every call (e.g. in a metrics endpoint), Monque caches results with a TTL+LRU strategy.
By default the TTL is 5 seconds. Configure it via statsCacheTtlMs:
Behaviour details:
- Each unique filter (global vs. name-scoped) has its own cache entry.
- The cache is size-capped at 100 entries. When full, it uses an LRU eviction policy—the least-recently-used filter entry is evicted to make room for new entries.
- Each per-filter entry adheres to the configured TTL (default 5s).
- Cached results are returned without hitting MongoDB until the TTL expires.
- The cache is cleared entirely when
stop()is called. - Set
statsCacheTtlMs: 0to disable caching entirely and always run the aggregation live.