Scheduling
Monque supports recurring job scheduling using standard 5-field cron expressions.
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 ensure only one scheduled instance exists:
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”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”To stop a recurring job, delete it from the database:
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”For long-running scheduled jobs, use uniqueKey to prevent overlap:
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