Running Scheduled Tasks

Running Scheduled Tasks

Moose provides a mechanism to schedule and execute arbitrary scripts or make HTTP GET calls at specified intervals via cron jobs. These jobs are useful for automating recurring tasks, for example:

  • Extracting batched data from an external data source on a schedule
  • Periodically calling an HTTP API to check for updates
  • Cleaning up old data on a schedule
  • Generating reports at a scheduled time each day
  • Any other recurring task you can imagine

Supported Task Types

  • Script Execution: Run arbitrary scripts written in Node.js (TypeScript supported), JavaScript, or Python.
  • HTTP Requests: Execute scheduled HTTP GET requests to specified endpoints.

Scheduling Jobs

Cron jobs are scheduled using an extended cron format that includes seconds. The cron expression consists of seven fields: seconds, minutes, hours, day of month, month, day of week, and optionally year. Each field can specify exact values, ranges, or wildcards to define the schedule.

Test Your Cron Spec

You can test your cron spec using this online tool (opens in a new tab).

Configuration

Cron jobs are defined within the moose.config.toml configuration file, under the [cron_jobs] array. Each job entry should include:

  • job_id: Unique identifier for the job.
  • cron_spec: Cron expression specifying the execution schedule.
  • script_path or url: Specifies the path to the arbitrary script file (TypeScript, JavaScript, or Python) or the URL of the HTTP endpoint.
Job ID Must Be Unique

Each job ID must be unique within the project. Moose will not allow duplicate job IDs.

Example:

[[cron_jobs]]
job_id = "Fetch Data Every Second"
cron_spec = "*/1 * * * * *" # Runs every second
script_path = "app/tasks/fetch_data.py"
 
[[cron_jobs]]
job_id = "Daily Report"
cron_spec = "0 0 0 * * *" # Runs at midnight every day
script_path = "app/tasks/report_generator.ts"
 
[[cron_jobs]]
job_id = "Hourly Health Check"
cron_spec = "0 0 * * * *" # Runs every hour
url = "http://localhost:4000/health"
 
[[cron_jobs]]
job_id = "Weekly Report"
cron_spec = "0 0 12 * * 0" # Runs every Sunday at 12pm
url = "app/tasks/weekly_report.js"
 
[[cron_jobs]]
job_id = "Bi-Monthly Cleanup"
cron_spec = "0 0 0 1,15 * *" # Runs at 00:00 on the 1st and 15th of every month
script_path = "app/tasks/bi_monthly_cleanup.py"
 
[[cron_jobs]]
job_id = "Hourly Business Hours"
cron_spec = "0 9-17 * * * 1-5" # Runs every hour during weekday business hours (9am-5pm)
script_path = "app/tasks/business_hours_task.ts"
 
Cron Format

Moose uses the tokio-cron-scheduler (opens in a new tab) library, which supports a more modern cron format with a seconds field. Note that standard crontab tools may not support this extended format.