Skip to content

How to run task in specific time #10

@danfimov

Description

@danfimov

I'm trying to create a simple script that will start task in specific time:

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",
                "time": datetime.now() + timedelta(seconds=10),
                "args": [],
                "kwargs": {},
                "labels": {},
            }
        )
    )

I'm starting worker:

> uv run taskiq worker example:broker --workers 1

[2025-10-11 02:01:46,494][taskiq.worker][INFO   ][MainProcess] Pid of a main process: 128475
[2025-10-11 02:01:46,494][taskiq.worker][INFO   ][MainProcess] Starting 1 worker processes.
[2025-10-11 02:01:46,495][taskiq.process-manager][INFO   ][MainProcess] Started process worker-0 with pid 128476
[2025-10-11 02:01:46,614][taskiq.receiver.receiver][INFO   ][worker-0] Listening started.

After that in separate terminal I'm starting scheduler:

> uv run taskiq scheduler example:scheduler

[2025-10-11 02:01:47,970][INFO   ][run:run_scheduler:272] Starting scheduler.
[2025-10-11 02:01:48,081][INFO   ][run:run_scheduler:274] Startup completed.

I checked that:

  • scheduler pulling schedules for tasks from time to time
  • row with schedule for task exists in database

I think it should work, but it's not (worker and scheduler just work endlessly). Maybe it's a bug?

Metadata

Metadata

Assignees

Labels

No labels
No labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions