This example demonstrates workflows that run on a schedule using cron expressions.
- Cron-based scheduling
- Timezone support
- Dynamic scheduling
- Disabling scheduling
- Daily cleanup tasks
- Periodic reports
- Recurring data synchronization
- Scheduled notifications
workflows.py- Scheduled workflow definitionsworker.py- Worker that registers workflows
-
Start the Polos server:
polos server start
-
Install dependencies:
# Using uv (recommended) uv sync # Or using pip pip install -e .
-
Set up environment variables:
cp .env.example .env
-
Run the worker:
python worker.py
┌───────────── minute (0-59)
│ ┌───────────── hour (0-23)
│ │ ┌───────────── day of month (1-31)
│ │ │ ┌───────────── month (1-12)
│ │ │ │ ┌───────────── day of week (0-6, Sun=0)
│ │ │ │ │
* * * * *
| Cron | Description |
|---|---|
0 * * * * |
Every hour |
0 0 * * * |
Daily at midnight |
0 8 * * * |
Daily at 8:00 AM |
0 8 * * 1-5 |
Weekdays at 8:00 AM |
0 9 * * 1 |
Mondays at 9:00 AM |
*/15 * * * * |
Every 15 minutes |
0 0 1 * * |
First of each month |
@workflow(id="daily_cleanup", schedule="0 3 * * *")
async def daily_cleanup(ctx, payload):
# Runs at 3:00 AM UTC every day
pass@workflow(
id="morning_report",
schedule={"cron": "0 8 * * 1-5", "timezone": "America/New_York"},
)
async def morning_report(ctx, payload):
# Runs at 8:00 AM Eastern, Monday-Friday
pass@workflow(id="reminder", schedule=True)
async def reminder(ctx, payload):
# Can be scheduled via API, no fixed schedule
pass@workflow(id="one_time", schedule=False)
async def one_time(ctx, payload):
# Cannot be scheduled, only invoked directly
passScheduled workflows receive a payload with:
{
"timestamp": "2024-01-01T08:00:00Z", # Scheduled execution time
"schedule_id": "sched-123", # Schedule identifier
}Supported timezone identifiers (IANA format):
America/New_YorkAmerica/Los_AngelesEurope/LondonEurope/ParisAsia/TokyoUTC(default)
- Use UTC for internal processing - Convert to local time only for display
- Idempotent operations - Scheduled jobs may run more than once
- Error handling - Implement proper error handling for reliability
- Logging - Log start/end times for debugging
- Avoid overlaps - Ensure previous run completes before next starts
View scheduled workflows in the Polos UI:
- Active schedules
- Next run time
- Last run status
- Execution history