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.
from moose_lib import Task, TaskConfig, Workflow, WorkflowConfig myworkflow = Workflow( name="myworkflow", config=WorkflowConfig(starting_task=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:
from moose_lib import Task, TaskConfig, Workflow, WorkflowConfig myworkflow = Workflow( name="myworkflow", config=WorkflowConfig(starting_task=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 |
from moose_lib import Workflow, WorkflowConfig daily_data_processing = Workflow( name="daily-data-processing", config=WorkflowConfig( starting_task=extract_data_task, schedule="0 2 * * *", # Run at 2 AM every day retries=2, timeout="2h" ))weekly_reports = Workflow( name="weekly-reports", config=WorkflowConfig( starting_task=generate_report_task, schedule="0 9 * * 1", # Run at 9 AM every Monday retries=1, timeout="1h" ))system_monitoring = Workflow( name="system-monitoring", config=WorkflowConfig( starting_task=check_system_health_task, 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:
def run_main_task() -> None: # Long-running task logic pass main_task = Task[None, None]( name="main", config=TaskConfig( run=run_main_task, retries=3, # Retry individual tasks timeout="1h" # Task-level timeout )) robust_scheduled_workflow = Workflow( name="robust-scheduled", config=WorkflowConfig( starting_task=main_task, schedule="0 3 * * *", # Run at 3 AM daily retries=2, # Retry failed workflows timeout="4h" # Allow sufficient time ))