Cron - Core Concepts
Architecture
The Cron function consists of several key components working together:
Core Components
- Cron (
cron.ts): The main entry point to schedule, validate, and manage tasks. - ScheduledTask (
scheduled-task.ts): Represents a scheduled task that runs in the main process. - BackgroundScheduledTask (
backgroundScheduledTask.ts): Runs tasks in a separate child process. - Scheduler (
scheduler.ts): Handles the timing logic for when to execute tasks. - Task (
task.ts): Wraps the actual function to be executed. - TimeZone (
timezone.ts): Handles timezone calculations and cron expression matching. - Storage (
storage.ts): Maintains task registry for retrieval and management. - Validation (
validatePattern.ts,validateExpression.ts): Ensures cron expressions are valid.
Cron Expressions
Syntax and Parsing
Scheduler supports standard cron expressions with an optional seconds field:
┌────────────── second (0 - 59) (optional)
│ ┌──────────── minute (0 - 59)
│ │ ┌────────── hour (0 - 23)
│ │ │ ┌──────── day of month (1 - 31)
│ │ │ │ ┌────── month (1 - 12) (or names)
│ │ │ │ │ ┌──── day of week (0 - 7) (0 or 7 is Sun, or names)
│ │ │ │ │ │
│ │ │ │ │ │
* * * * * *
Special Characters
*: Any value,: Value list separator (e.g.,1,3,5)-: Range of values (e.g.,1-5)/: Step values (e.g.,*/2for every 2 units)
Month and Day Names
- Month names: Both full names (
January,February, etc.) and abbreviations (Jan,Feb, etc.) - Weekday names: Both full names (
Sunday,Monday, etc.) and abbreviations (Sun,Mon, etc.)
Task Execution Flow
- When a task is scheduled, it's registered in the global storage.
- The scheduler continuously checks the current time against the cron pattern.
- When a match is found, the task is executed.
- For in-process tasks, the function is executed directly.
- For background tasks, a child process is forked to execute the task.
Execution Types
In-Process Execution (ScheduledTask)
┌───────────────┐ ┌───────────────┐ ┌──────┐
│ cron.schedule │=====> | ScheduledTask │====> │ Task │
└───────────────┘ └───────────────┘ └──────┘
│
┌─────▼─────┐
│ Scheduler │
└───────────┘
Background Execution (BackgroundScheduledTask)
┌───────────────┐ ┌─────────────────────────┐ ┌───────────────┐
│ cron.schedule │====> │ BackgroundScheduledTask │====> │ Child Process │
└───────────────┘ └─────────────────────────┘ └───────────────┘
│
┌──────▼────────┐
│ ScheduledTask │
└───────────────┘
Task Options
Common Options
scheduled: Whether to start the task immediately upon creationname: A unique identifier for the tasktimezone: The timezone to use for schedulingrecoverMissedExecutions: Whether to execute tasks that were missedrunOnInit: Whether to run the task immediately upon creation
Background Task Specific
- Background tasks require a path to a JavaScript file that exports a
taskfunction
Events
The function uses Node.js EventEmitter for communication:
ScheduledTask Events
task-done: Emitted when a task completes executiontask-finished: Emitted by the Task class when execution completes successfullytask-failed: Emitted by the Task class when execution fails
Scheduler Events
scheduled-time-matched: Emitted when the current time matches the cron pattern
Storage
Tasks are stored in a global Map, allowing for:
- Retrieval of all scheduled tasks
- Access to specific tasks by name
- Persistence of tasks during the application lifecycle
Validation
The validation process ensures cron expressions are valid by:
- Checking the format and number of fields
- Validating each field's range (seconds: 0-59, minutes: 0-59, etc.)
- Converting named months and weekdays to their numeric values
- Handling special characters like asterisks, ranges, and steps