Jobs
Jobs are the fundamental unit of work in Monque. This guide explains how jobs work, their lifecycle, and how to work with them effectively.
Quick Reference
Section titled “Quick Reference”| Method / Type | Description |
|---|---|
Job | Job document interface |
enqueue() | Create a job with optional scheduling |
now() | Create a job for immediate execution |
schedule() | Create a recurring cron-based job |
getJob() | Retrieve a single job by ID |
getJobs() | Query jobs with filters |
EnqueueOptions | Options for enqueue() |
JobStatus | Status values object |
What is a Job?
Section titled “What is a Job?”A job represents a unit of work to be processed asynchronously. Each job has:
- Name: Identifies which worker should process it
- Data: The payload containing information needed to complete the work
- Status: Current state in the lifecycle (
pending,processing,completed,failed). Can be referenced via theJobStatusObject from@monque/core. - Scheduling: When the job should run (
nextRunAt)
Job Interface
Section titled “Job Interface”For the full interface definition, see the Job API Reference.
Job Lifecycle
Section titled “Job Lifecycle”stateDiagram-v2
[*] --> pending: enqueue()
pending --> processing: worker claims
pending --> cancelled: cancelJob()
processing --> completed: success
processing --> pending: failure (retries left)
processing --> failed: max retries exceeded
completed --> pending: recurring job reschedules
cancelled --> pending: retryJob()
Status Definitions
Section titled “Status Definitions”| Status | Description |
|---|---|
pending | Job is waiting to be picked up by a worker |
processing | A worker has claimed and is executing the job |
completed | Job finished successfully |
failed | Job permanently failed after exhausting all retries |
cancelled | Job was manually cancelled via cancelJob() |
Creating Jobs
Section titled “Creating Jobs”Immediate Execution
Section titled “Immediate Execution”Use now() for jobs that should run immediately. This is sugar for enqueue() without scheduling options:
Scheduled Execution
Section titled “Scheduled Execution”Use enqueue() with runAt for delayed execution:
Recurring Jobs
Section titled “Recurring Jobs”Use schedule() for cron-based recurring jobs:
Preventing Duplicates
Section titled “Preventing Duplicates”Use uniqueKey to prevent duplicate jobs:
Querying Jobs
Section titled “Querying Jobs”Get Jobs by Filter
Section titled “Get Jobs by Filter”Get Single Job by ID
Section titled “Get Single Job by ID”Job Data Best Practices
Section titled “Job Data Best Practices”Keep Payloads Small
Section titled “Keep Payloads Small”Job data should be reference data, not full documents:
Use Type-Safe Payloads
Section titled “Use Type-Safe Payloads”Define interfaces for your job data:
Idempotent Operations
Section titled “Idempotent Operations”Design workers to handle duplicate execution safely:
Job Retention
Section titled “Job Retention”Monque provides built-in job retention to automatically clean up completed and failed jobs. This prevents your database from growing indefinitely.
You can configure retention when initializing Monque:
Indexes
Section titled “Indexes”Monque creates MongoDB indexes during initialize() to keep polling, claiming, deduplication, and recovery queries fast.
The indexes Monque creates are:
{ status: 1, nextRunAt: 1 }for efficient polling (ready-to-run jobs){ name: 1, uniqueKey: 1 }as a partial unique index for deduplication (only whenuniqueKeyexists and status ispending/processing){ name: 1, status: 1 }for querying by job type and status{ claimedBy: 1, status: 1 }for ownership queries and heartbeat updates{ lastHeartbeat: 1, status: 1 }for heartbeat-based monitoring queries{ status: 1, nextRunAt: 1, claimedBy: 1 }for the atomic claim query{ status: 1, lockedAt: 1, lastHeartbeat: 1 }to support stale recovery queries
Deleting Jobs
Section titled “Deleting Jobs”You can use the deletion APIs for manual cleanup:
deleteJob(jobId): Permanently delete a single job.deleteJobs(filter): Delete multiple jobs matching criteria (name, status, age).
Advanced Custom Cleanup
Section titled “Advanced Custom Cleanup”If you need more complex retention logic (e.g., keep important sales reports longer), implement a custom scheduled job that uses the MongoDB driver directly:
Next Steps
Section titled “Next Steps”- Workers - Learn how to process jobs
- Scheduling - Deep dive into cron expressions
- Retry & Backoff - Configure retry behavior
API Reference
Section titled “API Reference”Job- Full job document interfacePersistedJob- Type returned from enqueue operationsMonque.enqueue()- Enqueue a jobMonque.now()- Immediate execution sugarMonque.schedule()- Recurring job schedulingMonque.getJob()- Get job by IDMonque.getJobs()- Query jobsEnqueueOptions- Enqueue optionsGetJobsFilter- Job query filterJobStatus- Status constants