Skip to content

[BUG] Schedule source with cron_offset isn't working #11

@danfimov

Description

@danfimov

taskiq-postgresql version

0.4.0

Python version

Python 3.12

PostgreSQL driver

asyncpg

PostgreSQL version

18

What happened?

I tried to create a simple script with scheduler that uses cron_offset and put timedelta here. ScheduledTask model allowed me to do it, so I assumed that it should work.

Expected behavior

Task should work every minute (with cron_offset)

Steps to reproduce

  1. Run scheduler with:
uv run taskiq scheduler example:scheduler
  1. See error and cry

Minimal code example

#example.py
from taskiq_postgresql import PostgresqlBroker
from taskiq_postgresql.scheduler_source import PostgresqlSchedulerSource
from taskiq import TaskiqScheduler, ScheduledTask, TaskiqEvents
import uuid
from datetime import datetime, timedelta


dsn = "..."
broker = PostgresqlBroker(
    dsn=dsn,
    driver="asyncpg",
    run_migrations=True,
)
scheduler_source = PostgresqlSchedulerSource(
    dsn=dsn,
    table_name="taskiq_schedules",
    driver="asyncpg",
    run_migrations=True,
)

scheduler = TaskiqScheduler(
    broker=broker,
    sources=[scheduler_source],
)

@broker.task(task_name="scheduled_task")
async def scheduled_task():
    print("This task runs on schedule!")


@broker.on_event(TaskiqEvents.CLIENT_STARTUP)
async def setup_schedule(_):
    await scheduler_source.add_schedule(
        ScheduledTask.model_validate(
            {
                "schedule_id": uuid.uuid4().hex,
                "task_name": "scheduled_task",
                "cron": "*/1 * * * *",
                "cron_offset": timedelta(hours=1),  # added timedelta because ScheduledTask allow me to
                "args": [],
                "kwargs": {},
                "labels": {},
            }
        )
    )

Relevant log output

[2025-10-11 02:06:22,762][INFO   ][run:run_scheduler:272] Starting scheduler.
[2025-10-11 02:06:22,845][INFO   ][run:run_scheduler:274] Startup completed.
Traceback (most recent call last):
  File "/home/danfimov/Documents/fun/taskiq-postgres/.venv/bin/taskiq", line 10, in <module>
    sys.exit(main())
             ^^^^^^
  File "/home/danfimov/Documents/fun/taskiq-postgres/.venv/lib/python3.12/site-packages/taskiq/__main__.py", line 73, in main
    status = command.exec(sys.argv[1:])
             ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/danfimov/Documents/fun/taskiq-postgres/.venv/lib/python3.12/site-packages/taskiq/cli/scheduler/cmd.py", line 26, in exec
    asyncio.run(run_scheduler(parsed))
  File "/home/danfimov/.local/share/uv/python/cpython-3.12.11-linux-x86_64-gnu/lib/python3.12/asyncio/runners.py", line 195, in run
    return runner.run(main)
           ^^^^^^^^^^^^^^^^
  File "/home/danfimov/.local/share/uv/python/cpython-3.12.11-linux-x86_64-gnu/lib/python3.12/asyncio/runners.py", line 118, in run
    return self._loop.run_until_complete(task)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/danfimov/.local/share/uv/python/cpython-3.12.11-linux-x86_64-gnu/lib/python3.12/asyncio/base_events.py", line 691, in run_unti
    return future.result()
           ^^^^^^^^^^^^^^^
  File "/home/danfimov/Documents/fun/taskiq-postgres/.venv/lib/python3.12/site-packages/taskiq/cli/scheduler/run.py", line 288, in run_sched
    await run_scheduler_loop(scheduler, interval)
  File "/home/danfimov/Documents/fun/taskiq-postgres/.venv/lib/python3.12/site-packages/taskiq/cli/scheduler/run.py", line 184, in run_scheduler_loop
    task_delay = get_task_delay(task)
                 ^^^^^^^^^^^^^^^^^^^^
  File "/home/danfimov/Documents/fun/taskiq-postgres/.venv/lib/python3.12/site-packages/taskiq/cli/scheduler/run.py", line 93, in get_task_delay
    now = now.astimezone(pytz.timezone(task.cron_offset))
                         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/danfimov/Documents/fun/taskiq-postgres/.venv/lib/python3.12/site-packages/pytz/__init__.py", line 188, in timezone
    raise UnknownTimeZoneError(zone)
pytz.exceptions.UnknownTimeZoneError: 'PT1H'

Additional context

No response

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions