Scheduling
Monque selects due Jobs by priority and supports recurring scheduling using standard 5-field cron expressions.
Job Priority
Section titled “Job Priority”Pass priority when creating a Job:
Higher numbers take precedence among due pending Jobs with the same Job Name. The default
priority is 0; negative numbers put work below that default. Values must be signed JavaScript
safe integers. Monque rejects non-number, fractional, non-finite, and unsafe values before
writing a Job. The now() shortcut schedules immediately and accepts { priority, session }
options. Set priority on each enqueueMany() item or in schedule() options for batch
and recurring work.
Equal priorities are ordered by nextRunAt ascending, then _id ascending. A future Job
remains ineligible until nextRunAt, regardless of priority. Deadline discovery and future
wakeups still follow scheduled time, so future urgent Jobs cannot hide due routine work.
Priority compares Jobs within one Job Name. Monque still rotates between Job Names
to share instance capacity.
An enqueue using an active uniqueKey returns the existing Job unchanged, including its
priority, payload, and scheduled time. Public Job reads, metadata summaries, lifecycle
events, and the Dashboard expose effective numeric priority, including default 0.
Existing Jobs and Upgrades
Section titled “Existing Jobs and Upgrades”During initialize(), Monque automatically writes priority: 0 only to Jobs missing the
field. Jobs created without a priority then compete at priority zero, including
against negative priorities. Repeated initialization preserves existing priorities, timestamps,
lifecycle state, and ownership metadata. It runs even with skipIndexCreation: true.
You do not need to rewrite existing Jobs before upgrading.
Upgrade every claiming Scheduler Instance and every producer sharing the collection before relying on uniform priority ordering. Older schedulers ignore priority when claiming work, and older producers can create Jobs without a priority after initialization. See the production index guidance for the separate claim and deadline indexes.
Quick Reference
Section titled “Quick Reference”| Method / Type | Description |
|---|---|
schedule() | Create a recurring cron-based job |
ScheduleOptions | Options for schedule() |
Job.repeatInterval | Cron expression for recurring jobs |
InvalidCronError | Thrown for invalid cron expressions |
Basic Scheduling
Section titled “Basic Scheduling”Use schedule() to create recurring jobs:
Method Signature
Section titled “Method Signature”Schedule Options
Section titled “Schedule Options”Cron Expression Format
Section titled “Cron Expression Format”Monque uses the standard 5-field cron format:
Special Characters
Section titled “Special Characters”| Character | Description | Example |
|---|---|---|
* | Any value | * * * * * (every minute) |
, | List separator | 0,30 * * * * (0 and 30 minutes) |
- | Range | 0-15 * * * * (minutes 0-15) |
/ | Step values | */5 * * * * (every 5 minutes) |
Predefined Aliases
Section titled “Predefined Aliases”Monque supports cron aliases provided by the cron-parser library for common schedules:
| Alias | Description | Equivalent |
|---|---|---|
@yearly (or @annually) | Run once a year | 0 0 1 1 * |
@monthly | Run once a month | 0 0 1 * * |
@weekly | Run once a week | 0 0 * * 0 |
@daily (or @midnight) | Run once a day | 0 0 * * * |
@hourly | Run once an hour | 0 * * * * |
Common Examples
Section titled “Common Examples”Time-Based Schedules
Section titled “Time-Based Schedules”Daily Schedules
Section titled “Daily Schedules”Weekly Schedules
Section titled “Weekly Schedules”Monthly Schedules
Section titled “Monthly Schedules”How Recurring Jobs Work
Section titled “How Recurring Jobs Work”Initial Scheduling
Section titled “Initial Scheduling”When you call schedule():
- Cron expression is validated
- Next run time is calculated from current time
- Job is created with
repeatIntervalset to the cron expression
After Completion
Section titled “After Completion”When a recurring job completes successfully:
- Status changes to
completed - Monque calculates next run time from current time
- Job is reset to
pendingwith newnextRunAt
Example Timeline
Section titled “Example Timeline”For '0 * * * *' (hourly at minute 0):
Preventing Duplicate Schedules
Section titled “Preventing Duplicate Schedules”Use uniqueKey to avoid creating another active job with the same name and key:
Error Handling
Section titled “Error Handling”Invalid Cron Expression
Section titled “Invalid Cron Expression”Monque throws InvalidCronError for invalid expressions:
Helpful Error Messages
Section titled “Helpful Error Messages”Monque provides detailed error messages for invalid expressions:
Timezone Considerations
Section titled “Timezone Considerations”Pass an IANA timezone to evaluate every occurrence in that zone, independently of the
server’s timezone. Monque saves the timezone on the job and uses it again after each
successful run. Execution timestamps such as nextRunAt remain JavaScript Date values.
Use timezone: 'UTC' for a fixed UTC schedule. An invalid or empty timezone rejects
with InvalidCronError before a job is created.
Omitting timezone preserves the existing behavior: cron expressions use the server’s
local timezone, including for previously stored jobs. Keep server timezones consistent
across workers that process those jobs.
Timezone rules include daylight saving changes. For Berlin’s daily 30 2 * * *
schedule, cron-parser moves the missing spring 02:30 occurrence to 03:30 and selects
the first 02:30 occurrence during the autumn repeated hour. Missed occurrences are not
backfilled; the next occurrence is calculated after successful completion.
Managing Scheduled Jobs
Section titled “Managing Scheduled Jobs”List Recurring Jobs
Section titled “List Recurring Jobs”Cancel a Scheduled Job
Section titled “Cancel a Scheduled Job”Cancel a recurring job while it is pending to stop future runs:
Use deleteJob(jobId) to remove the schedule permanently. A processing job cannot be
cancelled, and deleting its document does not interrupt the running handler. Stop the
scheduler and wait for handlers to finish if you need to remove an active schedule safely.
See Job Management for action constraints.
Best Practices
Section titled “Best Practices”1. Use Descriptive Names
Section titled “1. Use Descriptive Names”2. Include Context in Payload
Section titled “2. Include Context in Payload”3. Handle Overlapping Runs
Section titled “3. Handle Overlapping Runs”Use uniqueKey when registering a long-running schedule to avoid creating duplicate active jobs:
4. Log Schedule Information
Section titled “4. Log Schedule Information”Next Steps
Section titled “Next Steps”- Retry & Backoff - Handle scheduled job failures
- Workers - Configure worker concurrency
API Reference
Section titled “API Reference”Monque.schedule()- Schedule a recurring jobScheduleOptions- Schedule optionsJob- Job interface withrepeatInterval,nextRunAtInvalidCronError- Cron validation errorJobStatus- Job status constants