Skip to main content
DeployStack includes a cron job scheduling system that integrates seamlessly with the Background Job Queue. This allows you to schedule recurring tasks using standard cron expressions, with all the benefits of the job queue system including persistence, retries, and monitoring.

Architecture

The cron system follows a two-tier architecture:
  1. Cron Scheduler: Uses node-cron to schedule tasks based on cron expressions
  2. Job Queue: Processes the actual work with persistence and retry capabilities
This separation provides:
  • Reliability: Jobs persist even if the server restarts
  • Visibility: All jobs are logged and trackable in the database
  • Rate Limiting: Built-in queue management prevents system overload
  • Monitoring: Track success/failure rates and execution history

Creating a Cron Job

Step 1: Define the Cron Job

Create a new file in src/cron/jobs/:
The CronJob interface has these fields:

Step 2: Create the Worker

Create a worker to process the job in src/workers/:

Step 3: Register the Worker

Add the worker to src/workers/index.ts:

Step 4: Register the Cron Job

Add the cron job to src/cron/index.ts:
The CronManager handles job creation automatically when the schedule fires. You don’t need to call createJob() yourself - just define the jobType and payload in your cron job definition.

Cron Expression Syntax

The system uses standard cron syntax with 5 or 6 fields:

Common Examples

Integration with Job Queue

The cron system is designed to work with the job queue system. This provides several benefits: Persistence: Jobs created by cron are stored in the database and survive server restarts. Retry Logic: Failed jobs are automatically retried with exponential backoff. Rate Limiting: The job queue processes jobs sequentially, preventing system overload. Monitoring: Track job execution history, success rates, and failures. For more details on the job queue system, see the Background Job Queue documentation.

Example: Complete Implementation

Here’s a complete example showing how to create a cron job that sends a daily email digest:
For dynamic payload values computed at runtime, use a function:
The send_email worker (already registered in the system) will process this job using the existing Email System.

Lifecycle Management

The cron system is automatically initialized during server startup and gracefully shut down when the server stops: Startup: All registered cron jobs are scheduled and begin running according to their expressions. Shutdown: When the server receives a shutdown signal, cron jobs stop creating new jobs, allowing the job queue to finish processing existing jobs. This ensures no jobs are lost during server restarts or deployments.