Moose workflows can be configured to run automatically on a schedule using cron expressions or interval-based scheduling. This enables you to automate recurring tasks, data processing jobs, and maintenance operations.
Workflows can be configured to run on a schedule using the schedule field in Workflow. This field is optional and blank by default.
import { Task, Workflow } from "@514labs/moose-lib"; export const myworkflow = new Workflow("myworkflow", { startingTask: task1, schedule: "0 12 * * *" // Runs at 12:00 PM every day});|------------------------------- Minute (0-59)| |------------------------- Hour (0-23)| | |------------------- Day of the month (1-31)| | | |------------- Month (1-12; or JAN to DEC)| | | | |------- Day of the week (0-6; or SUN to SAT; or 7 for Sunday)| | | | || | | | |* * * * *| Cron Expression | Description |
|---|---|
0 12 * * * | Runs at 12:00 PM every day |
0 0 * * 0 | Runs at 12:00 AM every Sunday |
0 8 * * 1-5 | Runs at 8:00 AM on weekdays (Monday to Friday) |
* * * * * |
| Runs every minute |
0 */6 * * * | Runs every 6 hours |
0 9 1 * * | Runs at 9:00 AM on the first day of every month |
0 0 1 1 * | Runs at midnight on January 1st every year |
Use an online cron expression visualizer like crontab.guru to help you understand how the cron expression will schedule your workflow.
Interval schedules can be specified as a string "@every <interval>". The interval follows standard duration format:
import { Task, Workflow } from "@514labs/moose-lib"; export const myworkflow = new Workflow("myworkflow", { startingTask: task1, schedule: "@every 1h" // Runs every hour});| Interval | Description |
|---|---|
@every 30s | Every 30 seconds |
@every 5m | Every 5 minutes |
@every 1h | Every hour |
@every 12h | Every 12 hours |
@every 24h | Every 24 hours |
@every 7d | Every 7 days |
import { Task, Workflow } from "@514labs/moose-lib"; export const dailyDataProcessing = new Workflow("daily-data-processing", { startingTask: extractDataTask, schedule: "0 2 * * *", // Run at 2 AM every day retries: 2, timeout: "2h",});export const weeklyReports = new Workflow("weekly-reports", { startingTask: generateReportTask, schedule: "0 9 * * 1", // Run at 9 AM every Monday retries: 1, timeout: "1h",});export const systemMonitoring = new Workflow("system-monitoring", { startingTask: checkSystemHealthTask, schedule: "@every 5m", // Check every 5 minutes retries: 0, // Don't retry monitoring checks timeout: "30s",});If your dev server is running, you should see logs in the terminal when your scheduled workflow is executed:
moose dev[2024-01-15 12:00:00] Scheduled workflow 'daily-data-processing' started[2024-01-15 12:00:01] Task 'extract' completed successfully[2024-01-15 12:00:15] Task 'transform' completed successfully[2024-01-15 12:00:30] Task 'load' completed successfully[2024-01-15 12:00:30] Workflow 'daily-data-processing' completed successfullyYou can check the status of scheduled workflows using the CLI:
# List all workflows defined in your project
moose workflow list
# Alternative command to list all workflows
moose ls --type workflows
# View workflow execution history
moose workflow history
# Check specific workflow status
moose workflow status daily-data-processing
# Get detailed execution history
moose workflow status daily-data-processing --verboseAccess the Temporal dashboard to view scheduled workflow executions:
# Open Temporal dashboard (typically at http://localhost:8080)
open http://localhost:8080The dashboard shows:
Configure appropriate timeouts and retries for scheduled workflows:
export const robustScheduledWorkflow = new Workflow("robust-scheduled", { startingTask: mainTask, schedule: "0 3 * * *", // Run at 3 AM daily retries: 2, // Retry failed workflows timeout: "4h", // Allow sufficient time}); export const mainTask = new Task<void, void>("main", { run: async () => { // Long-running task logic }, retries: 3, // Retry individual tasks timeout: "1h", // Task-level timeout});