Skip to main content
The scheduler package provides cron-based periodic task execution for running tasks on a schedule.

Scheduler Type

The Scheduler type represents a periodic task runner that executes an action based on a cron expression.

Constructor

(*Scheduler, error)
Creates a new Scheduler instance with a cron expression and runner.Parameters:
  • cronExpr (string): Cron expression or descriptor
  • runner (application.Runner): Task to execute on schedule
Returns:
  • *Scheduler: The scheduler instance
  • error: Validation error if cron expression is invalid
The constructor validates the cron expression eagerly, so errors are caught immediately rather than at runtime.

Methods

Run

error
Starts the scheduler and executes the runner according to the cron schedule. The scheduler continues running until the context is canceled.Behavior:
  • Tasks execute in UTC timezone
  • Each task execution gets a unique trace ID for logging
  • Errors in tasks are logged but don’t stop the scheduler
  • Returns when context is canceled

Cron Expression Formats

Platforma supports standard cron format with 5 fields, plus special descriptors and intervals.

Standard 5-Field Cron

0-59
Minute of the hour (0-59)
0-23
Hour of the day (0-23)
1-31
Day of the month (1-31)
1-12
Month of the year (1-12)
0-6
Day of the week (0-6, where 0 = Sunday)

Special Characters

  • * - Any value (e.g., * * * * * = every minute)
  • */n - Every n units (e.g., */5 * * * * = every 5 minutes)
  • n-m - Range (e.g., 0 9-17 * * * = every hour from 9 AM to 5 PM)
  • MON-FRI - Named weekdays (e.g., 0 9 * * MON-FRI = 9 AM on weekdays)

Descriptors

Special descriptors for common schedules:
string
Run once a year at midnight on January 1stEquivalent to: 0 0 1 1 *
string
Run once a month at midnight on the 1stEquivalent to: 0 0 1 * *
string
Run once a week at midnight on SundayEquivalent to: 0 0 * * 0
string
Run once a day at midnightEquivalent to: 0 0 * * *
string
Run once an hour at the beginning of the hourEquivalent to: 0 * * * *

Interval Syntax

For simple intervals, use the @every syntax:
string
Run at fixed intervalsSupported units: s (seconds), m (minutes), h (hours)Examples:
  • @every 1s - Every second
  • @every 30s - Every 30 seconds
  • @every 5m - Every 5 minutes
  • @every 2h - Every 2 hours

Cron Examples

Example Usage

Basic Scheduled Task

Multiple Schedulers with Application

Struct-Based Runner

Error Handling

The scheduler logs errors but continues running:

Trace IDs

Each scheduled task execution gets a unique trace ID for correlated logging: