Перейти к содержанию

taskiq_pg

aiopg

AiopgBroker

AiopgBroker(
    dsn: str | Callable[[], str] = ...,
    result_backend: AsyncResultBackend[_T] | None = ...,
    task_id_generator: Callable[[], str] | None = ...,
    channel_name: str = ...,
    table_name: str = ...,
    max_retry_attempts: int = ...,
    read_kwargs: dict[str, Any] | None = ...,
    write_kwargs: dict[str, Any] | None = ...,
    *,
    write_pool: None = ...,
    read_connection: None = ...,
    listen_keepalive_interval: float | None = ...,
    listen_keepalive_timeout: float = ...,
    poll_interval: float = ...,
    processing_timeout: float | None = ...,
)
AiopgBroker(
    dsn: str | Callable[[], str] = ...,
    result_backend: AsyncResultBackend[_T] | None = ...,
    task_id_generator: Callable[[], str] | None = ...,
    channel_name: str = ...,
    table_name: str = ...,
    max_retry_attempts: int = ...,
    read_kwargs: dict[str, Any] | None = ...,
    write_kwargs: dict[str, Any] | None = ...,
    *,
    write_pool: Pool,
    read_connection: None = ...,
    listen_keepalive_interval: float | None = ...,
    listen_keepalive_timeout: float = ...,
    poll_interval: float = ...,
    processing_timeout: float | None = ...,
)
AiopgBroker(
    dsn: str | Callable[[], str] = ...,
    result_backend: AsyncResultBackend[_T] | None = ...,
    task_id_generator: Callable[[], str] | None = ...,
    channel_name: str = ...,
    table_name: str = ...,
    max_retry_attempts: int = ...,
    read_kwargs: dict[str, Any] | None = ...,
    write_kwargs: dict[str, Any] | None = ...,
    *,
    write_pool: None = ...,
    read_connection: Connection,
    listen_keepalive_interval: float | None = ...,
    listen_keepalive_timeout: float = ...,
    poll_interval: float = ...,
    processing_timeout: float | None = ...,
)
AiopgBroker(
    dsn: str | Callable[[], str] = ...,
    result_backend: AsyncResultBackend[_T] | None = ...,
    task_id_generator: Callable[[], str] | None = ...,
    channel_name: str = ...,
    table_name: str = ...,
    max_retry_attempts: int = ...,
    read_kwargs: dict[str, Any] | None = ...,
    write_kwargs: dict[str, Any] | None = ...,
    *,
    write_pool: Pool,
    read_connection: Connection,
    listen_keepalive_interval: float | None = ...,
    listen_keepalive_timeout: float = ...,
    poll_interval: float = ...,
    processing_timeout: float | None = ...,
)
AiopgBroker(
    dsn="postgresql://postgres:postgres@localhost:5432/postgres",
    result_backend=None,
    task_id_generator=None,
    channel_name="taskiq",
    table_name="taskiq_messages",
    max_retry_attempts=5,
    read_kwargs=None,
    write_kwargs=None,
    *,
    write_pool=None,
    read_connection=None,
    listen_keepalive_interval=30.0,
    listen_keepalive_timeout=5.0,
    poll_interval=5.0,
    processing_timeout=None,
)

Bases: BasePostgresBroker

Broker that uses aiopg as driver and PostgreSQL with LISTEN/NOTIFY mechanism.

Construct a new AiopgBroker.

Parameters:

  • dsn (str | Callable[[], str], default: 'postgresql://postgres:postgres@localhost:5432/postgres' ) –

    PostgreSQL connection string or a callable returning one.

  • result_backend (AsyncResultBackend[_T] | None, default: None ) –

    Custom result backend.

  • task_id_generator (Callable[[], str] | None, default: None ) –

    Custom task_id generator.

  • channel_name (str, default: 'taskiq' ) –

    Name of the LISTEN/NOTIFY channel.

  • table_name (str, default: 'taskiq_messages' ) –

    Name of the table used to store messages.

  • max_retry_attempts (int, default: 5 ) –

    Maximum number of message processing attempts.

  • read_kwargs (dict[str, Any] | None, default: None ) –

    Extra kwargs forwarded to aiopg.connect().

  • write_kwargs (dict[str, Any] | None, default: None ) –

    Extra kwargs forwarded to aiopg.create_pool().

  • write_pool (Pool | None, default: None ) –

    An existing connection pool to reuse for writes.

  • read_connection (Connection | None, default: None ) –

    An existing connection to reuse for LISTEN. Its lifecycle, including reconnection, remains the caller's responsibility.

  • listen_keepalive_interval (float | None, default: 30.0 ) –

    How often, in seconds, to check the broker-owned LISTEN connection with a query; None disables the check.

  • listen_keepalive_timeout (float, default: 5.0 ) –

    How long, in seconds, to wait for the check before reconnecting.

  • poll_interval (float, default: 5.0 ) –

    How often, in seconds, to look for pending messages whose NOTIFY was missed.

  • processing_timeout (float | None, default: None ) –

    Seconds after which an unacknowledged claimed message is delivered again (at-least-once delivery); None keeps at-most-once delivery.

Source code in src/taskiq_pg/aiopg/broker.py
def __init__(  # noqa: PLR0913
    self,
    dsn: str | Callable[[], str] = "postgresql://postgres:postgres@localhost:5432/postgres",
    result_backend: AsyncResultBackend[_T] | None = None,
    task_id_generator: Callable[[], str] | None = None,
    channel_name: str = "taskiq",
    table_name: str = "taskiq_messages",
    max_retry_attempts: int = 5,
    read_kwargs: dict[str, tp.Any] | None = None,
    write_kwargs: dict[str, tp.Any] | None = None,
    *,
    write_pool: aiopg.Pool | None = None,
    read_connection: aiopg.Connection | None = None,
    listen_keepalive_interval: float | None = 30.0,
    listen_keepalive_timeout: float = 5.0,
    poll_interval: float = 5.0,
    processing_timeout: float | None = None,
) -> None:
    """
    Construct a new AiopgBroker.

    Args:
        dsn: PostgreSQL connection string or a callable returning one.
        result_backend: Custom result backend.
        task_id_generator: Custom task_id generator.
        channel_name: Name of the LISTEN/NOTIFY channel.
        table_name: Name of the table used to store messages.
        max_retry_attempts: Maximum number of message processing attempts.
        read_kwargs: Extra kwargs forwarded to `aiopg.connect()`.
        write_kwargs: Extra kwargs forwarded to `aiopg.create_pool()`.
        write_pool: An existing connection pool to reuse for writes.
        read_connection: An existing connection to reuse for LISTEN.
            Its lifecycle, including reconnection, remains the caller's responsibility.
        listen_keepalive_interval: How often, in seconds, to check the broker-owned LISTEN connection
            with a query; None disables the check.
        listen_keepalive_timeout: How long, in seconds, to wait for the check before reconnecting.
        poll_interval: How often, in seconds, to look for pending messages whose NOTIFY was missed.
        processing_timeout: Seconds after which an unacknowledged claimed message is delivered again
            (at-least-once delivery); None keeps at-most-once delivery.
    """
    super().__init__(
        dsn=dsn,
        result_backend=result_backend,
        task_id_generator=task_id_generator,
        channel_name=channel_name,
        table_name=table_name,
        max_retry_attempts=max_retry_attempts,
        read_kwargs=read_kwargs,
        write_kwargs=write_kwargs,
        listen_keepalive_interval=listen_keepalive_interval,
        listen_keepalive_timeout=listen_keepalive_timeout,
        poll_interval=poll_interval,
        processing_timeout=processing_timeout,
    )

    self._owns_write_pool = True
    if write_pool is not None:
        self._write_pool = write_pool
        self._owns_write_pool = False

    self._owns_read_conn = True
    if read_connection is not None:
        self._read_conn = read_connection
        self._owns_read_conn = False

    self._notify_tasks = set()

dsn property

dsn

Get the DSN string.

channel_name instance-attribute

channel_name = channel_name

table_name instance-attribute

table_name = table_name

read_kwargs instance-attribute

read_kwargs = read_kwargs or {}

write_kwargs instance-attribute

write_kwargs = write_kwargs or {}

max_retry_attempts instance-attribute

max_retry_attempts = max_retry_attempts

listen_keepalive_interval instance-attribute

listen_keepalive_interval = listen_keepalive_interval

listen_keepalive_timeout instance-attribute

listen_keepalive_timeout = listen_keepalive_timeout

poll_interval instance-attribute

poll_interval = poll_interval

processing_timeout instance-attribute

processing_timeout = processing_timeout

startup async

startup()

Initialize the broker.

Source code in src/taskiq_pg/aiopg/broker.py
async def startup(self) -> None:
    """Initialize the broker."""
    await super().startup()
    self._queue = asyncio.Queue()
    if self._write_pool is None:
        self._write_pool = await aiopg.create_pool(self.dsn, **self.write_kwargs)
    async with self._write_pool.acquire() as conn, conn.cursor() as cursor:
        # aiopg connections are always in autocommit mode, so the transaction is explicit.
        await cursor.execute("BEGIN")
        try:
            await cursor.execute(schema_lock_query(self.table_name))
            await cursor.execute(
                SCHEMA_STATUS_QUERY.format(
                    table="%(table)s",
                    pending_index="%(pending_index)s",
                    processing_index="%(processing_index)s",
                ),
                {
                    "table": self.table_name,
                    "pending_index": f"{self.table_name}_pending_idx",
                    "processing_index": f"{self.table_name}_processing_idx",
                },
            )
            has_table, has_delivery_columns, has_pending_index, has_processing_index = await cursor.fetchone()
            if not has_table:
                await cursor.execute(CREATE_MESSAGE_TABLE_QUERY.format(self.table_name))
            if not has_delivery_columns:
                await cursor.execute(ADD_DELIVERY_COLUMNS_QUERY.format(table=self.table_name))
            if not has_pending_index:
                await cursor.execute(
                    CREATE_PENDING_INDEX_QUERY.format(
                        index=f"{self.table_name}_pending_idx",
                        table=self.table_name,
                    ),
                )
            if self.processing_timeout is not None and not has_processing_index:
                await cursor.execute(
                    CREATE_PROCESSING_INDEX_QUERY.format(
                        index=f"{self.table_name}_processing_idx",
                        table=self.table_name,
                    ),
                )
        except BaseException:
            await cursor.execute("ROLLBACK")
            raise
        await cursor.execute("COMMIT")
    if self._owns_read_conn:
        self._read_conn = await self._connect_listener()
    elif self._read_conn is not None:
        await self._execute(self._read_conn, f"LISTEN {self.channel_name}")
    self._listener_task = asyncio.create_task(self._consume_notifications())

shutdown async

shutdown()

Close all connections on shutdown.

Source code in src/taskiq_pg/aiopg/broker.py
async def shutdown(self) -> None:
    """Close all connections on shutdown."""
    if self._listener_task is not None:
        await cancel_and_wait(self._listener_task)
        self._listener_task = None
    for task in list(self._notify_tasks):
        await cancel_and_wait(task)
    await super().shutdown()
    if self._read_conn is not None:
        if self._owns_read_conn:
            await self._read_conn.close()
            self._read_conn = None
        elif not self._read_conn.closed:
            await self._execute(self._read_conn, f"UNLISTEN {self.channel_name}")
    if self._write_pool is not None and self._owns_write_pool:
        self._write_pool.close()
        await self._write_pool.wait_closed()
        self._write_pool = None

kick async

kick(message)

Send message to the channel.

Inserts the message into the database and sends a NOTIFY.

:param message: Message to send.

Source code in src/taskiq_pg/aiopg/broker.py
async def kick(self, message: BrokerMessage) -> None:
    """
    Send message to the channel.

    Inserts the message into the database and sends a NOTIFY.

    :param message: Message to send.
    """
    if self._write_pool is None:
        msg = "Please run startup before kicking."
        raise ValueError(msg)

    # Parse the delay before inserting, so a malformed label does not leave a message behind.
    delay_value = message.labels.get("delay")
    delay_seconds = None if delay_value is None else int(delay_value)

    async with self._write_pool.acquire() as conn, conn.cursor() as cursor:
        await cursor.execute(
            INSERT_MESSAGE_QUERY.format(self.table_name),
            (message.task_id, message.task_name, message.message.decode(), json.dumps(message.labels)),
        )
        row = await cursor.fetchone()
        if row is None:
            msg = "failed to insert message"
            raise RuntimeError(msg)
        message_id = int(row[0])

        if delay_seconds is None:
            # Send a NOTIFY with the message ID as payload
            await cursor.execute(f"NOTIFY {self.channel_name}, %s", (str(message_id),))
            return

    task = asyncio.create_task(self._schedule_notification(message_id, delay_seconds))
    self._notify_tasks.add(task)
    task.add_done_callback(self._notify_tasks.discard)

listen async

listen()

Listen to the channel.

Yields messages as they are received.

:yields: AckableMessage instances.

Source code in src/taskiq_pg/aiopg/broker.py
async def listen(self) -> AsyncGenerator[AckableMessage, None]:
    """
    Listen to the channel.

    Yields messages as they are received.

    :yields: AckableMessage instances.
    """
    if self._write_pool is None:
        msg = "Call startup before starting listening."
        raise ValueError(msg)
    if self._queue is None:
        msg = "Startup did not initialize the queue."
        raise ValueError(msg)

    queue = self._queue
    loop = asyncio.get_running_loop()
    next_poll = loop.time()  # poll right away: messages may have been kicked while nobody listened
    while True:
        try:
            if loop.time() >= next_poll:
                next_poll = loop.time() + self.poll_interval
                await self._release_stale_messages()
                while (
                    claimed := await self._claim(CLAIM_NEXT_READY_MESSAGE_QUERY.format(table=self.table_name))
                ) is not None:
                    yield self._ackable(claimed)
                continue
            try:
                payload = await asyncio.wait_for(queue.get(), next_poll - loop.time())
            except TimeoutError:
                continue
            if payload == POLL_NOW:
                next_poll = loop.time()
                continue
            claimed = await self._claim(CLAIM_MESSAGE_QUERY.format(self.table_name), (int(payload),))
            if claimed is not None:
                yield self._ackable(claimed)
        except Exception:
            logger.exception("Error processing message")
            continue

AiopgResultBackend

AiopgResultBackend(
    dsn: Callable[[], str] | str | None = ...,
    keep_results: bool = ...,
    table_name: str = ...,
    field_for_task_id: Literal[
        "VarChar", "Text", "Uuid"
    ] = ...,
    serializer: TaskiqSerializer | None = ...,
    *,
    pool: None = ...,
    **connect_kwargs: Any,
)
AiopgResultBackend(
    dsn: Callable[[], str] | str | None = ...,
    keep_results: bool = ...,
    table_name: str = ...,
    field_for_task_id: Literal[
        "VarChar", "Text", "Uuid"
    ] = ...,
    serializer: TaskiqSerializer | None = ...,
    *,
    pool: Pool,
)
AiopgResultBackend(
    dsn="postgres://postgres:postgres@localhost:5432/postgres",
    keep_results=True,
    table_name="taskiq_results",
    field_for_task_id="VarChar",
    serializer=None,
    *,
    pool=None,
    **connect_kwargs,
)

Bases: BasePostgresResultBackend

Result backend for TaskIQ based on Aiopg.

Construct a new AiopgResultBackend.

Parameters:

  • dsn (Callable[[], str] | str | None, default: 'postgres://postgres:postgres@localhost:5432/postgres' ) –

    PostgreSQL connection string or callable. Can be None if pool is provided.

  • keep_results (bool, default: True ) –

    Whether to keep results after reading.

  • table_name (str, default: 'taskiq_results' ) –

    Table to store results in.

  • field_for_task_id (Literal['VarChar', 'Text', 'Uuid'], default: 'VarChar' ) –

    Column type for task_id.

  • serializer (TaskiqSerializer | None, default: None ) –

    Serializer for task results.

  • pool (Pool | None, default: None ) –

    An existing connection pool to reuse.

  • **connect_kwargs (Any, default: {} ) –

    Extra kwargs for connection pool creation.

Source code in src/taskiq_pg/aiopg/result_backend.py
def __init__(
    self,
    dsn: tp.Callable[[], str] | str | None = "postgres://postgres:postgres@localhost:5432/postgres",
    keep_results: bool = True,
    table_name: str = "taskiq_results",
    field_for_task_id: tp.Literal["VarChar", "Text", "Uuid"] = "VarChar",
    serializer: TaskiqSerializer | None = None,
    *,
    pool: Pool | None = None,
    **connect_kwargs: tp.Any,
) -> None:
    """
    Construct a new AiopgResultBackend.

    Args:
        dsn: PostgreSQL connection string or callable. Can be None if pool is provided.
        keep_results: Whether to keep results after reading.
        table_name: Table to store results in.
        field_for_task_id: Column type for task_id.
        serializer: Serializer for task results.
        pool: An existing connection pool to reuse.
        **connect_kwargs: Extra kwargs for connection pool creation.
    """
    self._owns_pool = True
    if pool is not None:
        self._owns_pool = False
        self._database_pool = pool

    super().__init__(
        dsn=dsn,
        keep_results=keep_results,
        table_name=table_name,
        field_for_task_id=field_for_task_id,
        serializer=serializer,
        **connect_kwargs,
    )

dsn property

dsn

Get the DSN string.

keep_results instance-attribute

keep_results = keep_results

table_name instance-attribute

table_name = table_name

field_for_task_id instance-attribute

field_for_task_id = field_for_task_id

connect_kwargs instance-attribute

connect_kwargs = connect_kwargs

serializer instance-attribute

serializer = serializer or PickleSerializer()

startup async

startup()

Initialize the result backend.

Construct new connection pool (if not provided externally) and create new table for results if not exists.

Source code in src/taskiq_pg/aiopg/result_backend.py
async def startup(self) -> None:
    """
    Initialize the result backend.

    Construct new connection pool (if not provided externally) and create new table for results if not exists.
    """
    try:
        if self._owns_pool:
            self._database_pool = await create_pool(
                self.dsn,
                **self.connect_kwargs,
            )

        async with self._database_pool.acquire() as connection, connection.cursor() as cursor:
            await cursor.execute(
                queries.CREATE_TABLE_QUERY.format(
                    self.table_name,
                    self.field_for_task_id,
                ),
            )
            await cursor.execute(
                queries.ADD_PROGRESS_COLUMN_QUERY.format(
                    self.table_name,
                    self.field_for_task_id,
                ),
            )
            await cursor.execute(
                queries.CREATE_INDEX_QUERY.format(
                    self.table_name,
                    self.table_name,
                ),
            )
    except Exception as error:
        raise exceptions.DatabaseConnectionError(str(error)) from error

shutdown async

shutdown()

Close the connection pool if created by this backend.

Source code in src/taskiq_pg/aiopg/result_backend.py
async def shutdown(self) -> None:
    """Close the connection pool if created by this backend."""
    if self._owns_pool and getattr(self, "_database_pool", None) is not None:
        self._database_pool.close()

set_result async

set_result(task_id, result)

Set result to the PostgreSQL table.

Parameters:

  • task_id (Any) –

    ID of the task.

  • result (TaskiqResult[_ReturnType]) –

    result of the task.

Source code in src/taskiq_pg/aiopg/result_backend.py
async def set_result(
    self,
    task_id: tp.Any,
    result: TaskiqResult[ReturnType],
) -> None:
    """
    Set result to the PostgreSQL table.

    Args:
        task_id (Any): ID of the task.
        result (TaskiqResult[_ReturnType]):  result of the task.

    """
    dumped_result = self.serializer.dumpb(result)
    async with self._database_pool.acquire() as connection, connection.cursor() as cursor:
        await cursor.execute(
            queries.INSERT_RESULT_QUERY.format(
                self.table_name,
            ),
            (
                task_id,
                dumped_result,
                dumped_result,
            ),
        )

is_result_ready async

is_result_ready(task_id)

Return whether the result is ready.

Parameters:

  • task_id (Any) –

    ID of the task.

Returns:

  • bool ( bool ) –

    True if the result is ready else False.

Source code in src/taskiq_pg/aiopg/result_backend.py
async def is_result_ready(
    self,
    task_id: tp.Any,
) -> bool:
    """
    Return whether the result is ready.

    Args:
        task_id (Any): ID of the task.

    Returns:
        bool: True if the result is ready else False.

    """
    async with self._database_pool.acquire() as connection, connection.cursor() as cursor:
        await cursor.execute(
            queries.IS_RESULT_EXISTS_QUERY.format(
                self.table_name,
            ),
            (task_id,),
        )
        result = await cursor.fetchone()
        return bool(result[0]) if result else False

get_result async

get_result(task_id, with_logs=False)

Retrieve result from the task.

:param task_id: task's id. :param with_logs: if True it will download task's logs. :raises ResultIsMissingError: if there is no result when trying to get it. :return: TaskiqResult.

Source code in src/taskiq_pg/aiopg/result_backend.py
async def get_result(
    self,
    task_id: tp.Any,
    with_logs: bool = False,
) -> TaskiqResult[ReturnType]:
    """
    Retrieve result from the task.

    :param task_id: task's id.
    :param with_logs: if True it will download task's logs.
    :raises ResultIsMissingError: if there is no result when trying to get it.
    :return: TaskiqResult.
    """
    async with self._database_pool.acquire() as connection, connection.cursor() as cursor:
        await cursor.execute(
            queries.SELECT_RESULT_QUERY.format(
                self.table_name,
            ),
            (task_id,),
        )
        result = await cursor.fetchone()

        if not result:
            msg = f"Cannot find record with task_id = {task_id} in PostgreSQL"
            raise exceptions.ResultIsMissingError(
                msg,
            )

        result_in_bytes: bytes = result[0]

        if not self.keep_results:
            await cursor.execute(
                queries.DELETE_RESULT_QUERY.format(
                    self.table_name,
                ),
                (task_id,),
            )

        taskiq_result: TaskiqResult[ReturnType] = self.serializer.loadb(
            result_in_bytes,
        )

        if not with_logs:
            taskiq_result.log = None

        return taskiq_result

set_progress async

set_progress(task_id, progress)

Saves progress.

:param task_id: task's id. :param progress: progress of execution.

Source code in src/taskiq_pg/aiopg/result_backend.py
async def set_progress(
    self,
    task_id: str,
    progress: TaskProgress[tp.Any],
) -> None:
    """
    Saves progress.

    :param task_id: task's id.
    :param progress: progress of execution.
    """
    dumped_progress = self.serializer.dumpb(progress)
    async with self._database_pool.acquire() as connection, connection.cursor() as cursor:
        await cursor.execute(
            queries.INSERT_PROGRESS_QUERY.format(
                self.table_name,
            ),
            (
                task_id,
                dumped_progress,
                dumped_progress,
            ),
        )

get_progress async

get_progress(task_id)

Gets progress.

:param task_id: task's id.

Source code in src/taskiq_pg/aiopg/result_backend.py
async def get_progress(
    self,
    task_id: str,
) -> TaskProgress[tp.Any] | None:
    """
    Gets progress.

    :param task_id: task's id.
    """
    async with self._database_pool.acquire() as connection, connection.cursor() as cursor:
        await cursor.execute(
            queries.SELECT_PROGRESS_QUERY.format(
                self.table_name,
            ),
            (task_id,),
        )
        progress = await cursor.fetchone()
        if not progress or progress[0] is None:
            return None
        progress_in_bytes: bytes = progress[0]
        taskiq_progress: TaskProgress[tp.Any] = self.serializer.loadb(progress_in_bytes)
        return taskiq_progress

AiopgScheduleSource

AiopgScheduleSource(
    broker: AsyncBroker,
    dsn: str | Callable[[], str] = ...,
    table_name: str = ...,
    *,
    pool: None = ...,
    **connect_kwargs: Any,
)
AiopgScheduleSource(
    broker: AsyncBroker,
    dsn: str | Callable[[], str] = ...,
    table_name: str = ...,
    *,
    pool: Pool,
)
AiopgScheduleSource(
    broker,
    dsn="postgresql://postgres:postgres@localhost:5432/postgres",
    table_name="taskiq_schedules",
    *,
    pool=None,
    **connect_kwargs,
)

Bases: BasePostgresScheduleSource

Schedule source that uses aiopg to store schedules in PostgreSQL.

Construct a new AiopgScheduleSource.

Parameters:

  • broker (AsyncBroker) –

    The TaskIQ broker instance.

  • dsn (str | Callable[[], str], default: 'postgresql://postgres:postgres@localhost:5432/postgres' ) –

    PostgreSQL connection string or callable. Ignored in pool mode.

  • table_name (str, default: 'taskiq_schedules' ) –

    Table to store schedules in.

  • pool (Pool | None, default: None ) –

    An existing connection pool to reuse.

  • **connect_kwargs (Any, default: {} ) –

    Extra kwargs for connection pool creation.

Source code in src/taskiq_pg/aiopg/schedule_source.py
def __init__(
    self,
    broker: AsyncBroker,
    dsn: str | tp.Callable[[], str] = "postgresql://postgres:postgres@localhost:5432/postgres",
    table_name: str = "taskiq_schedules",
    *,
    pool: Pool | None = None,
    **connect_kwargs: tp.Any,
) -> None:
    """
    Construct a new AiopgScheduleSource.

    Args:
        broker: The TaskIQ broker instance.
        dsn: PostgreSQL connection string or callable. Ignored in pool mode.
        table_name: Table to store schedules in.
        pool: An existing connection pool to reuse.
        **connect_kwargs: Extra kwargs for connection pool creation.
    """
    self._owns_pool = True
    if pool is not None:
        self._owns_pool = False
        self._database_pool = pool

    super().__init__(
        broker=broker,
        dsn=dsn,
        table_name=table_name,
        **connect_kwargs,
    )

dsn property

dsn

Get the DSN string.

extract_scheduled_tasks_from_broker

extract_scheduled_tasks_from_broker()

Extract schedules from tasks that were registered in broker.

Returns:

  • list[ScheduledTask] –

    A list of ScheduledTask instances extracted from the task's labels.

Source code in src/taskiq_pg/_internal/schedule_source.py
def extract_scheduled_tasks_from_broker(self) -> list[ScheduledTask]:
    """
    Extract schedules from tasks that were registered in broker.

    Returns:
        A list of ScheduledTask instances extracted from the task's labels.
    """
    scheduled_tasks_for_creation: list[ScheduledTask] = []
    for task_name, task in self._broker.get_all_tasks().items():
        if "schedule" not in task.labels:
            logger.debug("Task %s has no schedule, skipping", task_name)
            continue
        if not isinstance(task.labels["schedule"], list):
            logger.warning(
                "Schedule for task %s is not a list, skipping",
                task_name,
            )
            continue
        for schedule in task.labels["schedule"]:
            try:
                new_schedule = ScheduledTask.model_validate(
                    {
                        "task_name": task_name,
                        "labels": schedule.get("labels", {}),
                        "args": schedule.get("args", []),
                        "kwargs": schedule.get("kwargs", {}),
                        "schedule_id": str(uuid.uuid4()),
                        "cron": schedule.get("cron", None),
                        "cron_offset": schedule.get("cron_offset", None),
                        "interval": schedule.get("interval", None),
                        "time": schedule.get("time", None),
                    },
                )
                scheduled_tasks_for_creation.append(new_schedule)
            except ValidationError:  # noqa: PERF203
                logger.exception(
                    "Schedule for task %s is not valid, skipping",
                    task_name,
                )
                continue
    return scheduled_tasks_for_creation

startup async

startup()

Initialize the schedule source.

Construct new connection pool (if not provided externally), create new table for schedules if not exists and fill table with schedules from task labels.

Source code in src/taskiq_pg/aiopg/schedule_source.py
async def startup(self) -> None:
    """
    Initialize the schedule source.

    Construct new connection pool (if not provided externally), create new table for schedules if not exists
    and fill table with schedules from task labels.
    """
    try:
        if self._owns_pool:
            self._database_pool = await create_pool(
                dsn=self.dsn,
                **self._connect_kwargs,
            )
        async with self._database_pool.acquire() as connection, connection.cursor() as cursor:
            await cursor.execute(CREATE_SCHEDULES_TABLE_QUERY.format(self._table_name))
        scheduled_tasks_for_creation = self.extract_scheduled_tasks_from_broker()
        await self._update_schedules_on_startup(scheduled_tasks_for_creation)
    except Exception as error:
        raise exceptions.DatabaseConnectionError(str(error)) from error

shutdown async

shutdown()

Close the connection pool if it was created by this schedule source.

Source code in src/taskiq_pg/aiopg/schedule_source.py
async def shutdown(self) -> None:
    """Close the connection pool if it was created by this schedule source."""
    if self._owns_pool and getattr(self, "_database_pool", None) is not None:
        self._database_pool.close()

get_schedules async

get_schedules()

Fetch schedules from the database.

Source code in src/taskiq_pg/aiopg/schedule_source.py
async def get_schedules(self) -> list[ScheduledTask]:
    """Fetch schedules from the database."""
    async with self._database_pool.acquire() as connection, connection.cursor() as cursor:
        await cursor.execute(
            SELECT_SCHEDULES_QUERY.format(self._table_name),
        )
        schedules, rows = [], await cursor.fetchall()
    for schedule_id, task_name, schedule in rows:
        schedules.append(
            ScheduledTask.model_validate(
                {
                    "schedule_id": str(schedule_id),
                    "task_name": task_name,
                    "labels": schedule["labels"],
                    "args": schedule["args"],
                    "kwargs": schedule["kwargs"],
                    "cron": schedule["cron"],
                    "cron_offset": schedule["cron_offset"],
                    "time": schedule["time"],
                    "interval": schedule["interval"],
                },
            ),
        )
    return schedules

add_schedule async

add_schedule(schedule)

Add a new schedule.

Parameters:

  • schedule (ScheduledTask) –

    schedule to add.

Source code in src/taskiq_pg/aiopg/schedule_source.py
async def add_schedule(self, schedule: ScheduledTask) -> None:
    """
    Add a new schedule.

    Args:
        schedule: schedule to add.
    """
    async with self._database_pool.acquire() as connection, connection.cursor() as cursor:
        await cursor.execute(
            INSERT_SCHEDULE_QUERY.format(self._table_name),
            [
                schedule.schedule_id,
                schedule.task_name,
                schedule.model_dump_json(
                    exclude={"schedule_id", "task_name"},
                ),
            ],
        )

delete_schedule async

delete_schedule(schedule_id)

Method to delete schedule by id.

This is useful for schedule cancelation.

Parameters:

  • schedule_id (str) –

    id of schedule to delete.

Source code in src/taskiq_pg/aiopg/schedule_source.py
async def delete_schedule(self, schedule_id: str) -> None:
    """
    Method to delete schedule by id.

    This is useful for schedule cancelation.

    Args:
        schedule_id: id of schedule to delete.
    """
    async with self._database_pool.acquire() as connection, connection.cursor() as cursor:
        await cursor.execute(
            DELETE_SCHEDULE_QUERY.format(self._table_name),
            [schedule_id],
        )

post_send async

post_send(task)

Delete a task after it's completed.

Source code in src/taskiq_pg/aiopg/schedule_source.py
async def post_send(self, task: ScheduledTask) -> None:
    """Delete a task after it's completed."""
    if task.time is not None:
        await self.delete_schedule(task.schedule_id)

broker

logger module-attribute

logger = logging.getLogger('taskiq.aiopg_broker')

AiopgBroker

AiopgBroker(
    dsn: str | Callable[[], str] = ...,
    result_backend: AsyncResultBackend[_T] | None = ...,
    task_id_generator: Callable[[], str] | None = ...,
    channel_name: str = ...,
    table_name: str = ...,
    max_retry_attempts: int = ...,
    read_kwargs: dict[str, Any] | None = ...,
    write_kwargs: dict[str, Any] | None = ...,
    *,
    write_pool: None = ...,
    read_connection: None = ...,
    listen_keepalive_interval: float | None = ...,
    listen_keepalive_timeout: float = ...,
    poll_interval: float = ...,
    processing_timeout: float | None = ...,
)
AiopgBroker(
    dsn: str | Callable[[], str] = ...,
    result_backend: AsyncResultBackend[_T] | None = ...,
    task_id_generator: Callable[[], str] | None = ...,
    channel_name: str = ...,
    table_name: str = ...,
    max_retry_attempts: int = ...,
    read_kwargs: dict[str, Any] | None = ...,
    write_kwargs: dict[str, Any] | None = ...,
    *,
    write_pool: Pool,
    read_connection: None = ...,
    listen_keepalive_interval: float | None = ...,
    listen_keepalive_timeout: float = ...,
    poll_interval: float = ...,
    processing_timeout: float | None = ...,
)
AiopgBroker(
    dsn: str | Callable[[], str] = ...,
    result_backend: AsyncResultBackend[_T] | None = ...,
    task_id_generator: Callable[[], str] | None = ...,
    channel_name: str = ...,
    table_name: str = ...,
    max_retry_attempts: int = ...,
    read_kwargs: dict[str, Any] | None = ...,
    write_kwargs: dict[str, Any] | None = ...,
    *,
    write_pool: None = ...,
    read_connection: Connection,
    listen_keepalive_interval: float | None = ...,
    listen_keepalive_timeout: float = ...,
    poll_interval: float = ...,
    processing_timeout: float | None = ...,
)
AiopgBroker(
    dsn: str | Callable[[], str] = ...,
    result_backend: AsyncResultBackend[_T] | None = ...,
    task_id_generator: Callable[[], str] | None = ...,
    channel_name: str = ...,
    table_name: str = ...,
    max_retry_attempts: int = ...,
    read_kwargs: dict[str, Any] | None = ...,
    write_kwargs: dict[str, Any] | None = ...,
    *,
    write_pool: Pool,
    read_connection: Connection,
    listen_keepalive_interval: float | None = ...,
    listen_keepalive_timeout: float = ...,
    poll_interval: float = ...,
    processing_timeout: float | None = ...,
)
AiopgBroker(
    dsn="postgresql://postgres:postgres@localhost:5432/postgres",
    result_backend=None,
    task_id_generator=None,
    channel_name="taskiq",
    table_name="taskiq_messages",
    max_retry_attempts=5,
    read_kwargs=None,
    write_kwargs=None,
    *,
    write_pool=None,
    read_connection=None,
    listen_keepalive_interval=30.0,
    listen_keepalive_timeout=5.0,
    poll_interval=5.0,
    processing_timeout=None,
)

Bases: BasePostgresBroker

Broker that uses aiopg as driver and PostgreSQL with LISTEN/NOTIFY mechanism.

Construct a new AiopgBroker.

Parameters:

  • dsn (str | Callable[[], str], default: 'postgresql://postgres:postgres@localhost:5432/postgres' ) –

    PostgreSQL connection string or a callable returning one.

  • result_backend (AsyncResultBackend[_T] | None, default: None ) –

    Custom result backend.

  • task_id_generator (Callable[[], str] | None, default: None ) –

    Custom task_id generator.

  • channel_name (str, default: 'taskiq' ) –

    Name of the LISTEN/NOTIFY channel.

  • table_name (str, default: 'taskiq_messages' ) –

    Name of the table used to store messages.

  • max_retry_attempts (int, default: 5 ) –

    Maximum number of message processing attempts.

  • read_kwargs (dict[str, Any] | None, default: None ) –

    Extra kwargs forwarded to aiopg.connect().

  • write_kwargs (dict[str, Any] | None, default: None ) –

    Extra kwargs forwarded to aiopg.create_pool().

  • write_pool (Pool | None, default: None ) –

    An existing connection pool to reuse for writes.

  • read_connection (Connection | None, default: None ) –

    An existing connection to reuse for LISTEN. Its lifecycle, including reconnection, remains the caller's responsibility.

  • listen_keepalive_interval (float | None, default: 30.0 ) –

    How often, in seconds, to check the broker-owned LISTEN connection with a query; None disables the check.

  • listen_keepalive_timeout (float, default: 5.0 ) –

    How long, in seconds, to wait for the check before reconnecting.

  • poll_interval (float, default: 5.0 ) –

    How often, in seconds, to look for pending messages whose NOTIFY was missed.

  • processing_timeout (float | None, default: None ) –

    Seconds after which an unacknowledged claimed message is delivered again (at-least-once delivery); None keeps at-most-once delivery.

Source code in src/taskiq_pg/aiopg/broker.py
def __init__(  # noqa: PLR0913
    self,
    dsn: str | Callable[[], str] = "postgresql://postgres:postgres@localhost:5432/postgres",
    result_backend: AsyncResultBackend[_T] | None = None,
    task_id_generator: Callable[[], str] | None = None,
    channel_name: str = "taskiq",
    table_name: str = "taskiq_messages",
    max_retry_attempts: int = 5,
    read_kwargs: dict[str, tp.Any] | None = None,
    write_kwargs: dict[str, tp.Any] | None = None,
    *,
    write_pool: aiopg.Pool | None = None,
    read_connection: aiopg.Connection | None = None,
    listen_keepalive_interval: float | None = 30.0,
    listen_keepalive_timeout: float = 5.0,
    poll_interval: float = 5.0,
    processing_timeout: float | None = None,
) -> None:
    """
    Construct a new AiopgBroker.

    Args:
        dsn: PostgreSQL connection string or a callable returning one.
        result_backend: Custom result backend.
        task_id_generator: Custom task_id generator.
        channel_name: Name of the LISTEN/NOTIFY channel.
        table_name: Name of the table used to store messages.
        max_retry_attempts: Maximum number of message processing attempts.
        read_kwargs: Extra kwargs forwarded to `aiopg.connect()`.
        write_kwargs: Extra kwargs forwarded to `aiopg.create_pool()`.
        write_pool: An existing connection pool to reuse for writes.
        read_connection: An existing connection to reuse for LISTEN.
            Its lifecycle, including reconnection, remains the caller's responsibility.
        listen_keepalive_interval: How often, in seconds, to check the broker-owned LISTEN connection
            with a query; None disables the check.
        listen_keepalive_timeout: How long, in seconds, to wait for the check before reconnecting.
        poll_interval: How often, in seconds, to look for pending messages whose NOTIFY was missed.
        processing_timeout: Seconds after which an unacknowledged claimed message is delivered again
            (at-least-once delivery); None keeps at-most-once delivery.
    """
    super().__init__(
        dsn=dsn,
        result_backend=result_backend,
        task_id_generator=task_id_generator,
        channel_name=channel_name,
        table_name=table_name,
        max_retry_attempts=max_retry_attempts,
        read_kwargs=read_kwargs,
        write_kwargs=write_kwargs,
        listen_keepalive_interval=listen_keepalive_interval,
        listen_keepalive_timeout=listen_keepalive_timeout,
        poll_interval=poll_interval,
        processing_timeout=processing_timeout,
    )

    self._owns_write_pool = True
    if write_pool is not None:
        self._write_pool = write_pool
        self._owns_write_pool = False

    self._owns_read_conn = True
    if read_connection is not None:
        self._read_conn = read_connection
        self._owns_read_conn = False

    self._notify_tasks = set()
dsn property
dsn

Get the DSN string.

channel_name instance-attribute
channel_name = channel_name
table_name instance-attribute
table_name = table_name
read_kwargs instance-attribute
read_kwargs = read_kwargs or {}
write_kwargs instance-attribute
write_kwargs = write_kwargs or {}
max_retry_attempts instance-attribute
max_retry_attempts = max_retry_attempts
listen_keepalive_interval instance-attribute
listen_keepalive_interval = listen_keepalive_interval
listen_keepalive_timeout instance-attribute
listen_keepalive_timeout = listen_keepalive_timeout
poll_interval instance-attribute
poll_interval = poll_interval
processing_timeout instance-attribute
processing_timeout = processing_timeout
startup async
startup()

Initialize the broker.

Source code in src/taskiq_pg/aiopg/broker.py
async def startup(self) -> None:
    """Initialize the broker."""
    await super().startup()
    self._queue = asyncio.Queue()
    if self._write_pool is None:
        self._write_pool = await aiopg.create_pool(self.dsn, **self.write_kwargs)
    async with self._write_pool.acquire() as conn, conn.cursor() as cursor:
        # aiopg connections are always in autocommit mode, so the transaction is explicit.
        await cursor.execute("BEGIN")
        try:
            await cursor.execute(schema_lock_query(self.table_name))
            await cursor.execute(
                SCHEMA_STATUS_QUERY.format(
                    table="%(table)s",
                    pending_index="%(pending_index)s",
                    processing_index="%(processing_index)s",
                ),
                {
                    "table": self.table_name,
                    "pending_index": f"{self.table_name}_pending_idx",
                    "processing_index": f"{self.table_name}_processing_idx",
                },
            )
            has_table, has_delivery_columns, has_pending_index, has_processing_index = await cursor.fetchone()
            if not has_table:
                await cursor.execute(CREATE_MESSAGE_TABLE_QUERY.format(self.table_name))
            if not has_delivery_columns:
                await cursor.execute(ADD_DELIVERY_COLUMNS_QUERY.format(table=self.table_name))
            if not has_pending_index:
                await cursor.execute(
                    CREATE_PENDING_INDEX_QUERY.format(
                        index=f"{self.table_name}_pending_idx",
                        table=self.table_name,
                    ),
                )
            if self.processing_timeout is not None and not has_processing_index:
                await cursor.execute(
                    CREATE_PROCESSING_INDEX_QUERY.format(
                        index=f"{self.table_name}_processing_idx",
                        table=self.table_name,
                    ),
                )
        except BaseException:
            await cursor.execute("ROLLBACK")
            raise
        await cursor.execute("COMMIT")
    if self._owns_read_conn:
        self._read_conn = await self._connect_listener()
    elif self._read_conn is not None:
        await self._execute(self._read_conn, f"LISTEN {self.channel_name}")
    self._listener_task = asyncio.create_task(self._consume_notifications())
shutdown async
shutdown()

Close all connections on shutdown.

Source code in src/taskiq_pg/aiopg/broker.py
async def shutdown(self) -> None:
    """Close all connections on shutdown."""
    if self._listener_task is not None:
        await cancel_and_wait(self._listener_task)
        self._listener_task = None
    for task in list(self._notify_tasks):
        await cancel_and_wait(task)
    await super().shutdown()
    if self._read_conn is not None:
        if self._owns_read_conn:
            await self._read_conn.close()
            self._read_conn = None
        elif not self._read_conn.closed:
            await self._execute(self._read_conn, f"UNLISTEN {self.channel_name}")
    if self._write_pool is not None and self._owns_write_pool:
        self._write_pool.close()
        await self._write_pool.wait_closed()
        self._write_pool = None
kick async
kick(message)

Send message to the channel.

Inserts the message into the database and sends a NOTIFY.

:param message: Message to send.

Source code in src/taskiq_pg/aiopg/broker.py
async def kick(self, message: BrokerMessage) -> None:
    """
    Send message to the channel.

    Inserts the message into the database and sends a NOTIFY.

    :param message: Message to send.
    """
    if self._write_pool is None:
        msg = "Please run startup before kicking."
        raise ValueError(msg)

    # Parse the delay before inserting, so a malformed label does not leave a message behind.
    delay_value = message.labels.get("delay")
    delay_seconds = None if delay_value is None else int(delay_value)

    async with self._write_pool.acquire() as conn, conn.cursor() as cursor:
        await cursor.execute(
            INSERT_MESSAGE_QUERY.format(self.table_name),
            (message.task_id, message.task_name, message.message.decode(), json.dumps(message.labels)),
        )
        row = await cursor.fetchone()
        if row is None:
            msg = "failed to insert message"
            raise RuntimeError(msg)
        message_id = int(row[0])

        if delay_seconds is None:
            # Send a NOTIFY with the message ID as payload
            await cursor.execute(f"NOTIFY {self.channel_name}, %s", (str(message_id),))
            return

    task = asyncio.create_task(self._schedule_notification(message_id, delay_seconds))
    self._notify_tasks.add(task)
    task.add_done_callback(self._notify_tasks.discard)
listen async
listen()

Listen to the channel.

Yields messages as they are received.

:yields: AckableMessage instances.

Source code in src/taskiq_pg/aiopg/broker.py
async def listen(self) -> AsyncGenerator[AckableMessage, None]:
    """
    Listen to the channel.

    Yields messages as they are received.

    :yields: AckableMessage instances.
    """
    if self._write_pool is None:
        msg = "Call startup before starting listening."
        raise ValueError(msg)
    if self._queue is None:
        msg = "Startup did not initialize the queue."
        raise ValueError(msg)

    queue = self._queue
    loop = asyncio.get_running_loop()
    next_poll = loop.time()  # poll right away: messages may have been kicked while nobody listened
    while True:
        try:
            if loop.time() >= next_poll:
                next_poll = loop.time() + self.poll_interval
                await self._release_stale_messages()
                while (
                    claimed := await self._claim(CLAIM_NEXT_READY_MESSAGE_QUERY.format(table=self.table_name))
                ) is not None:
                    yield self._ackable(claimed)
                continue
            try:
                payload = await asyncio.wait_for(queue.get(), next_poll - loop.time())
            except TimeoutError:
                continue
            if payload == POLL_NOW:
                next_poll = loop.time()
                continue
            claimed = await self._claim(CLAIM_MESSAGE_QUERY.format(self.table_name), (int(payload),))
            if claimed is not None:
                yield self._ackable(claimed)
        except Exception:
            logger.exception("Error processing message")
            continue

queries

CREATE_TABLE_QUERY module-attribute

CREATE_TABLE_QUERY = "\nCREATE TABLE IF NOT EXISTS {} (\n    task_id {} UNIQUE,\n    result BYTEA,\n    progress BYTEA\n)\n"

ADD_PROGRESS_COLUMN_QUERY module-attribute

ADD_PROGRESS_COLUMN_QUERY = "\nALTER TABLE {} ADD COLUMN IF NOT EXISTS progress BYTEA;\n"

CREATE_INDEX_QUERY module-attribute

CREATE_INDEX_QUERY = "\nCREATE INDEX IF NOT EXISTS {}_task_id_idx ON {} USING HASH (task_id)\n"

INSERT_RESULT_QUERY module-attribute

INSERT_RESULT_QUERY = "\nINSERT INTO {} VALUES (%s, %s, NULL)\nON CONFLICT (task_id)\nDO UPDATE\nSET result = %s\n"

INSERT_PROGRESS_QUERY module-attribute

INSERT_PROGRESS_QUERY = "\nINSERT INTO {} VALUES (%s, NULL, %s)\nON CONFLICT (task_id)\nDO UPDATE\nSET progress = %s\n"

SELECT_PROGRESS_QUERY module-attribute

SELECT_PROGRESS_QUERY = (
    "\nSELECT progress FROM {} WHERE task_id = %s\n"
)

IS_RESULT_EXISTS_QUERY module-attribute

IS_RESULT_EXISTS_QUERY = "\nSELECT EXISTS(\n    SELECT 1 FROM {} WHERE task_id = %s and result IS NOT NULL\n)\n"

SELECT_RESULT_QUERY module-attribute

SELECT_RESULT_QUERY = (
    "\nSELECT result FROM {} WHERE task_id = %s\n"
)

DELETE_RESULT_QUERY module-attribute

DELETE_RESULT_QUERY = (
    "\nDELETE FROM {} WHERE task_id = %s\n"
)

CREATE_SCHEDULES_TABLE_QUERY module-attribute

CREATE_SCHEDULES_TABLE_QUERY = "\nCREATE TABLE IF NOT EXISTS {} (\n    id UUID PRIMARY KEY,\n    task_name VARCHAR(100) NOT NULL,\n    schedule JSONB NOT NULL,\n    created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),\n    updated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW()\n);\n"

INSERT_SCHEDULE_QUERY module-attribute

INSERT_SCHEDULE_QUERY = "\nINSERT INTO {} (id, task_name, schedule)\nVALUES (%s, %s, %s)\nON CONFLICT (id) DO UPDATE\nSET task_name = EXCLUDED.task_name,\n    schedule = EXCLUDED.schedule,\n    updated_at = NOW();\n"

SELECT_SCHEDULES_QUERY module-attribute

SELECT_SCHEDULES_QUERY = (
    "\nSELECT id, task_name, schedule\nFROM {};\n"
)

DELETE_ALL_SCHEDULES_QUERY module-attribute

DELETE_ALL_SCHEDULES_QUERY = '\nDELETE FROM {};\n'

DELETE_SCHEDULE_QUERY module-attribute

DELETE_SCHEDULE_QUERY = '\nDELETE FROM {} WHERE id = %s;\n'

CREATE_MESSAGE_TABLE_QUERY module-attribute

CREATE_MESSAGE_TABLE_QUERY = "\nCREATE TABLE IF NOT EXISTS {} (\n    id SERIAL PRIMARY KEY,\n    task_id VARCHAR NOT NULL,\n    task_name VARCHAR NOT NULL,\n    message TEXT NOT NULL,\n    labels JSONB NOT NULL,\n    status TEXT NOT NULL DEFAULT 'pending',\n    created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW()\n);\n"

INSERT_MESSAGE_QUERY module-attribute

INSERT_MESSAGE_QUERY = "\nINSERT INTO {} (task_id, task_name, message, labels)\nVALUES (%s, %s, %s, %s)\nRETURNING id\n"

CLAIM_MESSAGE_QUERY module-attribute

CLAIM_MESSAGE_QUERY = "\nUPDATE {} SET status = 'processing', claimed_at = now(), attempts = attempts + 1\nWHERE id = %s AND status = 'pending'\nRETURNING id, message, attempts\n"

DELETE_MESSAGE_QUERY module-attribute

DELETE_MESSAGE_QUERY = (
    "DELETE FROM {} WHERE id = %s AND attempts = %s"
)

CREATE_PENDING_INDEX_QUERY module-attribute

CREATE_PENDING_INDEX_QUERY = "CREATE INDEX IF NOT EXISTS {index} ON {table} (id) WHERE status = 'pending'"

CLAIM_NEXT_READY_MESSAGE_QUERY module-attribute

CLAIM_NEXT_READY_MESSAGE_QUERY = "\nUPDATE {table} SET status = 'processing', claimed_at = now(), attempts = attempts + 1\nWHERE id = (\n    SELECT id FROM {table}\n    WHERE status = 'pending'\n        AND created_at + make_interval(secs => CASE\n            -- a label that is not a plain number must not break the query for every other row\n            WHEN labels->>'delay' ~ '^[0-9]{{1,10}}(\\.[0-9]+)?$' THEN (labels->>'delay')::double precision\n            WHEN labels->>'delay' ~ '^[0-9]+(\\.[0-9]+)?$' THEN 1e10  -- over 300 years: never ready\n            ELSE 0\n        END) <= now()\n    ORDER BY id\n    LIMIT 1\n    FOR UPDATE SKIP LOCKED\n)\nRETURNING id, message, attempts\n"

ADD_DELIVERY_COLUMNS_QUERY module-attribute

ADD_DELIVERY_COLUMNS_QUERY = "\nALTER TABLE {table}\n    ADD COLUMN IF NOT EXISTS claimed_at TIMESTAMP WITH TIME ZONE,\n    ADD COLUMN IF NOT EXISTS attempts INTEGER NOT NULL DEFAULT 0\n"

CREATE_PROCESSING_INDEX_QUERY module-attribute

CREATE_PROCESSING_INDEX_QUERY = "CREATE INDEX IF NOT EXISTS {index} ON {table} (claimed_at) WHERE status = 'processing'"

RELEASE_STALE_MESSAGES_QUERY module-attribute

RELEASE_STALE_MESSAGES_QUERY = "\nUPDATE {table} SET status = 'pending'\nWHERE status = 'processing'\n    AND COALESCE(claimed_at, created_at) < now() - make_interval(secs => %s)\n    AND attempts < %s\n"

FAIL_EXHAUSTED_MESSAGES_QUERY module-attribute

FAIL_EXHAUSTED_MESSAGES_QUERY = "\nUPDATE {table} SET status = 'failed'\nWHERE status = 'processing'\n    AND COALESCE(claimed_at, created_at) < now() - make_interval(secs => %s)\n    AND attempts >= %s\nRETURNING id, task_id, task_name, attempts\n"

result_backend

AiopgResultBackend

AiopgResultBackend(
    dsn: Callable[[], str] | str | None = ...,
    keep_results: bool = ...,
    table_name: str = ...,
    field_for_task_id: Literal[
        "VarChar", "Text", "Uuid"
    ] = ...,
    serializer: TaskiqSerializer | None = ...,
    *,
    pool: None = ...,
    **connect_kwargs: Any,
)
AiopgResultBackend(
    dsn: Callable[[], str] | str | None = ...,
    keep_results: bool = ...,
    table_name: str = ...,
    field_for_task_id: Literal[
        "VarChar", "Text", "Uuid"
    ] = ...,
    serializer: TaskiqSerializer | None = ...,
    *,
    pool: Pool,
)
AiopgResultBackend(
    dsn="postgres://postgres:postgres@localhost:5432/postgres",
    keep_results=True,
    table_name="taskiq_results",
    field_for_task_id="VarChar",
    serializer=None,
    *,
    pool=None,
    **connect_kwargs,
)

Bases: BasePostgresResultBackend

Result backend for TaskIQ based on Aiopg.

Construct a new AiopgResultBackend.

Parameters:

  • dsn (Callable[[], str] | str | None, default: 'postgres://postgres:postgres@localhost:5432/postgres' ) –

    PostgreSQL connection string or callable. Can be None if pool is provided.

  • keep_results (bool, default: True ) –

    Whether to keep results after reading.

  • table_name (str, default: 'taskiq_results' ) –

    Table to store results in.

  • field_for_task_id (Literal['VarChar', 'Text', 'Uuid'], default: 'VarChar' ) –

    Column type for task_id.

  • serializer (TaskiqSerializer | None, default: None ) –

    Serializer for task results.

  • pool (Pool | None, default: None ) –

    An existing connection pool to reuse.

  • **connect_kwargs (Any, default: {} ) –

    Extra kwargs for connection pool creation.

Source code in src/taskiq_pg/aiopg/result_backend.py
def __init__(
    self,
    dsn: tp.Callable[[], str] | str | None = "postgres://postgres:postgres@localhost:5432/postgres",
    keep_results: bool = True,
    table_name: str = "taskiq_results",
    field_for_task_id: tp.Literal["VarChar", "Text", "Uuid"] = "VarChar",
    serializer: TaskiqSerializer | None = None,
    *,
    pool: Pool | None = None,
    **connect_kwargs: tp.Any,
) -> None:
    """
    Construct a new AiopgResultBackend.

    Args:
        dsn: PostgreSQL connection string or callable. Can be None if pool is provided.
        keep_results: Whether to keep results after reading.
        table_name: Table to store results in.
        field_for_task_id: Column type for task_id.
        serializer: Serializer for task results.
        pool: An existing connection pool to reuse.
        **connect_kwargs: Extra kwargs for connection pool creation.
    """
    self._owns_pool = True
    if pool is not None:
        self._owns_pool = False
        self._database_pool = pool

    super().__init__(
        dsn=dsn,
        keep_results=keep_results,
        table_name=table_name,
        field_for_task_id=field_for_task_id,
        serializer=serializer,
        **connect_kwargs,
    )
dsn property
dsn

Get the DSN string.

keep_results instance-attribute
keep_results = keep_results
table_name instance-attribute
table_name = table_name
field_for_task_id instance-attribute
field_for_task_id = field_for_task_id
connect_kwargs instance-attribute
connect_kwargs = connect_kwargs
serializer instance-attribute
serializer = serializer or PickleSerializer()
startup async
startup()

Initialize the result backend.

Construct new connection pool (if not provided externally) and create new table for results if not exists.

Source code in src/taskiq_pg/aiopg/result_backend.py
async def startup(self) -> None:
    """
    Initialize the result backend.

    Construct new connection pool (if not provided externally) and create new table for results if not exists.
    """
    try:
        if self._owns_pool:
            self._database_pool = await create_pool(
                self.dsn,
                **self.connect_kwargs,
            )

        async with self._database_pool.acquire() as connection, connection.cursor() as cursor:
            await cursor.execute(
                queries.CREATE_TABLE_QUERY.format(
                    self.table_name,
                    self.field_for_task_id,
                ),
            )
            await cursor.execute(
                queries.ADD_PROGRESS_COLUMN_QUERY.format(
                    self.table_name,
                    self.field_for_task_id,
                ),
            )
            await cursor.execute(
                queries.CREATE_INDEX_QUERY.format(
                    self.table_name,
                    self.table_name,
                ),
            )
    except Exception as error:
        raise exceptions.DatabaseConnectionError(str(error)) from error
shutdown async
shutdown()

Close the connection pool if created by this backend.

Source code in src/taskiq_pg/aiopg/result_backend.py
async def shutdown(self) -> None:
    """Close the connection pool if created by this backend."""
    if self._owns_pool and getattr(self, "_database_pool", None) is not None:
        self._database_pool.close()
set_result async
set_result(task_id, result)

Set result to the PostgreSQL table.

Parameters:

  • task_id (Any) –

    ID of the task.

  • result (TaskiqResult[_ReturnType]) –

    result of the task.

Source code in src/taskiq_pg/aiopg/result_backend.py
async def set_result(
    self,
    task_id: tp.Any,
    result: TaskiqResult[ReturnType],
) -> None:
    """
    Set result to the PostgreSQL table.

    Args:
        task_id (Any): ID of the task.
        result (TaskiqResult[_ReturnType]):  result of the task.

    """
    dumped_result = self.serializer.dumpb(result)
    async with self._database_pool.acquire() as connection, connection.cursor() as cursor:
        await cursor.execute(
            queries.INSERT_RESULT_QUERY.format(
                self.table_name,
            ),
            (
                task_id,
                dumped_result,
                dumped_result,
            ),
        )
is_result_ready async
is_result_ready(task_id)

Return whether the result is ready.

Parameters:

  • task_id (Any) –

    ID of the task.

Returns:

  • bool ( bool ) –

    True if the result is ready else False.

Source code in src/taskiq_pg/aiopg/result_backend.py
async def is_result_ready(
    self,
    task_id: tp.Any,
) -> bool:
    """
    Return whether the result is ready.

    Args:
        task_id (Any): ID of the task.

    Returns:
        bool: True if the result is ready else False.

    """
    async with self._database_pool.acquire() as connection, connection.cursor() as cursor:
        await cursor.execute(
            queries.IS_RESULT_EXISTS_QUERY.format(
                self.table_name,
            ),
            (task_id,),
        )
        result = await cursor.fetchone()
        return bool(result[0]) if result else False
get_result async
get_result(task_id, with_logs=False)

Retrieve result from the task.

:param task_id: task's id. :param with_logs: if True it will download task's logs. :raises ResultIsMissingError: if there is no result when trying to get it. :return: TaskiqResult.

Source code in src/taskiq_pg/aiopg/result_backend.py
async def get_result(
    self,
    task_id: tp.Any,
    with_logs: bool = False,
) -> TaskiqResult[ReturnType]:
    """
    Retrieve result from the task.

    :param task_id: task's id.
    :param with_logs: if True it will download task's logs.
    :raises ResultIsMissingError: if there is no result when trying to get it.
    :return: TaskiqResult.
    """
    async with self._database_pool.acquire() as connection, connection.cursor() as cursor:
        await cursor.execute(
            queries.SELECT_RESULT_QUERY.format(
                self.table_name,
            ),
            (task_id,),
        )
        result = await cursor.fetchone()

        if not result:
            msg = f"Cannot find record with task_id = {task_id} in PostgreSQL"
            raise exceptions.ResultIsMissingError(
                msg,
            )

        result_in_bytes: bytes = result[0]

        if not self.keep_results:
            await cursor.execute(
                queries.DELETE_RESULT_QUERY.format(
                    self.table_name,
                ),
                (task_id,),
            )

        taskiq_result: TaskiqResult[ReturnType] = self.serializer.loadb(
            result_in_bytes,
        )

        if not with_logs:
            taskiq_result.log = None

        return taskiq_result
set_progress async
set_progress(task_id, progress)

Saves progress.

:param task_id: task's id. :param progress: progress of execution.

Source code in src/taskiq_pg/aiopg/result_backend.py
async def set_progress(
    self,
    task_id: str,
    progress: TaskProgress[tp.Any],
) -> None:
    """
    Saves progress.

    :param task_id: task's id.
    :param progress: progress of execution.
    """
    dumped_progress = self.serializer.dumpb(progress)
    async with self._database_pool.acquire() as connection, connection.cursor() as cursor:
        await cursor.execute(
            queries.INSERT_PROGRESS_QUERY.format(
                self.table_name,
            ),
            (
                task_id,
                dumped_progress,
                dumped_progress,
            ),
        )
get_progress async
get_progress(task_id)

Gets progress.

:param task_id: task's id.

Source code in src/taskiq_pg/aiopg/result_backend.py
async def get_progress(
    self,
    task_id: str,
) -> TaskProgress[tp.Any] | None:
    """
    Gets progress.

    :param task_id: task's id.
    """
    async with self._database_pool.acquire() as connection, connection.cursor() as cursor:
        await cursor.execute(
            queries.SELECT_PROGRESS_QUERY.format(
                self.table_name,
            ),
            (task_id,),
        )
        progress = await cursor.fetchone()
        if not progress or progress[0] is None:
            return None
        progress_in_bytes: bytes = progress[0]
        taskiq_progress: TaskProgress[tp.Any] = self.serializer.loadb(progress_in_bytes)
        return taskiq_progress

schedule_source

logger module-attribute

logger = getLogger('taskiq_pg.aiopg_schedule_source')

AiopgScheduleSource

AiopgScheduleSource(
    broker: AsyncBroker,
    dsn: str | Callable[[], str] = ...,
    table_name: str = ...,
    *,
    pool: None = ...,
    **connect_kwargs: Any,
)
AiopgScheduleSource(
    broker: AsyncBroker,
    dsn: str | Callable[[], str] = ...,
    table_name: str = ...,
    *,
    pool: Pool,
)
AiopgScheduleSource(
    broker,
    dsn="postgresql://postgres:postgres@localhost:5432/postgres",
    table_name="taskiq_schedules",
    *,
    pool=None,
    **connect_kwargs,
)

Bases: BasePostgresScheduleSource

Schedule source that uses aiopg to store schedules in PostgreSQL.

Construct a new AiopgScheduleSource.

Parameters:

  • broker (AsyncBroker) –

    The TaskIQ broker instance.

  • dsn (str | Callable[[], str], default: 'postgresql://postgres:postgres@localhost:5432/postgres' ) –

    PostgreSQL connection string or callable. Ignored in pool mode.

  • table_name (str, default: 'taskiq_schedules' ) –

    Table to store schedules in.

  • pool (Pool | None, default: None ) –

    An existing connection pool to reuse.

  • **connect_kwargs (Any, default: {} ) –

    Extra kwargs for connection pool creation.

Source code in src/taskiq_pg/aiopg/schedule_source.py
def __init__(
    self,
    broker: AsyncBroker,
    dsn: str | tp.Callable[[], str] = "postgresql://postgres:postgres@localhost:5432/postgres",
    table_name: str = "taskiq_schedules",
    *,
    pool: Pool | None = None,
    **connect_kwargs: tp.Any,
) -> None:
    """
    Construct a new AiopgScheduleSource.

    Args:
        broker: The TaskIQ broker instance.
        dsn: PostgreSQL connection string or callable. Ignored in pool mode.
        table_name: Table to store schedules in.
        pool: An existing connection pool to reuse.
        **connect_kwargs: Extra kwargs for connection pool creation.
    """
    self._owns_pool = True
    if pool is not None:
        self._owns_pool = False
        self._database_pool = pool

    super().__init__(
        broker=broker,
        dsn=dsn,
        table_name=table_name,
        **connect_kwargs,
    )
dsn property
dsn

Get the DSN string.

startup async
startup()

Initialize the schedule source.

Construct new connection pool (if not provided externally), create new table for schedules if not exists and fill table with schedules from task labels.

Source code in src/taskiq_pg/aiopg/schedule_source.py
async def startup(self) -> None:
    """
    Initialize the schedule source.

    Construct new connection pool (if not provided externally), create new table for schedules if not exists
    and fill table with schedules from task labels.
    """
    try:
        if self._owns_pool:
            self._database_pool = await create_pool(
                dsn=self.dsn,
                **self._connect_kwargs,
            )
        async with self._database_pool.acquire() as connection, connection.cursor() as cursor:
            await cursor.execute(CREATE_SCHEDULES_TABLE_QUERY.format(self._table_name))
        scheduled_tasks_for_creation = self.extract_scheduled_tasks_from_broker()
        await self._update_schedules_on_startup(scheduled_tasks_for_creation)
    except Exception as error:
        raise exceptions.DatabaseConnectionError(str(error)) from error
shutdown async
shutdown()

Close the connection pool if it was created by this schedule source.

Source code in src/taskiq_pg/aiopg/schedule_source.py
async def shutdown(self) -> None:
    """Close the connection pool if it was created by this schedule source."""
    if self._owns_pool and getattr(self, "_database_pool", None) is not None:
        self._database_pool.close()
get_schedules async
get_schedules()

Fetch schedules from the database.

Source code in src/taskiq_pg/aiopg/schedule_source.py
async def get_schedules(self) -> list[ScheduledTask]:
    """Fetch schedules from the database."""
    async with self._database_pool.acquire() as connection, connection.cursor() as cursor:
        await cursor.execute(
            SELECT_SCHEDULES_QUERY.format(self._table_name),
        )
        schedules, rows = [], await cursor.fetchall()
    for schedule_id, task_name, schedule in rows:
        schedules.append(
            ScheduledTask.model_validate(
                {
                    "schedule_id": str(schedule_id),
                    "task_name": task_name,
                    "labels": schedule["labels"],
                    "args": schedule["args"],
                    "kwargs": schedule["kwargs"],
                    "cron": schedule["cron"],
                    "cron_offset": schedule["cron_offset"],
                    "time": schedule["time"],
                    "interval": schedule["interval"],
                },
            ),
        )
    return schedules
add_schedule async
add_schedule(schedule)

Add a new schedule.

Parameters:

  • schedule (ScheduledTask) –

    schedule to add.

Source code in src/taskiq_pg/aiopg/schedule_source.py
async def add_schedule(self, schedule: ScheduledTask) -> None:
    """
    Add a new schedule.

    Args:
        schedule: schedule to add.
    """
    async with self._database_pool.acquire() as connection, connection.cursor() as cursor:
        await cursor.execute(
            INSERT_SCHEDULE_QUERY.format(self._table_name),
            [
                schedule.schedule_id,
                schedule.task_name,
                schedule.model_dump_json(
                    exclude={"schedule_id", "task_name"},
                ),
            ],
        )
delete_schedule async
delete_schedule(schedule_id)

Method to delete schedule by id.

This is useful for schedule cancelation.

Parameters:

  • schedule_id (str) –

    id of schedule to delete.

Source code in src/taskiq_pg/aiopg/schedule_source.py
async def delete_schedule(self, schedule_id: str) -> None:
    """
    Method to delete schedule by id.

    This is useful for schedule cancelation.

    Args:
        schedule_id: id of schedule to delete.
    """
    async with self._database_pool.acquire() as connection, connection.cursor() as cursor:
        await cursor.execute(
            DELETE_SCHEDULE_QUERY.format(self._table_name),
            [schedule_id],
        )
post_send async
post_send(task)

Delete a task after it's completed.

Source code in src/taskiq_pg/aiopg/schedule_source.py
async def post_send(self, task: ScheduledTask) -> None:
    """Delete a task after it's completed."""
    if task.time is not None:
        await self.delete_schedule(task.schedule_id)
extract_scheduled_tasks_from_broker
extract_scheduled_tasks_from_broker()

Extract schedules from tasks that were registered in broker.

Returns:

  • list[ScheduledTask] –

    A list of ScheduledTask instances extracted from the task's labels.

Source code in src/taskiq_pg/_internal/schedule_source.py
def extract_scheduled_tasks_from_broker(self) -> list[ScheduledTask]:
    """
    Extract schedules from tasks that were registered in broker.

    Returns:
        A list of ScheduledTask instances extracted from the task's labels.
    """
    scheduled_tasks_for_creation: list[ScheduledTask] = []
    for task_name, task in self._broker.get_all_tasks().items():
        if "schedule" not in task.labels:
            logger.debug("Task %s has no schedule, skipping", task_name)
            continue
        if not isinstance(task.labels["schedule"], list):
            logger.warning(
                "Schedule for task %s is not a list, skipping",
                task_name,
            )
            continue
        for schedule in task.labels["schedule"]:
            try:
                new_schedule = ScheduledTask.model_validate(
                    {
                        "task_name": task_name,
                        "labels": schedule.get("labels", {}),
                        "args": schedule.get("args", []),
                        "kwargs": schedule.get("kwargs", {}),
                        "schedule_id": str(uuid.uuid4()),
                        "cron": schedule.get("cron", None),
                        "cron_offset": schedule.get("cron_offset", None),
                        "interval": schedule.get("interval", None),
                        "time": schedule.get("time", None),
                    },
                )
                scheduled_tasks_for_creation.append(new_schedule)
            except ValidationError:  # noqa: PERF203
                logger.exception(
                    "Schedule for task %s is not valid, skipping",
                    task_name,
                )
                continue
    return scheduled_tasks_for_creation

asyncpg

AsyncpgBroker

AsyncpgBroker(
    dsn: str | Callable[[], str] = ...,
    result_backend: AsyncResultBackend[_T] | None = ...,
    task_id_generator: Callable[[], str] | None = ...,
    channel_name: str = ...,
    table_name: str = ...,
    max_retry_attempts: int = ...,
    read_kwargs: dict[str, Any] | None = ...,
    write_kwargs: dict[str, Any] | None = ...,
    *,
    write_pool: None = ...,
    read_connection: None = ...,
    listen_keepalive_interval: float | None = ...,
    listen_keepalive_timeout: float = ...,
    poll_interval: float = ...,
    processing_timeout: float | None = ...,
)
AsyncpgBroker(
    dsn: str | Callable[[], str] = ...,
    result_backend: AsyncResultBackend[_T] | None = ...,
    task_id_generator: Callable[[], str] | None = ...,
    channel_name: str = ...,
    table_name: str = ...,
    max_retry_attempts: int = ...,
    read_kwargs: dict[str, Any] | None = ...,
    write_kwargs: dict[str, Any] | None = ...,
    *,
    write_pool: Pool,
    read_connection: None = ...,
    listen_keepalive_interval: float | None = ...,
    listen_keepalive_timeout: float = ...,
    poll_interval: float = ...,
    processing_timeout: float | None = ...,
)
AsyncpgBroker(
    dsn: str | Callable[[], str] = ...,
    result_backend: AsyncResultBackend[_T] | None = ...,
    task_id_generator: Callable[[], str] | None = ...,
    channel_name: str = ...,
    table_name: str = ...,
    max_retry_attempts: int = ...,
    read_kwargs: dict[str, Any] | None = ...,
    write_kwargs: dict[str, Any] | None = ...,
    *,
    write_pool: None = ...,
    read_connection: Connection,
    listen_keepalive_interval: float | None = ...,
    listen_keepalive_timeout: float = ...,
    poll_interval: float = ...,
    processing_timeout: float | None = ...,
)
AsyncpgBroker(
    dsn: str | Callable[[], str] = ...,
    result_backend: AsyncResultBackend[_T] | None = ...,
    task_id_generator: Callable[[], str] | None = ...,
    channel_name: str = ...,
    table_name: str = ...,
    max_retry_attempts: int = ...,
    read_kwargs: dict[str, Any] | None = ...,
    write_kwargs: dict[str, Any] | None = ...,
    *,
    write_pool: Pool,
    read_connection: Connection,
    listen_keepalive_interval: float | None = ...,
    listen_keepalive_timeout: float = ...,
    poll_interval: float = ...,
    processing_timeout: float | None = ...,
)
AsyncpgBroker(
    dsn="postgresql://postgres:postgres@localhost:5432/postgres",
    result_backend=None,
    task_id_generator=None,
    channel_name="taskiq",
    table_name="taskiq_messages",
    max_retry_attempts=5,
    read_kwargs=None,
    write_kwargs=None,
    *,
    write_pool=None,
    read_connection=None,
    listen_keepalive_interval=30.0,
    listen_keepalive_timeout=5.0,
    poll_interval=5.0,
    processing_timeout=None,
)

Bases: BasePostgresBroker

Broker that uses asyncpg as driver and PostgreSQL with LISTEN/NOTIFY mechanism.

Construct a new AsyncpgBroker.

Parameters:

  • dsn (str | Callable[[], str], default: 'postgresql://postgres:postgres@localhost:5432/postgres' ) –

    PostgreSQL connection string or a callable returning one.

  • result_backend (AsyncResultBackend[_T] | None, default: None ) –

    Custom result backend.

  • task_id_generator (Callable[[], str] | None, default: None ) –

    Custom task_id generator.

  • channel_name (str, default: 'taskiq' ) –

    Name of the LISTEN/NOTIFY channel.

  • table_name (str, default: 'taskiq_messages' ) –

    Name of the table used to store messages.

  • max_retry_attempts (int, default: 5 ) –

    Maximum number of message processing attempts.

  • read_kwargs (dict[str, Any] | None, default: None ) –

    Extra kwargs forwarded to asyncpg.connect().

  • write_kwargs (dict[str, Any] | None, default: None ) –

    Extra kwargs forwarded to asyncpg.create_pool().

  • write_pool (Pool | None, default: None ) –

    An existing connection pool to reuse for writes.

  • read_connection (Connection | None, default: None ) –

    An existing connection to reuse for LISTEN. Its lifecycle, including reconnection, remains the caller's responsibility.

  • listen_keepalive_interval (float | None, default: 30.0 ) –

    How often, in seconds, to check the broker-owned LISTEN connection with a query; None disables the check.

  • listen_keepalive_timeout (float, default: 5.0 ) –

    How long, in seconds, to wait for the check before reconnecting.

  • poll_interval (float, default: 5.0 ) –

    How often, in seconds, to look for pending messages whose NOTIFY was missed.

  • processing_timeout (float | None, default: None ) –

    Seconds after which an unacknowledged claimed message is delivered again (at-least-once delivery); None keeps at-most-once delivery.

Source code in src/taskiq_pg/asyncpg/broker.py
def __init__(  # noqa: PLR0913
    self,
    dsn: str | Callable[[], str] = "postgresql://postgres:postgres@localhost:5432/postgres",
    result_backend: AsyncResultBackend[_T] | None = None,
    task_id_generator: Callable[[], str] | None = None,
    channel_name: str = "taskiq",
    table_name: str = "taskiq_messages",
    max_retry_attempts: int = 5,
    read_kwargs: dict[str, tp.Any] | None = None,
    write_kwargs: dict[str, tp.Any] | None = None,
    *,
    write_pool: asyncpg.pool.Pool | None = None,
    read_connection: asyncpg.Connection | None = None,
    listen_keepalive_interval: float | None = 30.0,
    listen_keepalive_timeout: float = 5.0,
    poll_interval: float = 5.0,
    processing_timeout: float | None = None,
) -> None:
    """
    Construct a new AsyncpgBroker.

    Args:
        dsn: PostgreSQL connection string or a callable returning one.
        result_backend: Custom result backend.
        task_id_generator: Custom task_id generator.
        channel_name: Name of the LISTEN/NOTIFY channel.
        table_name: Name of the table used to store messages.
        max_retry_attempts: Maximum number of message processing attempts.
        read_kwargs: Extra kwargs forwarded to `asyncpg.connect()`.
        write_kwargs: Extra kwargs forwarded to `asyncpg.create_pool()`.
        write_pool: An existing connection pool to reuse for writes.
        read_connection: An existing connection to reuse for LISTEN.
            Its lifecycle, including reconnection, remains the caller's responsibility.
        listen_keepalive_interval: How often, in seconds, to check the broker-owned LISTEN connection
            with a query; None disables the check.
        listen_keepalive_timeout: How long, in seconds, to wait for the check before reconnecting.
        poll_interval: How often, in seconds, to look for pending messages whose NOTIFY was missed.
        processing_timeout: Seconds after which an unacknowledged claimed message is delivered again
            (at-least-once delivery); None keeps at-most-once delivery.
    """
    super().__init__(
        dsn=dsn,
        result_backend=result_backend,
        task_id_generator=task_id_generator,
        channel_name=channel_name,
        table_name=table_name,
        max_retry_attempts=max_retry_attempts,
        read_kwargs=read_kwargs,
        write_kwargs=write_kwargs,
        listen_keepalive_interval=listen_keepalive_interval,
        listen_keepalive_timeout=listen_keepalive_timeout,
        poll_interval=poll_interval,
        processing_timeout=processing_timeout,
    )

    self._owns_write_pool = True
    if write_pool is not None:
        self._write_pool = write_pool
        self._owns_write_pool = False

    self._owns_read_conn = True
    if read_connection is not None:
        self._read_conn = read_connection
        self._owns_read_conn = False

dsn property

dsn

Get the DSN string.

channel_name instance-attribute

channel_name = channel_name

table_name instance-attribute

table_name = table_name

read_kwargs instance-attribute

read_kwargs = read_kwargs or {}

write_kwargs instance-attribute

write_kwargs = write_kwargs or {}

max_retry_attempts instance-attribute

max_retry_attempts = max_retry_attempts

listen_keepalive_interval instance-attribute

listen_keepalive_interval = listen_keepalive_interval

listen_keepalive_timeout instance-attribute

listen_keepalive_timeout = listen_keepalive_timeout

poll_interval instance-attribute

poll_interval = poll_interval

processing_timeout instance-attribute

processing_timeout = processing_timeout

startup async

startup()

Initialize the broker.

Source code in src/taskiq_pg/asyncpg/broker.py
async def startup(self) -> None:
    """Initialize the broker."""
    await super().startup()
    self._queue = asyncio.Queue()
    self._listener_disconnected = asyncio.Event()
    if self._write_pool is None:
        self._write_pool = await asyncpg.create_pool(self.dsn, **self.write_kwargs)
    async with self._write_pool.acquire() as conn, conn.transaction():
        await conn.execute(schema_lock_query(self.table_name))
        status = await conn.fetchrow(
            SCHEMA_STATUS_QUERY.format(table="$1", pending_index="$2", processing_index="$3"),
            self.table_name,
            f"{self.table_name}_pending_idx",
            f"{self.table_name}_processing_idx",
        )
        if status is None:
            msg = "schema status query returned no row"
            raise RuntimeError(msg)
        if not status["has_table"]:
            await conn.execute(CREATE_MESSAGE_TABLE_QUERY.format(self.table_name))
        if not status["has_delivery_columns"]:
            await conn.execute(ADD_DELIVERY_COLUMNS_QUERY.format(table=self.table_name))
        if not status["has_pending_index"]:
            await conn.execute(
                CREATE_PENDING_INDEX_QUERY.format(index=f"{self.table_name}_pending_idx", table=self.table_name),
            )
        if self.processing_timeout is not None and not status["has_processing_index"]:
            await conn.execute(
                CREATE_PROCESSING_INDEX_QUERY.format(
                    index=f"{self.table_name}_processing_idx",
                    table=self.table_name,
                ),
            )
    await self._connect_listener()
    self._listener_task = asyncio.create_task(self._restore_listener())

shutdown async

shutdown()

Close all connections on shutdown.

Source code in src/taskiq_pg/asyncpg/broker.py
async def shutdown(self) -> None:
    """Close all connections on shutdown."""
    if self._listener_task is not None:
        await cancel_and_wait(self._listener_task)
        self._listener_task = None
    await super().shutdown()
    if self._read_conn is not None:
        self._read_conn.remove_termination_listener(self._connection_terminated)
        if not self._read_conn.is_closed():
            await self._read_conn.remove_listener(self.channel_name, self._notification_handler)
        if self._owns_read_conn:
            await self._read_conn.close()
            self._read_conn = None
    if self._write_pool is not None and self._owns_write_pool:
        await self._write_pool.close()
        self._write_pool = None

kick async

kick(message)

Send message to the channel.

Inserts the message into the database and sends a NOTIFY.

:param message: Message to send.

Source code in src/taskiq_pg/asyncpg/broker.py
async def kick(self, message: BrokerMessage) -> None:
    """
    Send message to the channel.

    Inserts the message into the database and sends a NOTIFY.

    :param message: Message to send.
    """
    if self._write_pool is None:
        msg = "Please run startup before kicking."
        raise ValueError(msg)

    # Parse the delay before inserting, so a malformed label does not leave a message behind.
    delay_value = message.labels.get("delay")
    delay_seconds = None if delay_value is None else int(delay_value)

    async with self._write_pool.acquire() as conn:
        # Insert the message into the database
        message_inserted_id = tp.cast(
            "int",
            await conn.fetchval(
                INSERT_MESSAGE_QUERY.format(self.table_name),
                message.task_id,
                message.task_name,
                message.message.decode(),
                json.dumps(message.labels),
            ),
        )

        if delay_seconds is not None:
            _ = asyncio.create_task(  # noqa: RUF006
                self._schedule_notification(message_inserted_id, delay_seconds),
            )
        else:
            # Send a NOTIFY with the message ID as payload
            _ = await conn.execute(
                f"NOTIFY {self.channel_name}, '{message_inserted_id}'",
            )

listen async

listen()

Listen to the channel.

Yields messages as they are received.

:yields: AckableMessage instances.

Source code in src/taskiq_pg/asyncpg/broker.py
async def listen(self) -> AsyncGenerator[AckableMessage, None]:
    """
    Listen to the channel.

    Yields messages as they are received.

    :yields: AckableMessage instances.
    """
    if self._write_pool is None:
        msg = "Call startup before starting listening."
        raise ValueError(msg)
    if self._queue is None:
        msg = "Startup did not initialize the queue."
        raise ValueError(msg)

    queue = self._queue
    loop = asyncio.get_running_loop()
    next_poll = loop.time()  # poll right away: messages may have been kicked while nobody listened
    while True:
        try:
            if loop.time() >= next_poll:
                next_poll = loop.time() + self.poll_interval
                await self._release_stale_messages()
                while (claimed := await self._claim_next_ready()) is not None:
                    yield self._ackable(claimed)
                continue
            try:
                payload = await asyncio.wait_for(queue.get(), next_poll - loop.time())
            except TimeoutError:
                continue
            if payload == POLL_NOW:
                next_poll = loop.time()
                continue
            claimed = await self._claim(int(payload))
            if claimed is not None:
                yield self._ackable(claimed)
        except Exception:
            logger.exception("Error processing message")
            continue

AsyncpgResultBackend

AsyncpgResultBackend(
    dsn: Callable[[], str] | str | None = ...,
    keep_results: bool = ...,
    table_name: str = ...,
    field_for_task_id: Literal[
        "VarChar", "Text", "Uuid"
    ] = ...,
    serializer: TaskiqSerializer | None = ...,
    *,
    pool: None = ...,
    **connect_kwargs: Any,
)
AsyncpgResultBackend(
    dsn: Callable[[], str] | str | None = ...,
    keep_results: bool = ...,
    table_name: str = ...,
    field_for_task_id: Literal[
        "VarChar", "Text", "Uuid"
    ] = ...,
    serializer: TaskiqSerializer | None = ...,
    *,
    pool: Pool[Any],
)
AsyncpgResultBackend(
    dsn="postgres://postgres:postgres@localhost:5432/postgres",
    keep_results=True,
    table_name="taskiq_results",
    field_for_task_id="VarChar",
    serializer=None,
    *,
    pool=None,
    **connect_kwargs,
)

Bases: BasePostgresResultBackend

Result backend for TaskIQ based on asyncpg.

Construct a new AsyncpgResultBackend.

Parameters:

  • dsn (Callable[[], str] | str | None, default: 'postgres://postgres:postgres@localhost:5432/postgres' ) –

    PostgreSQL connection string or callable. Can be None if pool is provided.

  • keep_results (bool, default: True ) –

    Whether to keep results after reading.

  • table_name (str, default: 'taskiq_results' ) –

    Table to store results in.

  • field_for_task_id (Literal['VarChar', 'Text', 'Uuid'], default: 'VarChar' ) –

    Column type for task_id.

  • serializer (TaskiqSerializer | None, default: None ) –

    Serializer for task results.

  • pool (Pool[Any] | None, default: None ) –

    An existing connection pool to reuse.

  • **connect_kwargs (Any, default: {} ) –

    Extra kwargs for connection pool creation.

Source code in src/taskiq_pg/asyncpg/result_backend.py
def __init__(
    self,
    dsn: tp.Callable[[], str] | str | None = "postgres://postgres:postgres@localhost:5432/postgres",
    keep_results: bool = True,
    table_name: str = "taskiq_results",
    field_for_task_id: tp.Literal["VarChar", "Text", "Uuid"] = "VarChar",
    serializer: TaskiqSerializer | None = None,
    *,
    pool: "asyncpg.Pool[tp.Any] | None" = None,
    **connect_kwargs: tp.Any,
) -> None:
    """
    Construct a new AsyncpgResultBackend.

    Args:
        dsn: PostgreSQL connection string or callable. Can be None if pool is provided.
        keep_results: Whether to keep results after reading.
        table_name: Table to store results in.
        field_for_task_id: Column type for task_id.
        serializer: Serializer for task results.
        pool: An existing connection pool to reuse.
        **connect_kwargs: Extra kwargs for connection pool creation.
    """
    self._owns_pool = True
    if pool is not None:
        self._owns_pool = False
        self._database_pool = pool

    super().__init__(
        dsn=dsn,
        keep_results=keep_results,
        table_name=table_name,
        field_for_task_id=field_for_task_id,
        serializer=serializer,
        **connect_kwargs,
    )

dsn property

dsn

Get the DSN string.

keep_results instance-attribute

keep_results = keep_results

table_name instance-attribute

table_name = table_name

field_for_task_id instance-attribute

field_for_task_id = field_for_task_id

connect_kwargs instance-attribute

connect_kwargs = connect_kwargs

serializer instance-attribute

serializer = serializer or PickleSerializer()

startup async

startup()

Initialize the result backend.

Construct new connection pool (if not provided externally) and create new table for results if not exists.

Source code in src/taskiq_pg/asyncpg/result_backend.py
async def startup(self) -> None:
    """
    Initialize the result backend.

    Construct new connection pool (if not provided externally) and create new table
    for results if not exists.
    """
    if self._owns_pool:
        _database_pool = await asyncpg.create_pool(
            dsn=self.dsn,
            **self.connect_kwargs,
        )
        self._database_pool = _database_pool

    await self._database_pool.execute(
        queries.CREATE_TABLE_QUERY.format(
            self.table_name,
            self.field_for_task_id,
        ),
    )
    await self._database_pool.execute(
        queries.ADD_PROGRESS_COLUMN_QUERY.format(
            self.table_name,
        ),
    )
    await self._database_pool.execute(
        queries.CREATE_INDEX_QUERY.format(
            self.table_name,
            self.table_name,
        ),
    )

shutdown async

shutdown()

Close the connection pool if it was created by this backend.

Source code in src/taskiq_pg/asyncpg/result_backend.py
async def shutdown(self) -> None:
    """Close the connection pool if it was created by this backend."""
    if self._owns_pool and getattr(self, "_database_pool", None) is not None:
        await self._database_pool.close()

set_result async

set_result(task_id, result)

Set result to the PostgreSQL table.

:param task_id: ID of the task. :param result: result of the task.

Source code in src/taskiq_pg/asyncpg/result_backend.py
async def set_result(
    self,
    task_id: str,
    result: TaskiqResult[ReturnType],
) -> None:
    """
    Set result to the PostgreSQL table.

    :param task_id: ID of the task.
    :param result: result of the task.
    """
    _ = await self._database_pool.execute(
        queries.INSERT_RESULT_QUERY.format(
            self.table_name,
        ),
        task_id,
        self.serializer.dumpb(result.model_dump(mode="json")),
    )

is_result_ready async

is_result_ready(task_id)

Returns whether the result is ready.

:param task_id: ID of the task. :returns: True if the result is ready else False.

Source code in src/taskiq_pg/asyncpg/result_backend.py
async def is_result_ready(self, task_id: str) -> bool:
    """
    Returns whether the result is ready.

    :param task_id: ID of the task.
    :returns: True if the result is ready else False.
    """
    return tp.cast(
        "bool",
        await self._database_pool.fetchval(
            queries.IS_RESULT_EXISTS_QUERY.format(
                self.table_name,
            ),
            task_id,
        ),
    )

get_result async

get_result(task_id, with_logs=False)

Retrieve result from the task.

:param task_id: task's id. :param with_logs: if True it will download task's logs. (deprecated in taskiq) :raises ResultIsMissingError: if there is no result when trying to get it. :return: TaskiqResult.

Source code in src/taskiq_pg/asyncpg/result_backend.py
async def get_result(
    self,
    task_id: str,
    with_logs: bool = False,
) -> TaskiqResult[ReturnType]:
    """
    Retrieve result from the task.

    :param task_id: task's id.
    :param with_logs: if True it will download task's logs. (deprecated in taskiq)
    :raises ResultIsMissingError: if there is no result when trying to get it.
    :return: TaskiqResult.
    """
    result_in_bytes = tp.cast(
        "bytes",
        await self._database_pool.fetchval(
            queries.SELECT_RESULT_QUERY.format(
                self.table_name,
            ),
            task_id,
        ),
    )
    if not self.keep_results:
        await self._database_pool.execute(
            queries.DELETE_RESULT_QUERY.format(
                self.table_name,
            ),
            task_id,
        )
    taskiq_result: tp.Final = TaskiqResult[ReturnType].model_validate(
        self.serializer.loadb(result_in_bytes),
    )
    if not with_logs:
        taskiq_result.log = None
    return taskiq_result

set_progress async

set_progress(task_id, progress)

Saves progress.

:param task_id: task's id. :param progress: progress of execution.

Source code in src/taskiq_pg/asyncpg/result_backend.py
async def set_progress(
    self,
    task_id: str,
    progress: TaskProgress[tp.Any],
) -> None:
    """
    Saves progress.

    :param task_id: task's id.
    :param progress: progress of execution.
    """
    await self._database_pool.execute(
        queries.INSERT_PROGRESS_QUERY.format(
            self.table_name,
        ),
        task_id,
        self.serializer.dumpb(progress.model_dump(mode="json")),
    )

get_progress async

get_progress(task_id)

Gets progress.

:param task_id: task's id.

Source code in src/taskiq_pg/asyncpg/result_backend.py
async def get_progress(
    self,
    task_id: str,
) -> TaskProgress[tp.Any] | None:
    """
    Gets progress.

    :param task_id: task's id.
    """
    progress_in_bytes = await self._database_pool.fetchval(
        queries.SELECT_PROGRESS_QUERY.format(
            self.table_name,
        ),
        task_id,
    )
    if progress_in_bytes is None:
        return None
    return TaskProgress[tp.Any].model_validate(
        self.serializer.loadb(progress_in_bytes),
    )

AsyncpgScheduleSource

AsyncpgScheduleSource(
    broker: AsyncBroker,
    dsn: str | Callable[[], str] = ...,
    table_name: str = ...,
    *,
    pool: None = ...,
    **connect_kwargs: Any,
)
AsyncpgScheduleSource(
    broker: AsyncBroker,
    dsn: str | Callable[[], str] = ...,
    table_name: str = ...,
    *,
    pool: Pool[Record],
)
AsyncpgScheduleSource(
    broker,
    dsn="postgresql://postgres:postgres@localhost:5432/postgres",
    table_name="taskiq_schedules",
    *,
    pool=None,
    **connect_kwargs,
)

Bases: BasePostgresScheduleSource

Schedule source that uses asyncpg to store schedules in PostgreSQL.

Construct a new AsyncpgScheduleSource.

Parameters:

  • broker (AsyncBroker) –

    The TaskIQ broker instance.

  • dsn (str | Callable[[], str], default: 'postgresql://postgres:postgres@localhost:5432/postgres' ) –

    PostgreSQL connection string or callable. Ignored in pool mode.

  • table_name (str, default: 'taskiq_schedules' ) –

    Table to store schedules in.

  • pool (Pool[Record] | None, default: None ) –

    An existing connection pool to reuse.

  • **connect_kwargs (Any, default: {} ) –

    Extra kwargs for connection pool creation.

Source code in src/taskiq_pg/asyncpg/schedule_source.py
def __init__(
    self,
    broker: AsyncBroker,
    dsn: str | tp.Callable[[], str] = "postgresql://postgres:postgres@localhost:5432/postgres",
    table_name: str = "taskiq_schedules",
    *,
    pool: "asyncpg.Pool[asyncpg.Record] | None" = None,
    **connect_kwargs: tp.Any,
) -> None:
    """
    Construct a new AsyncpgScheduleSource.

    Args:
        broker: The TaskIQ broker instance.
        dsn: PostgreSQL connection string or callable. Ignored in pool mode.
        table_name: Table to store schedules in.
        pool: An existing connection pool to reuse.
        **connect_kwargs: Extra kwargs for connection pool creation.
    """
    self._owns_pool = True
    if pool is not None:
        self._owns_pool = False
        self._database_pool = pool

    super().__init__(
        broker=broker,
        dsn=dsn,
        table_name=table_name,
        **connect_kwargs,
    )

dsn property

dsn

Get the DSN string.

extract_scheduled_tasks_from_broker

extract_scheduled_tasks_from_broker()

Extract schedules from tasks that were registered in broker.

Returns:

  • list[ScheduledTask] –

    A list of ScheduledTask instances extracted from the task's labels.

Source code in src/taskiq_pg/_internal/schedule_source.py
def extract_scheduled_tasks_from_broker(self) -> list[ScheduledTask]:
    """
    Extract schedules from tasks that were registered in broker.

    Returns:
        A list of ScheduledTask instances extracted from the task's labels.
    """
    scheduled_tasks_for_creation: list[ScheduledTask] = []
    for task_name, task in self._broker.get_all_tasks().items():
        if "schedule" not in task.labels:
            logger.debug("Task %s has no schedule, skipping", task_name)
            continue
        if not isinstance(task.labels["schedule"], list):
            logger.warning(
                "Schedule for task %s is not a list, skipping",
                task_name,
            )
            continue
        for schedule in task.labels["schedule"]:
            try:
                new_schedule = ScheduledTask.model_validate(
                    {
                        "task_name": task_name,
                        "labels": schedule.get("labels", {}),
                        "args": schedule.get("args", []),
                        "kwargs": schedule.get("kwargs", {}),
                        "schedule_id": str(uuid.uuid4()),
                        "cron": schedule.get("cron", None),
                        "cron_offset": schedule.get("cron_offset", None),
                        "interval": schedule.get("interval", None),
                        "time": schedule.get("time", None),
                    },
                )
                scheduled_tasks_for_creation.append(new_schedule)
            except ValidationError:  # noqa: PERF203
                logger.exception(
                    "Schedule for task %s is not valid, skipping",
                    task_name,
                )
                continue
    return scheduled_tasks_for_creation

startup async

startup()

Initialize the schedule source.

Construct new connection pool (if not provided externally), create new table for schedules if not exists and fill table with schedules from task labels.

Source code in src/taskiq_pg/asyncpg/schedule_source.py
async def startup(self) -> None:
    """
    Initialize the schedule source.

    Construct new connection pool (if not provided externally), create new table for schedules if not exists
    and fill table with schedules from task labels.
    """
    if self._owns_pool:
        self._database_pool = await asyncpg.create_pool(
            dsn=self.dsn,
            **self._connect_kwargs,
        )
    await self._database_pool.execute(
        CREATE_SCHEDULES_TABLE_QUERY.format(
            self._table_name,
        ),
    )
    scheduled_tasks_for_creation = self.extract_scheduled_tasks_from_broker()
    await self._update_schedules_on_startup(scheduled_tasks_for_creation)

shutdown async

shutdown()

Close the connection pool if it created by this schedule source.

Source code in src/taskiq_pg/asyncpg/schedule_source.py
async def shutdown(self) -> None:
    """Close the connection pool if it created by this schedule source."""
    if self._owns_pool and getattr(self, "_database_pool", None) is not None:
        await self._database_pool.close()

get_schedules async

get_schedules()

Fetch schedules from the database.

Source code in src/taskiq_pg/asyncpg/schedule_source.py
async def get_schedules(self) -> list["ScheduledTask"]:
    """Fetch schedules from the database."""
    async with self._database_pool.acquire() as conn:
        rows_with_schedules = await conn.fetch(
            SELECT_SCHEDULES_QUERY.format(self._table_name),
        )
    schedules = []
    for row in rows_with_schedules:
        schedule = json.loads(row["schedule"])
        schedules.append(
            ScheduledTask.model_validate(
                {
                    "schedule_id": str(row["id"]),
                    "task_name": row["task_name"],
                    "labels": schedule["labels"],
                    "args": schedule["args"],
                    "kwargs": schedule["kwargs"],
                    "cron": schedule["cron"],
                    "cron_offset": schedule["cron_offset"],
                    "time": schedule["time"],
                    "interval": schedule["interval"],
                },
            ),
        )
    return schedules

add_schedule async

add_schedule(schedule)

Add a new schedule.

Parameters:

  • schedule (ScheduledTask) –

    schedule to add.

Source code in src/taskiq_pg/asyncpg/schedule_source.py
async def add_schedule(self, schedule: "ScheduledTask") -> None:
    """
    Add a new schedule.

    Args:
        schedule: schedule to add.
    """
    await self._database_pool.execute(
        INSERT_SCHEDULE_QUERY.format(self._table_name),
        str(schedule.schedule_id),
        schedule.task_name,
        schedule.model_dump_json(
            exclude={"schedule_id", "task_name"},
        ),
    )

delete_schedule async

delete_schedule(schedule_id)

Method to delete schedule by id.

This is useful for schedule cancelation.

Parameters:

  • schedule_id (str) –

    id of schedule to delete.

Source code in src/taskiq_pg/asyncpg/schedule_source.py
async def delete_schedule(self, schedule_id: str) -> None:
    """
    Method to delete schedule by id.

    This is useful for schedule cancelation.

    Args:
        schedule_id: id of schedule to delete.
    """
    await self._database_pool.execute(
        DELETE_SCHEDULE_QUERY.format(self._table_name),
        schedule_id,
    )

post_send async

post_send(task)

Delete a task after it's completed.

Source code in src/taskiq_pg/asyncpg/schedule_source.py
async def post_send(self, task: ScheduledTask) -> None:
    """Delete a task after it's completed."""
    if task.time is not None:
        await self.delete_schedule(task.schedule_id)

broker

logger module-attribute

logger = logging.getLogger('taskiq.asyncpg_broker')

AsyncpgBroker

AsyncpgBroker(
    dsn: str | Callable[[], str] = ...,
    result_backend: AsyncResultBackend[_T] | None = ...,
    task_id_generator: Callable[[], str] | None = ...,
    channel_name: str = ...,
    table_name: str = ...,
    max_retry_attempts: int = ...,
    read_kwargs: dict[str, Any] | None = ...,
    write_kwargs: dict[str, Any] | None = ...,
    *,
    write_pool: None = ...,
    read_connection: None = ...,
    listen_keepalive_interval: float | None = ...,
    listen_keepalive_timeout: float = ...,
    poll_interval: float = ...,
    processing_timeout: float | None = ...,
)
AsyncpgBroker(
    dsn: str | Callable[[], str] = ...,
    result_backend: AsyncResultBackend[_T] | None = ...,
    task_id_generator: Callable[[], str] | None = ...,
    channel_name: str = ...,
    table_name: str = ...,
    max_retry_attempts: int = ...,
    read_kwargs: dict[str, Any] | None = ...,
    write_kwargs: dict[str, Any] | None = ...,
    *,
    write_pool: Pool,
    read_connection: None = ...,
    listen_keepalive_interval: float | None = ...,
    listen_keepalive_timeout: float = ...,
    poll_interval: float = ...,
    processing_timeout: float | None = ...,
)
AsyncpgBroker(
    dsn: str | Callable[[], str] = ...,
    result_backend: AsyncResultBackend[_T] | None = ...,
    task_id_generator: Callable[[], str] | None = ...,
    channel_name: str = ...,
    table_name: str = ...,
    max_retry_attempts: int = ...,
    read_kwargs: dict[str, Any] | None = ...,
    write_kwargs: dict[str, Any] | None = ...,
    *,
    write_pool: None = ...,
    read_connection: Connection,
    listen_keepalive_interval: float | None = ...,
    listen_keepalive_timeout: float = ...,
    poll_interval: float = ...,
    processing_timeout: float | None = ...,
)
AsyncpgBroker(
    dsn: str | Callable[[], str] = ...,
    result_backend: AsyncResultBackend[_T] | None = ...,
    task_id_generator: Callable[[], str] | None = ...,
    channel_name: str = ...,
    table_name: str = ...,
    max_retry_attempts: int = ...,
    read_kwargs: dict[str, Any] | None = ...,
    write_kwargs: dict[str, Any] | None = ...,
    *,
    write_pool: Pool,
    read_connection: Connection,
    listen_keepalive_interval: float | None = ...,
    listen_keepalive_timeout: float = ...,
    poll_interval: float = ...,
    processing_timeout: float | None = ...,
)
AsyncpgBroker(
    dsn="postgresql://postgres:postgres@localhost:5432/postgres",
    result_backend=None,
    task_id_generator=None,
    channel_name="taskiq",
    table_name="taskiq_messages",
    max_retry_attempts=5,
    read_kwargs=None,
    write_kwargs=None,
    *,
    write_pool=None,
    read_connection=None,
    listen_keepalive_interval=30.0,
    listen_keepalive_timeout=5.0,
    poll_interval=5.0,
    processing_timeout=None,
)

Bases: BasePostgresBroker

Broker that uses asyncpg as driver and PostgreSQL with LISTEN/NOTIFY mechanism.

Construct a new AsyncpgBroker.

Parameters:

  • dsn (str | Callable[[], str], default: 'postgresql://postgres:postgres@localhost:5432/postgres' ) –

    PostgreSQL connection string or a callable returning one.

  • result_backend (AsyncResultBackend[_T] | None, default: None ) –

    Custom result backend.

  • task_id_generator (Callable[[], str] | None, default: None ) –

    Custom task_id generator.

  • channel_name (str, default: 'taskiq' ) –

    Name of the LISTEN/NOTIFY channel.

  • table_name (str, default: 'taskiq_messages' ) –

    Name of the table used to store messages.

  • max_retry_attempts (int, default: 5 ) –

    Maximum number of message processing attempts.

  • read_kwargs (dict[str, Any] | None, default: None ) –

    Extra kwargs forwarded to asyncpg.connect().

  • write_kwargs (dict[str, Any] | None, default: None ) –

    Extra kwargs forwarded to asyncpg.create_pool().

  • write_pool (Pool | None, default: None ) –

    An existing connection pool to reuse for writes.

  • read_connection (Connection | None, default: None ) –

    An existing connection to reuse for LISTEN. Its lifecycle, including reconnection, remains the caller's responsibility.

  • listen_keepalive_interval (float | None, default: 30.0 ) –

    How often, in seconds, to check the broker-owned LISTEN connection with a query; None disables the check.

  • listen_keepalive_timeout (float, default: 5.0 ) –

    How long, in seconds, to wait for the check before reconnecting.

  • poll_interval (float, default: 5.0 ) –

    How often, in seconds, to look for pending messages whose NOTIFY was missed.

  • processing_timeout (float | None, default: None ) –

    Seconds after which an unacknowledged claimed message is delivered again (at-least-once delivery); None keeps at-most-once delivery.

Source code in src/taskiq_pg/asyncpg/broker.py
def __init__(  # noqa: PLR0913
    self,
    dsn: str | Callable[[], str] = "postgresql://postgres:postgres@localhost:5432/postgres",
    result_backend: AsyncResultBackend[_T] | None = None,
    task_id_generator: Callable[[], str] | None = None,
    channel_name: str = "taskiq",
    table_name: str = "taskiq_messages",
    max_retry_attempts: int = 5,
    read_kwargs: dict[str, tp.Any] | None = None,
    write_kwargs: dict[str, tp.Any] | None = None,
    *,
    write_pool: asyncpg.pool.Pool | None = None,
    read_connection: asyncpg.Connection | None = None,
    listen_keepalive_interval: float | None = 30.0,
    listen_keepalive_timeout: float = 5.0,
    poll_interval: float = 5.0,
    processing_timeout: float | None = None,
) -> None:
    """
    Construct a new AsyncpgBroker.

    Args:
        dsn: PostgreSQL connection string or a callable returning one.
        result_backend: Custom result backend.
        task_id_generator: Custom task_id generator.
        channel_name: Name of the LISTEN/NOTIFY channel.
        table_name: Name of the table used to store messages.
        max_retry_attempts: Maximum number of message processing attempts.
        read_kwargs: Extra kwargs forwarded to `asyncpg.connect()`.
        write_kwargs: Extra kwargs forwarded to `asyncpg.create_pool()`.
        write_pool: An existing connection pool to reuse for writes.
        read_connection: An existing connection to reuse for LISTEN.
            Its lifecycle, including reconnection, remains the caller's responsibility.
        listen_keepalive_interval: How often, in seconds, to check the broker-owned LISTEN connection
            with a query; None disables the check.
        listen_keepalive_timeout: How long, in seconds, to wait for the check before reconnecting.
        poll_interval: How often, in seconds, to look for pending messages whose NOTIFY was missed.
        processing_timeout: Seconds after which an unacknowledged claimed message is delivered again
            (at-least-once delivery); None keeps at-most-once delivery.
    """
    super().__init__(
        dsn=dsn,
        result_backend=result_backend,
        task_id_generator=task_id_generator,
        channel_name=channel_name,
        table_name=table_name,
        max_retry_attempts=max_retry_attempts,
        read_kwargs=read_kwargs,
        write_kwargs=write_kwargs,
        listen_keepalive_interval=listen_keepalive_interval,
        listen_keepalive_timeout=listen_keepalive_timeout,
        poll_interval=poll_interval,
        processing_timeout=processing_timeout,
    )

    self._owns_write_pool = True
    if write_pool is not None:
        self._write_pool = write_pool
        self._owns_write_pool = False

    self._owns_read_conn = True
    if read_connection is not None:
        self._read_conn = read_connection
        self._owns_read_conn = False
dsn property
dsn

Get the DSN string.

channel_name instance-attribute
channel_name = channel_name
table_name instance-attribute
table_name = table_name
read_kwargs instance-attribute
read_kwargs = read_kwargs or {}
write_kwargs instance-attribute
write_kwargs = write_kwargs or {}
max_retry_attempts instance-attribute
max_retry_attempts = max_retry_attempts
listen_keepalive_interval instance-attribute
listen_keepalive_interval = listen_keepalive_interval
listen_keepalive_timeout instance-attribute
listen_keepalive_timeout = listen_keepalive_timeout
poll_interval instance-attribute
poll_interval = poll_interval
processing_timeout instance-attribute
processing_timeout = processing_timeout
startup async
startup()

Initialize the broker.

Source code in src/taskiq_pg/asyncpg/broker.py
async def startup(self) -> None:
    """Initialize the broker."""
    await super().startup()
    self._queue = asyncio.Queue()
    self._listener_disconnected = asyncio.Event()
    if self._write_pool is None:
        self._write_pool = await asyncpg.create_pool(self.dsn, **self.write_kwargs)
    async with self._write_pool.acquire() as conn, conn.transaction():
        await conn.execute(schema_lock_query(self.table_name))
        status = await conn.fetchrow(
            SCHEMA_STATUS_QUERY.format(table="$1", pending_index="$2", processing_index="$3"),
            self.table_name,
            f"{self.table_name}_pending_idx",
            f"{self.table_name}_processing_idx",
        )
        if status is None:
            msg = "schema status query returned no row"
            raise RuntimeError(msg)
        if not status["has_table"]:
            await conn.execute(CREATE_MESSAGE_TABLE_QUERY.format(self.table_name))
        if not status["has_delivery_columns"]:
            await conn.execute(ADD_DELIVERY_COLUMNS_QUERY.format(table=self.table_name))
        if not status["has_pending_index"]:
            await conn.execute(
                CREATE_PENDING_INDEX_QUERY.format(index=f"{self.table_name}_pending_idx", table=self.table_name),
            )
        if self.processing_timeout is not None and not status["has_processing_index"]:
            await conn.execute(
                CREATE_PROCESSING_INDEX_QUERY.format(
                    index=f"{self.table_name}_processing_idx",
                    table=self.table_name,
                ),
            )
    await self._connect_listener()
    self._listener_task = asyncio.create_task(self._restore_listener())
shutdown async
shutdown()

Close all connections on shutdown.

Source code in src/taskiq_pg/asyncpg/broker.py
async def shutdown(self) -> None:
    """Close all connections on shutdown."""
    if self._listener_task is not None:
        await cancel_and_wait(self._listener_task)
        self._listener_task = None
    await super().shutdown()
    if self._read_conn is not None:
        self._read_conn.remove_termination_listener(self._connection_terminated)
        if not self._read_conn.is_closed():
            await self._read_conn.remove_listener(self.channel_name, self._notification_handler)
        if self._owns_read_conn:
            await self._read_conn.close()
            self._read_conn = None
    if self._write_pool is not None and self._owns_write_pool:
        await self._write_pool.close()
        self._write_pool = None
kick async
kick(message)

Send message to the channel.

Inserts the message into the database and sends a NOTIFY.

:param message: Message to send.

Source code in src/taskiq_pg/asyncpg/broker.py
async def kick(self, message: BrokerMessage) -> None:
    """
    Send message to the channel.

    Inserts the message into the database and sends a NOTIFY.

    :param message: Message to send.
    """
    if self._write_pool is None:
        msg = "Please run startup before kicking."
        raise ValueError(msg)

    # Parse the delay before inserting, so a malformed label does not leave a message behind.
    delay_value = message.labels.get("delay")
    delay_seconds = None if delay_value is None else int(delay_value)

    async with self._write_pool.acquire() as conn:
        # Insert the message into the database
        message_inserted_id = tp.cast(
            "int",
            await conn.fetchval(
                INSERT_MESSAGE_QUERY.format(self.table_name),
                message.task_id,
                message.task_name,
                message.message.decode(),
                json.dumps(message.labels),
            ),
        )

        if delay_seconds is not None:
            _ = asyncio.create_task(  # noqa: RUF006
                self._schedule_notification(message_inserted_id, delay_seconds),
            )
        else:
            # Send a NOTIFY with the message ID as payload
            _ = await conn.execute(
                f"NOTIFY {self.channel_name}, '{message_inserted_id}'",
            )
listen async
listen()

Listen to the channel.

Yields messages as they are received.

:yields: AckableMessage instances.

Source code in src/taskiq_pg/asyncpg/broker.py
async def listen(self) -> AsyncGenerator[AckableMessage, None]:
    """
    Listen to the channel.

    Yields messages as they are received.

    :yields: AckableMessage instances.
    """
    if self._write_pool is None:
        msg = "Call startup before starting listening."
        raise ValueError(msg)
    if self._queue is None:
        msg = "Startup did not initialize the queue."
        raise ValueError(msg)

    queue = self._queue
    loop = asyncio.get_running_loop()
    next_poll = loop.time()  # poll right away: messages may have been kicked while nobody listened
    while True:
        try:
            if loop.time() >= next_poll:
                next_poll = loop.time() + self.poll_interval
                await self._release_stale_messages()
                while (claimed := await self._claim_next_ready()) is not None:
                    yield self._ackable(claimed)
                continue
            try:
                payload = await asyncio.wait_for(queue.get(), next_poll - loop.time())
            except TimeoutError:
                continue
            if payload == POLL_NOW:
                next_poll = loop.time()
                continue
            claimed = await self._claim(int(payload))
            if claimed is not None:
                yield self._ackable(claimed)
        except Exception:
            logger.exception("Error processing message")
            continue

queries

CREATE_TABLE_QUERY module-attribute

CREATE_TABLE_QUERY = "\nCREATE TABLE IF NOT EXISTS {} (\n    task_id {} UNIQUE,\n    result BYTEA,\n    progress BYTEA\n)\n"

ADD_PROGRESS_COLUMN_QUERY module-attribute

ADD_PROGRESS_COLUMN_QUERY = "\nALTER TABLE {} ADD COLUMN IF NOT EXISTS progress BYTEA;\n"

CREATE_INDEX_QUERY module-attribute

CREATE_INDEX_QUERY = "\nCREATE INDEX IF NOT EXISTS {}_task_id_idx ON {} USING HASH (task_id)\n"

INSERT_RESULT_QUERY module-attribute

INSERT_RESULT_QUERY = "\nINSERT INTO {} VALUES ($1, $2, NULL)\nON CONFLICT (task_id)\nDO UPDATE\nSET result = $2\n"

INSERT_PROGRESS_QUERY module-attribute

INSERT_PROGRESS_QUERY = "\nINSERT INTO {} VALUES ($1, NULL, $2)\nON CONFLICT (task_id)\nDO UPDATE\nSET progress = $2\n"

SELECT_PROGRESS_QUERY module-attribute

SELECT_PROGRESS_QUERY = (
    "\nSELECT progress FROM {} WHERE task_id = $1\n"
)

IS_RESULT_EXISTS_QUERY module-attribute

IS_RESULT_EXISTS_QUERY = "\nSELECT EXISTS(\n    SELECT 1 FROM {} WHERE task_id = $1 and result IS NOT NULL\n)\n"

SELECT_RESULT_QUERY module-attribute

SELECT_RESULT_QUERY = (
    "\nSELECT result FROM {} WHERE task_id = $1\n"
)

DELETE_RESULT_QUERY module-attribute

DELETE_RESULT_QUERY = (
    "\nDELETE FROM {} WHERE task_id = $1\n"
)

CREATE_MESSAGE_TABLE_QUERY module-attribute

CREATE_MESSAGE_TABLE_QUERY = "\nCREATE TABLE IF NOT EXISTS {} (\n    id SERIAL PRIMARY KEY,\n    task_id VARCHAR NOT NULL,\n    task_name VARCHAR NOT NULL,\n    message TEXT NOT NULL,\n    labels JSONB NOT NULL,\n    status TEXT NOT NULL DEFAULT 'pending',\n    created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW()\n);\n"

INSERT_MESSAGE_QUERY module-attribute

INSERT_MESSAGE_QUERY = "\nINSERT INTO {} (task_id, task_name, message, labels)\nVALUES ($1, $2, $3, $4)\nRETURNING id\n"

CLAIM_MESSAGE_QUERY module-attribute

CLAIM_MESSAGE_QUERY = "\nUPDATE {} SET status = 'processing', claimed_at = now(), attempts = attempts + 1\nWHERE id = $1 AND status = 'pending'\nRETURNING id, message, attempts\n"

DELETE_MESSAGE_QUERY module-attribute

DELETE_MESSAGE_QUERY = (
    "DELETE FROM {} WHERE id = $1 AND attempts = $2"
)

CREATE_SCHEDULES_TABLE_QUERY module-attribute

CREATE_SCHEDULES_TABLE_QUERY = "\nCREATE TABLE IF NOT EXISTS {} (\n    id UUID PRIMARY KEY,\n    task_name VARCHAR(100) NOT NULL,\n    schedule JSONB NOT NULL,\n    created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),\n    updated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW()\n);\n"

INSERT_SCHEDULE_QUERY module-attribute

INSERT_SCHEDULE_QUERY = "\nINSERT INTO {} (id, task_name, schedule)\nVALUES ($1, $2, $3)\nON CONFLICT (id) DO UPDATE\nSET task_name = EXCLUDED.task_name,\n    schedule = EXCLUDED.schedule,\n    updated_at = NOW();\n"

SELECT_SCHEDULES_QUERY module-attribute

SELECT_SCHEDULES_QUERY = (
    "\nSELECT id, task_name, schedule\nFROM {};\n"
)

DELETE_ALL_SCHEDULES_QUERY module-attribute

DELETE_ALL_SCHEDULES_QUERY = '\nDELETE FROM {};\n'

DELETE_SCHEDULE_QUERY module-attribute

DELETE_SCHEDULE_QUERY = '\nDELETE FROM {} WHERE id = $1;\n'

CREATE_PENDING_INDEX_QUERY module-attribute

CREATE_PENDING_INDEX_QUERY = "CREATE INDEX IF NOT EXISTS {index} ON {table} (id) WHERE status = 'pending'"

CLAIM_NEXT_READY_MESSAGE_QUERY module-attribute

CLAIM_NEXT_READY_MESSAGE_QUERY = "\nUPDATE {table} SET status = 'processing', claimed_at = now(), attempts = attempts + 1\nWHERE id = (\n    SELECT id FROM {table}\n    WHERE status = 'pending'\n        AND created_at + make_interval(secs => CASE\n            -- a label that is not a plain number must not break the query for every other row\n            WHEN labels->>'delay' ~ '^[0-9]{{1,10}}(\\.[0-9]+)?$' THEN (labels->>'delay')::double precision\n            WHEN labels->>'delay' ~ '^[0-9]+(\\.[0-9]+)?$' THEN 1e10  -- over 300 years: never ready\n            ELSE 0\n        END) <= now()\n    ORDER BY id\n    LIMIT 1\n    FOR UPDATE SKIP LOCKED\n)\nRETURNING id, message, attempts\n"

ADD_DELIVERY_COLUMNS_QUERY module-attribute

ADD_DELIVERY_COLUMNS_QUERY = "\nALTER TABLE {table}\n    ADD COLUMN IF NOT EXISTS claimed_at TIMESTAMP WITH TIME ZONE,\n    ADD COLUMN IF NOT EXISTS attempts INTEGER NOT NULL DEFAULT 0\n"

CREATE_PROCESSING_INDEX_QUERY module-attribute

CREATE_PROCESSING_INDEX_QUERY = "CREATE INDEX IF NOT EXISTS {index} ON {table} (claimed_at) WHERE status = 'processing'"

RELEASE_STALE_MESSAGES_QUERY module-attribute

RELEASE_STALE_MESSAGES_QUERY = "\nUPDATE {table} SET status = 'pending'\nWHERE status = 'processing'\n    AND COALESCE(claimed_at, created_at) < now() - make_interval(secs => $1)\n    AND attempts < $2\n"

FAIL_EXHAUSTED_MESSAGES_QUERY module-attribute

FAIL_EXHAUSTED_MESSAGES_QUERY = "\nUPDATE {table} SET status = 'failed'\nWHERE status = 'processing'\n    AND COALESCE(claimed_at, created_at) < now() - make_interval(secs => $1)\n    AND attempts >= $2\nRETURNING id, task_id, task_name, attempts\n"

result_backend

logger module-attribute

logger = logging.getLogger('taskiq.asyncpg_result_backend')

AsyncpgResultBackend

AsyncpgResultBackend(
    dsn: Callable[[], str] | str | None = ...,
    keep_results: bool = ...,
    table_name: str = ...,
    field_for_task_id: Literal[
        "VarChar", "Text", "Uuid"
    ] = ...,
    serializer: TaskiqSerializer | None = ...,
    *,
    pool: None = ...,
    **connect_kwargs: Any,
)
AsyncpgResultBackend(
    dsn: Callable[[], str] | str | None = ...,
    keep_results: bool = ...,
    table_name: str = ...,
    field_for_task_id: Literal[
        "VarChar", "Text", "Uuid"
    ] = ...,
    serializer: TaskiqSerializer | None = ...,
    *,
    pool: Pool[Any],
)
AsyncpgResultBackend(
    dsn="postgres://postgres:postgres@localhost:5432/postgres",
    keep_results=True,
    table_name="taskiq_results",
    field_for_task_id="VarChar",
    serializer=None,
    *,
    pool=None,
    **connect_kwargs,
)

Bases: BasePostgresResultBackend

Result backend for TaskIQ based on asyncpg.

Construct a new AsyncpgResultBackend.

Parameters:

  • dsn (Callable[[], str] | str | None, default: 'postgres://postgres:postgres@localhost:5432/postgres' ) –

    PostgreSQL connection string or callable. Can be None if pool is provided.

  • keep_results (bool, default: True ) –

    Whether to keep results after reading.

  • table_name (str, default: 'taskiq_results' ) –

    Table to store results in.

  • field_for_task_id (Literal['VarChar', 'Text', 'Uuid'], default: 'VarChar' ) –

    Column type for task_id.

  • serializer (TaskiqSerializer | None, default: None ) –

    Serializer for task results.

  • pool (Pool[Any] | None, default: None ) –

    An existing connection pool to reuse.

  • **connect_kwargs (Any, default: {} ) –

    Extra kwargs for connection pool creation.

Source code in src/taskiq_pg/asyncpg/result_backend.py
def __init__(
    self,
    dsn: tp.Callable[[], str] | str | None = "postgres://postgres:postgres@localhost:5432/postgres",
    keep_results: bool = True,
    table_name: str = "taskiq_results",
    field_for_task_id: tp.Literal["VarChar", "Text", "Uuid"] = "VarChar",
    serializer: TaskiqSerializer | None = None,
    *,
    pool: "asyncpg.Pool[tp.Any] | None" = None,
    **connect_kwargs: tp.Any,
) -> None:
    """
    Construct a new AsyncpgResultBackend.

    Args:
        dsn: PostgreSQL connection string or callable. Can be None if pool is provided.
        keep_results: Whether to keep results after reading.
        table_name: Table to store results in.
        field_for_task_id: Column type for task_id.
        serializer: Serializer for task results.
        pool: An existing connection pool to reuse.
        **connect_kwargs: Extra kwargs for connection pool creation.
    """
    self._owns_pool = True
    if pool is not None:
        self._owns_pool = False
        self._database_pool = pool

    super().__init__(
        dsn=dsn,
        keep_results=keep_results,
        table_name=table_name,
        field_for_task_id=field_for_task_id,
        serializer=serializer,
        **connect_kwargs,
    )
dsn property
dsn

Get the DSN string.

keep_results instance-attribute
keep_results = keep_results
table_name instance-attribute
table_name = table_name
field_for_task_id instance-attribute
field_for_task_id = field_for_task_id
connect_kwargs instance-attribute
connect_kwargs = connect_kwargs
serializer instance-attribute
serializer = serializer or PickleSerializer()
startup async
startup()

Initialize the result backend.

Construct new connection pool (if not provided externally) and create new table for results if not exists.

Source code in src/taskiq_pg/asyncpg/result_backend.py
async def startup(self) -> None:
    """
    Initialize the result backend.

    Construct new connection pool (if not provided externally) and create new table
    for results if not exists.
    """
    if self._owns_pool:
        _database_pool = await asyncpg.create_pool(
            dsn=self.dsn,
            **self.connect_kwargs,
        )
        self._database_pool = _database_pool

    await self._database_pool.execute(
        queries.CREATE_TABLE_QUERY.format(
            self.table_name,
            self.field_for_task_id,
        ),
    )
    await self._database_pool.execute(
        queries.ADD_PROGRESS_COLUMN_QUERY.format(
            self.table_name,
        ),
    )
    await self._database_pool.execute(
        queries.CREATE_INDEX_QUERY.format(
            self.table_name,
            self.table_name,
        ),
    )
shutdown async
shutdown()

Close the connection pool if it was created by this backend.

Source code in src/taskiq_pg/asyncpg/result_backend.py
async def shutdown(self) -> None:
    """Close the connection pool if it was created by this backend."""
    if self._owns_pool and getattr(self, "_database_pool", None) is not None:
        await self._database_pool.close()
set_result async
set_result(task_id, result)

Set result to the PostgreSQL table.

:param task_id: ID of the task. :param result: result of the task.

Source code in src/taskiq_pg/asyncpg/result_backend.py
async def set_result(
    self,
    task_id: str,
    result: TaskiqResult[ReturnType],
) -> None:
    """
    Set result to the PostgreSQL table.

    :param task_id: ID of the task.
    :param result: result of the task.
    """
    _ = await self._database_pool.execute(
        queries.INSERT_RESULT_QUERY.format(
            self.table_name,
        ),
        task_id,
        self.serializer.dumpb(result.model_dump(mode="json")),
    )
is_result_ready async
is_result_ready(task_id)

Returns whether the result is ready.

:param task_id: ID of the task. :returns: True if the result is ready else False.

Source code in src/taskiq_pg/asyncpg/result_backend.py
async def is_result_ready(self, task_id: str) -> bool:
    """
    Returns whether the result is ready.

    :param task_id: ID of the task.
    :returns: True if the result is ready else False.
    """
    return tp.cast(
        "bool",
        await self._database_pool.fetchval(
            queries.IS_RESULT_EXISTS_QUERY.format(
                self.table_name,
            ),
            task_id,
        ),
    )
get_result async
get_result(task_id, with_logs=False)

Retrieve result from the task.

:param task_id: task's id. :param with_logs: if True it will download task's logs. (deprecated in taskiq) :raises ResultIsMissingError: if there is no result when trying to get it. :return: TaskiqResult.

Source code in src/taskiq_pg/asyncpg/result_backend.py
async def get_result(
    self,
    task_id: str,
    with_logs: bool = False,
) -> TaskiqResult[ReturnType]:
    """
    Retrieve result from the task.

    :param task_id: task's id.
    :param with_logs: if True it will download task's logs. (deprecated in taskiq)
    :raises ResultIsMissingError: if there is no result when trying to get it.
    :return: TaskiqResult.
    """
    result_in_bytes = tp.cast(
        "bytes",
        await self._database_pool.fetchval(
            queries.SELECT_RESULT_QUERY.format(
                self.table_name,
            ),
            task_id,
        ),
    )
    if not self.keep_results:
        await self._database_pool.execute(
            queries.DELETE_RESULT_QUERY.format(
                self.table_name,
            ),
            task_id,
        )
    taskiq_result: tp.Final = TaskiqResult[ReturnType].model_validate(
        self.serializer.loadb(result_in_bytes),
    )
    if not with_logs:
        taskiq_result.log = None
    return taskiq_result
set_progress async
set_progress(task_id, progress)

Saves progress.

:param task_id: task's id. :param progress: progress of execution.

Source code in src/taskiq_pg/asyncpg/result_backend.py
async def set_progress(
    self,
    task_id: str,
    progress: TaskProgress[tp.Any],
) -> None:
    """
    Saves progress.

    :param task_id: task's id.
    :param progress: progress of execution.
    """
    await self._database_pool.execute(
        queries.INSERT_PROGRESS_QUERY.format(
            self.table_name,
        ),
        task_id,
        self.serializer.dumpb(progress.model_dump(mode="json")),
    )
get_progress async
get_progress(task_id)

Gets progress.

:param task_id: task's id.

Source code in src/taskiq_pg/asyncpg/result_backend.py
async def get_progress(
    self,
    task_id: str,
) -> TaskProgress[tp.Any] | None:
    """
    Gets progress.

    :param task_id: task's id.
    """
    progress_in_bytes = await self._database_pool.fetchval(
        queries.SELECT_PROGRESS_QUERY.format(
            self.table_name,
        ),
        task_id,
    )
    if progress_in_bytes is None:
        return None
    return TaskProgress[tp.Any].model_validate(
        self.serializer.loadb(progress_in_bytes),
    )

schedule_source

logger module-attribute

logger = getLogger('taskiq_pg.asyncpg_schedule_source')

AsyncpgScheduleSource

AsyncpgScheduleSource(
    broker: AsyncBroker,
    dsn: str | Callable[[], str] = ...,
    table_name: str = ...,
    *,
    pool: None = ...,
    **connect_kwargs: Any,
)
AsyncpgScheduleSource(
    broker: AsyncBroker,
    dsn: str | Callable[[], str] = ...,
    table_name: str = ...,
    *,
    pool: Pool[Record],
)
AsyncpgScheduleSource(
    broker,
    dsn="postgresql://postgres:postgres@localhost:5432/postgres",
    table_name="taskiq_schedules",
    *,
    pool=None,
    **connect_kwargs,
)

Bases: BasePostgresScheduleSource

Schedule source that uses asyncpg to store schedules in PostgreSQL.

Construct a new AsyncpgScheduleSource.

Parameters:

  • broker (AsyncBroker) –

    The TaskIQ broker instance.

  • dsn (str | Callable[[], str], default: 'postgresql://postgres:postgres@localhost:5432/postgres' ) –

    PostgreSQL connection string or callable. Ignored in pool mode.

  • table_name (str, default: 'taskiq_schedules' ) –

    Table to store schedules in.

  • pool (Pool[Record] | None, default: None ) –

    An existing connection pool to reuse.

  • **connect_kwargs (Any, default: {} ) –

    Extra kwargs for connection pool creation.

Source code in src/taskiq_pg/asyncpg/schedule_source.py
def __init__(
    self,
    broker: AsyncBroker,
    dsn: str | tp.Callable[[], str] = "postgresql://postgres:postgres@localhost:5432/postgres",
    table_name: str = "taskiq_schedules",
    *,
    pool: "asyncpg.Pool[asyncpg.Record] | None" = None,
    **connect_kwargs: tp.Any,
) -> None:
    """
    Construct a new AsyncpgScheduleSource.

    Args:
        broker: The TaskIQ broker instance.
        dsn: PostgreSQL connection string or callable. Ignored in pool mode.
        table_name: Table to store schedules in.
        pool: An existing connection pool to reuse.
        **connect_kwargs: Extra kwargs for connection pool creation.
    """
    self._owns_pool = True
    if pool is not None:
        self._owns_pool = False
        self._database_pool = pool

    super().__init__(
        broker=broker,
        dsn=dsn,
        table_name=table_name,
        **connect_kwargs,
    )
dsn property
dsn

Get the DSN string.

startup async
startup()

Initialize the schedule source.

Construct new connection pool (if not provided externally), create new table for schedules if not exists and fill table with schedules from task labels.

Source code in src/taskiq_pg/asyncpg/schedule_source.py
async def startup(self) -> None:
    """
    Initialize the schedule source.

    Construct new connection pool (if not provided externally), create new table for schedules if not exists
    and fill table with schedules from task labels.
    """
    if self._owns_pool:
        self._database_pool = await asyncpg.create_pool(
            dsn=self.dsn,
            **self._connect_kwargs,
        )
    await self._database_pool.execute(
        CREATE_SCHEDULES_TABLE_QUERY.format(
            self._table_name,
        ),
    )
    scheduled_tasks_for_creation = self.extract_scheduled_tasks_from_broker()
    await self._update_schedules_on_startup(scheduled_tasks_for_creation)
shutdown async
shutdown()

Close the connection pool if it created by this schedule source.

Source code in src/taskiq_pg/asyncpg/schedule_source.py
async def shutdown(self) -> None:
    """Close the connection pool if it created by this schedule source."""
    if self._owns_pool and getattr(self, "_database_pool", None) is not None:
        await self._database_pool.close()
get_schedules async
get_schedules()

Fetch schedules from the database.

Source code in src/taskiq_pg/asyncpg/schedule_source.py
async def get_schedules(self) -> list["ScheduledTask"]:
    """Fetch schedules from the database."""
    async with self._database_pool.acquire() as conn:
        rows_with_schedules = await conn.fetch(
            SELECT_SCHEDULES_QUERY.format(self._table_name),
        )
    schedules = []
    for row in rows_with_schedules:
        schedule = json.loads(row["schedule"])
        schedules.append(
            ScheduledTask.model_validate(
                {
                    "schedule_id": str(row["id"]),
                    "task_name": row["task_name"],
                    "labels": schedule["labels"],
                    "args": schedule["args"],
                    "kwargs": schedule["kwargs"],
                    "cron": schedule["cron"],
                    "cron_offset": schedule["cron_offset"],
                    "time": schedule["time"],
                    "interval": schedule["interval"],
                },
            ),
        )
    return schedules
add_schedule async
add_schedule(schedule)

Add a new schedule.

Parameters:

  • schedule (ScheduledTask) –

    schedule to add.

Source code in src/taskiq_pg/asyncpg/schedule_source.py
async def add_schedule(self, schedule: "ScheduledTask") -> None:
    """
    Add a new schedule.

    Args:
        schedule: schedule to add.
    """
    await self._database_pool.execute(
        INSERT_SCHEDULE_QUERY.format(self._table_name),
        str(schedule.schedule_id),
        schedule.task_name,
        schedule.model_dump_json(
            exclude={"schedule_id", "task_name"},
        ),
    )
delete_schedule async
delete_schedule(schedule_id)

Method to delete schedule by id.

This is useful for schedule cancelation.

Parameters:

  • schedule_id (str) –

    id of schedule to delete.

Source code in src/taskiq_pg/asyncpg/schedule_source.py
async def delete_schedule(self, schedule_id: str) -> None:
    """
    Method to delete schedule by id.

    This is useful for schedule cancelation.

    Args:
        schedule_id: id of schedule to delete.
    """
    await self._database_pool.execute(
        DELETE_SCHEDULE_QUERY.format(self._table_name),
        schedule_id,
    )
post_send async
post_send(task)

Delete a task after it's completed.

Source code in src/taskiq_pg/asyncpg/schedule_source.py
async def post_send(self, task: ScheduledTask) -> None:
    """Delete a task after it's completed."""
    if task.time is not None:
        await self.delete_schedule(task.schedule_id)
extract_scheduled_tasks_from_broker
extract_scheduled_tasks_from_broker()

Extract schedules from tasks that were registered in broker.

Returns:

  • list[ScheduledTask] –

    A list of ScheduledTask instances extracted from the task's labels.

Source code in src/taskiq_pg/_internal/schedule_source.py
def extract_scheduled_tasks_from_broker(self) -> list[ScheduledTask]:
    """
    Extract schedules from tasks that were registered in broker.

    Returns:
        A list of ScheduledTask instances extracted from the task's labels.
    """
    scheduled_tasks_for_creation: list[ScheduledTask] = []
    for task_name, task in self._broker.get_all_tasks().items():
        if "schedule" not in task.labels:
            logger.debug("Task %s has no schedule, skipping", task_name)
            continue
        if not isinstance(task.labels["schedule"], list):
            logger.warning(
                "Schedule for task %s is not a list, skipping",
                task_name,
            )
            continue
        for schedule in task.labels["schedule"]:
            try:
                new_schedule = ScheduledTask.model_validate(
                    {
                        "task_name": task_name,
                        "labels": schedule.get("labels", {}),
                        "args": schedule.get("args", []),
                        "kwargs": schedule.get("kwargs", {}),
                        "schedule_id": str(uuid.uuid4()),
                        "cron": schedule.get("cron", None),
                        "cron_offset": schedule.get("cron_offset", None),
                        "interval": schedule.get("interval", None),
                        "time": schedule.get("time", None),
                    },
                )
                scheduled_tasks_for_creation.append(new_schedule)
            except ValidationError:  # noqa: PERF203
                logger.exception(
                    "Schedule for task %s is not valid, skipping",
                    task_name,
                )
                continue
    return scheduled_tasks_for_creation

exceptions

BaseTaskiqPgError

Bases: Exception

Base error for all possible exception in the lib.

DatabaseConnectionError

Bases: BaseTaskiqPgError

Error if cannot connect to PostgreSQL.

ResultIsMissingError

Bases: BaseTaskiqPgError

Error if cannot retrieve result from PostgreSQL.

psqlpy

PSQLPyBroker

PSQLPyBroker(
    dsn="postgresql://postgres:postgres@localhost:5432/postgres",
    result_backend=None,
    task_id_generator=None,
    channel_name="taskiq",
    table_name="taskiq_messages",
    max_retry_attempts=5,
    write_kwargs=None,
    *,
    write_pool=None,
    listen_keepalive_interval=30.0,
    listen_keepalive_timeout=5.0,
    poll_interval=5.0,
    processing_timeout=None,
)

Bases: BasePostgresBroker

Broker that uses PostgreSQL and PSQLPy with LISTEN/NOTIFY.

Construct a new PSQLPyBroker.

The broker LISTENs on a connection from the write pool.

Parameters:

  • dsn (str | Callable[[], str], default: 'postgresql://postgres:postgres@localhost:5432/postgres' ) –

    PostgreSQL connection string or callable.

  • result_backend (AsyncResultBackend[_T] | None, default: None ) –

    Custom result backend.

  • task_id_generator (Callable[[], str] | None, default: None ) –

    Custom task_id generator.

  • channel_name (str, default: 'taskiq' ) –

    Name of the LISTEN/NOTIFY channel.

  • table_name (str, default: 'taskiq_messages' ) –

    Name of the table used to store messages.

  • max_retry_attempts (int, default: 5 ) –

    Maximum number of message processing attempts.

  • write_kwargs (dict[str, Any] | None, default: None ) –

    Extra kwargs forwarded to psqlpy.ConnectionPool()

  • write_pool (ConnectionPool | None, default: None ) –

    An existing pool to reuse for writes and LISTEN.

  • listen_keepalive_interval (float | None, default: 30.0 ) –

    How often, in seconds, to check the broker-owned LISTEN connection with a query; None disables the check.

  • listen_keepalive_timeout (float, default: 5.0 ) –

    How long, in seconds, to wait for the check before reconnecting.

  • poll_interval (float, default: 5.0 ) –

    How often, in seconds, to look for pending messages whose NOTIFY was missed.

  • processing_timeout (float | None, default: None ) –

    Seconds after which an unacknowledged claimed message is delivered again (at-least-once delivery); None keeps at-most-once delivery.

Source code in src/taskiq_pg/psqlpy/broker.py
def __init__(  # noqa: PLR0913
    self,
    dsn: str | Callable[[], str] = "postgresql://postgres:postgres@localhost:5432/postgres",
    result_backend: AsyncResultBackend[_T] | None = None,
    task_id_generator: Callable[[], str] | None = None,
    channel_name: str = "taskiq",
    table_name: str = "taskiq_messages",
    max_retry_attempts: int = 5,
    write_kwargs: dict[str, tp.Any] | None = None,
    *,
    write_pool: psqlpy.ConnectionPool | None = None,
    listen_keepalive_interval: float | None = 30.0,
    listen_keepalive_timeout: float = 5.0,
    poll_interval: float = 5.0,
    processing_timeout: float | None = None,
) -> None:
    """
    Construct a new PSQLPyBroker.

    The broker LISTENs on a connection from the write pool.

    Args:
        dsn: PostgreSQL connection string or callable.
        result_backend: Custom result backend.
        task_id_generator: Custom task_id generator.
        channel_name: Name of the LISTEN/NOTIFY channel.
        table_name: Name of the table used to store messages.
        max_retry_attempts: Maximum number of message processing attempts.
        write_kwargs: Extra kwargs forwarded to `psqlpy.ConnectionPool()`
        write_pool: An existing pool to reuse for writes and LISTEN.
        listen_keepalive_interval: How often, in seconds, to check the broker-owned LISTEN connection
            with a query; None disables the check.
        listen_keepalive_timeout: How long, in seconds, to wait for the check before reconnecting.
        poll_interval: How often, in seconds, to look for pending messages whose NOTIFY was missed.
        processing_timeout: Seconds after which an unacknowledged claimed message is delivered again
            (at-least-once delivery); None keeps at-most-once delivery.
    """
    super().__init__(
        dsn=dsn,
        result_backend=result_backend,
        task_id_generator=task_id_generator,
        channel_name=channel_name,
        table_name=table_name,
        max_retry_attempts=max_retry_attempts,
        write_kwargs=write_kwargs,
        listen_keepalive_interval=listen_keepalive_interval,
        listen_keepalive_timeout=listen_keepalive_timeout,
        poll_interval=poll_interval,
        processing_timeout=processing_timeout,
    )

    self._owns_write_pool = True
    if write_pool is not None:
        self._write_pool = write_pool
        self._owns_write_pool = False

dsn property

dsn

Get the DSN string.

channel_name instance-attribute

channel_name = channel_name

table_name instance-attribute

table_name = table_name

read_kwargs instance-attribute

read_kwargs = read_kwargs or {}

write_kwargs instance-attribute

write_kwargs = write_kwargs or {}

max_retry_attempts instance-attribute

max_retry_attempts = max_retry_attempts

listen_keepalive_interval instance-attribute

listen_keepalive_interval = listen_keepalive_interval

listen_keepalive_timeout instance-attribute

listen_keepalive_timeout = listen_keepalive_timeout

poll_interval instance-attribute

poll_interval = poll_interval

processing_timeout instance-attribute

processing_timeout = processing_timeout

startup async

startup()

Initialize the broker.

Source code in src/taskiq_pg/psqlpy/broker.py
async def startup(self) -> None:
    """Initialize the broker."""
    await super().startup()

    if self._owns_write_pool:
        self._write_pool = psqlpy.ConnectionPool(
            dsn=self.dsn,
            **self.write_kwargs,
        )

    # create messages table if it doesn't exist
    async with self._write_pool.acquire() as conn, conn.transaction() as transaction:
        await transaction.execute(schema_lock_query(self.table_name))
        result = await transaction.fetch(
            SCHEMA_STATUS_QUERY.format(table="$1", pending_index="$2", processing_index="$3"),
            [self.table_name, f"{self.table_name}_pending_idx", f"{self.table_name}_processing_idx"],
        )
        status = result.result()[0]
        if not status["has_table"]:
            await transaction.execute(CREATE_MESSAGE_TABLE_QUERY.format(self.table_name))
        if not status["has_delivery_columns"]:
            await transaction.execute(ADD_DELIVERY_COLUMNS_QUERY.format(table=self.table_name))
        if not status["has_pending_index"]:
            await transaction.execute(
                CREATE_PENDING_INDEX_QUERY.format(index=f"{self.table_name}_pending_idx", table=self.table_name),
            )
        if self.processing_timeout is not None and not status["has_processing_index"]:
            await transaction.execute(
                CREATE_PROCESSING_INDEX_QUERY.format(
                    index=f"{self.table_name}_processing_idx",
                    table=self.table_name,
                ),
            )

    self._queue = asyncio.Queue()

    # listen to notification channel
    self._listener = await self._start_listener()
    self._listener_task = asyncio.create_task(self._consume_notifications())

shutdown async

shutdown()

Close all connections on shutdown.

Source code in src/taskiq_pg/psqlpy/broker.py
async def shutdown(self) -> None:
    """Close all connections on shutdown."""
    await super().shutdown()
    if self._listener_task is not None:
        await cancel_and_wait(self._listener_task)
        self._listener_task = None
    # startup() may have failed before creating these
    listener = getattr(self, "_listener", None)
    if listener is not None:
        await listener.shutdown()
    if getattr(self, "_write_pool", None) is not None and self._owns_write_pool:
        self._write_pool.close()

kick async

kick(message)

Send message to the channel.

Inserts the message into the database and sends a NOTIFY.

:param message: Message to send.

Source code in src/taskiq_pg/psqlpy/broker.py
async def kick(self, message: BrokerMessage) -> None:
    """
    Send message to the channel.

    Inserts the message into the database and sends a NOTIFY.

    :param message: Message to send.
    """
    # Parse the delay before inserting, so a malformed label does not leave a message behind.
    delay_value = message.labels.get("delay")
    delay_seconds = None if delay_value is None else int(delay_value)

    async with self._write_pool.acquire() as conn:
        # insert message into db table
        message_inserted_id = tp.cast(
            "int",
            await conn.fetch_val(
                INSERT_MESSAGE_QUERY.format(self.table_name),
                [
                    message.task_id,
                    message.task_name,
                    message.message.decode(),
                    JSONB(message.labels),
                ],
            ),
        )

        if delay_seconds is not None:
            asyncio.create_task(  # noqa: RUF006
                self._schedule_notification(message_inserted_id, delay_seconds),
            )
        else:
            # Send NOTIFY with message ID as payload
            _ = await conn.execute(
                f"NOTIFY {self.channel_name}, '{message_inserted_id}'",
            )

listen async

listen()

Listen to the channel.

Yields messages as they are received.

:yields: AckableMessage instances.

Source code in src/taskiq_pg/psqlpy/broker.py
async def listen(self) -> AsyncGenerator[AckableMessage, None]:
    """
    Listen to the channel.

    Yields messages as they are received.

    :yields: AckableMessage instances.
    """
    loop = asyncio.get_running_loop()
    next_poll = loop.time()  # poll right away: messages may have been kicked while nobody listened
    while True:
        try:
            if loop.time() >= next_poll:
                next_poll = loop.time() + self.poll_interval
                await self._release_stale_messages()
                while (claimed := await self._claim_next_ready()) is not None:
                    yield self._ackable(claimed)
                continue
            try:
                payload = await asyncio.wait_for(self._queue.get(), next_poll - loop.time())
            except TimeoutError:
                continue
            if payload == POLL_NOW:
                next_poll = loop.time()
                continue
            claimed = await self._claim(int(payload))  # payload is the message id
            if claimed is not None:
                yield self._ackable(claimed)
        except Exception:
            logger.exception("Error processing message")
            continue

PSQLPyResultBackend

PSQLPyResultBackend(
    dsn: Callable[[], str] | str | None = ...,
    keep_results: bool = ...,
    table_name: str = ...,
    field_for_task_id: Literal[
        "VarChar", "Text", "Uuid"
    ] = ...,
    serializer: TaskiqSerializer | None = ...,
    *,
    pool: None = ...,
    **connect_kwargs: Any,
)
PSQLPyResultBackend(
    dsn: Callable[[], str] | str | None = ...,
    keep_results: bool = ...,
    table_name: str = ...,
    field_for_task_id: Literal[
        "VarChar", "Text", "Uuid"
    ] = ...,
    serializer: TaskiqSerializer | None = ...,
    *,
    pool: ConnectionPool,
)
PSQLPyResultBackend(
    dsn="postgres://postgres:postgres@localhost:5432/postgres",
    keep_results=True,
    table_name="taskiq_results",
    field_for_task_id="VarChar",
    serializer=None,
    *,
    pool=None,
    **connect_kwargs,
)

Bases: BasePostgresResultBackend

Result backend for TaskIQ based on PSQLPy.

Construct a new PSQLPyResultBackend.

Parameters:

  • dsn (Callable[[], str] | str | None, default: 'postgres://postgres:postgres@localhost:5432/postgres' ) –

    PostgreSQL connection string or callable. Can be None if pool is provided.

  • keep_results (bool, default: True ) –

    Whether to keep results after reading.

  • table_name (str, default: 'taskiq_results' ) –

    Table to store results in.

  • field_for_task_id (Literal['VarChar', 'Text', 'Uuid'], default: 'VarChar' ) –

    Column type for task_id.

  • serializer (TaskiqSerializer | None, default: None ) –

    Serializer for task results.

  • pool (ConnectionPool | None, default: None ) –

    An existing connection pool to reuse.

  • **connect_kwargs (Any, default: {} ) –

    Extra kwargs for connection pool creation.

Source code in src/taskiq_pg/psqlpy/result_backend.py
def __init__(
    self,
    dsn: tp.Callable[[], str] | str | None = "postgres://postgres:postgres@localhost:5432/postgres",
    keep_results: bool = True,
    table_name: str = "taskiq_results",
    field_for_task_id: tp.Literal["VarChar", "Text", "Uuid"] = "VarChar",
    serializer: TaskiqSerializer | None = None,
    *,
    pool: ConnectionPool | None = None,
    **connect_kwargs: tp.Any,
) -> None:
    """
    Construct a new PSQLPyResultBackend.

    Args:
        dsn: PostgreSQL connection string or callable. Can be None if pool is provided.
        keep_results: Whether to keep results after reading.
        table_name: Table to store results in.
        field_for_task_id: Column type for task_id.
        serializer: Serializer for task results.
        pool: An existing connection pool to reuse.
        **connect_kwargs: Extra kwargs for connection pool creation.
    """
    self._owns_pool = True
    if pool is not None:
        self._owns_pool = False
        self._database_pool = pool

    super().__init__(
        dsn=dsn,
        keep_results=keep_results,
        table_name=table_name,
        field_for_task_id=field_for_task_id,
        serializer=serializer,
        **connect_kwargs,
    )

dsn property

dsn

Get the DSN string.

keep_results instance-attribute

keep_results = keep_results

table_name instance-attribute

table_name = table_name

field_for_task_id instance-attribute

field_for_task_id = field_for_task_id

connect_kwargs instance-attribute

connect_kwargs = connect_kwargs

serializer instance-attribute

serializer = serializer or PickleSerializer()

startup async

startup()

Initialize the result backend.

Construct new connection pool (if not provided externally) and create new table for results if not exists.

Source code in src/taskiq_pg/psqlpy/result_backend.py
async def startup(self) -> None:
    """
    Initialize the result backend.

    Construct new connection pool (if not provided externally) and create new table for results if not exists.
    """
    if self._owns_pool:
        self._database_pool = ConnectionPool(
            dsn=self.dsn,
            **self.connect_kwargs,
        )
    connection = await self._database_pool.connection()
    await connection.execute(
        querystring=queries.CREATE_TABLE_QUERY.format(
            self.table_name,
            self.field_for_task_id,
        ),
    )
    await connection.execute(
        querystring=queries.ADD_PROGRESS_COLUMN_QUERY.format(
            self.table_name,
        ),
    )
    await connection.execute(
        querystring=queries.CREATE_INDEX_QUERY.format(
            self.table_name,
            self.table_name,
        ),
    )

shutdown async

shutdown()

Close the connection pool if it was created by this result backend.

Source code in src/taskiq_pg/psqlpy/result_backend.py
async def shutdown(self) -> None:
    """Close the connection pool if it was created by this result backend."""
    if self._owns_pool and getattr(self, "_database_pool", None) is not None:
        self._database_pool.close()

set_result async

set_result(task_id, result)

Set result to the PostgreSQL table.

:param task_id: ID of the task. :param result: result of the task.

Source code in src/taskiq_pg/psqlpy/result_backend.py
async def set_result(
    self,
    task_id: str,
    result: TaskiqResult[ReturnType],
) -> None:
    """
    Set result to the PostgreSQL table.

    :param task_id: ID of the task.
    :param result: result of the task.
    """
    connection = await self._database_pool.connection()
    await connection.execute(
        querystring=queries.INSERT_RESULT_QUERY.format(
            self.table_name,
        ),
        parameters=[
            task_id,
            self.serializer.dumpb(result.model_dump(mode="json")),
        ],
    )

is_result_ready async

is_result_ready(task_id)

Returns whether the result is ready.

:param task_id: ID of the task.

:returns: True if the result is ready else False.

Source code in src/taskiq_pg/psqlpy/result_backend.py
async def is_result_ready(self, task_id: str) -> bool:
    """
    Returns whether the result is ready.

    :param task_id: ID of the task.

    :returns: True if the result is ready else False.
    """
    connection: tp.Final = await self._database_pool.connection()
    return tp.cast(
        "bool",
        await connection.fetch_val(
            querystring=queries.IS_RESULT_EXISTS_QUERY.format(
                self.table_name,
            ),
            parameters=[task_id],
        ),
    )

get_result async

get_result(task_id, with_logs=False)

Retrieve result from the task.

:param task_id: task's id. :param with_logs: if True it will download task's logs. :raises ResultIsMissingError: if there is no result when trying to get it. :return: TaskiqResult.

Source code in src/taskiq_pg/psqlpy/result_backend.py
async def get_result(
    self,
    task_id: str,
    with_logs: bool = False,
) -> TaskiqResult[ReturnType]:
    """
    Retrieve result from the task.

    :param task_id: task's id.
    :param with_logs: if True it will download task's logs.
    :raises ResultIsMissingError: if there is no result when trying to get it.
    :return: TaskiqResult.
    """
    connection: tp.Final = await self._database_pool.connection()
    try:
        result_in_bytes: tp.Final[bytes] = await connection.fetch_val(
            querystring=queries.SELECT_RESULT_QUERY.format(
                self.table_name,
            ),
            parameters=[task_id],
        )
    except BaseConnectionError as exc:
        msg = f"Cannot find record with task_id = {task_id} in PostgreSQL"
        raise ResultIsMissingError(msg) from exc

    if not self.keep_results:
        await connection.execute(
            querystring=queries.DELETE_RESULT_QUERY.format(
                self.table_name,
            ),
            parameters=[task_id],
        )

    taskiq_result: tp.Final = TaskiqResult[ReturnType].model_validate(
        self.serializer.loadb(result_in_bytes),
    )

    if not with_logs:
        taskiq_result.log = None

    return taskiq_result

set_progress async

set_progress(task_id, progress)

Saves progress.

:param task_id: task's id. :param progress: progress of execution.

Source code in src/taskiq_pg/psqlpy/result_backend.py
async def set_progress(
    self,
    task_id: str,
    progress: TaskProgress[tp.Any],
) -> None:
    """
    Saves progress.

    :param task_id: task's id.
    :param progress: progress of execution.
    """
    connection = await self._database_pool.connection()
    await connection.execute(
        querystring=queries.INSERT_PROGRESS_QUERY.format(
            self.table_name,
        ),
        parameters=[
            task_id,
            self.serializer.dumpb(progress.model_dump(mode="json")),
        ],
    )

get_progress async

get_progress(task_id)

Gets progress.

:param task_id: task's id.

Source code in src/taskiq_pg/psqlpy/result_backend.py
async def get_progress(
    self,
    task_id: str,
) -> TaskProgress[tp.Any] | None:
    """
    Gets progress.

    :param task_id: task's id.
    """
    connection: tp.Final = await self._database_pool.connection()
    try:
        progress_in_bytes = await connection.fetch_val(
            querystring=queries.SELECT_PROGRESS_QUERY.format(
                self.table_name,
            ),
            parameters=[task_id],
        )
    except BaseConnectionError as exc:
        msg = f"Cannot find record with task_id = {task_id} in PostgreSQL"
        raise ResultIsMissingError(msg) from exc
    if progress_in_bytes is None:
        return None
    return TaskProgress[tp.Any].model_validate(
        self.serializer.loadb(progress_in_bytes),
    )

PSQLPyScheduleSource

PSQLPyScheduleSource(
    broker: AsyncBroker,
    dsn: str | Callable[[], str] = ...,
    table_name: str = ...,
    *,
    pool: None = ...,
    **connect_kwargs: Any,
)
PSQLPyScheduleSource(
    broker: AsyncBroker,
    dsn: str | Callable[[], str] = ...,
    table_name: str = ...,
    *,
    pool: ConnectionPool,
)
PSQLPyScheduleSource(
    broker,
    dsn="postgresql://postgres:postgres@localhost:5432/postgres",
    table_name="taskiq_schedules",
    *,
    pool=None,
    **connect_kwargs,
)

Bases: BasePostgresScheduleSource

Schedule source that uses psqlpy to store schedules in PostgreSQL.

Construct a new PSQLPyScheduleSource.

Parameters:

  • broker (AsyncBroker) –

    The TaskIQ broker instance.

  • dsn (str | Callable[[], str], default: 'postgresql://postgres:postgres@localhost:5432/postgres' ) –

    PostgreSQL connection string or callable. Ignored in pool mode.

  • table_name (str, default: 'taskiq_schedules' ) –

    Table to store schedules in.

  • pool (ConnectionPool | None, default: None ) –

    An existing connection pool to reuse.

  • **connect_kwargs (Any, default: {} ) –

    Extra kwargs for connection pool creation.

Source code in src/taskiq_pg/psqlpy/schedule_source.py
def __init__(
    self,
    broker: AsyncBroker,
    dsn: "str | tp.Callable[[], str]" = "postgresql://postgres:postgres@localhost:5432/postgres",
    table_name: str = "taskiq_schedules",
    *,
    pool: ConnectionPool | None = None,
    **connect_kwargs: tp.Any,
) -> None:
    """
    Construct a new PSQLPyScheduleSource.

    Args:
        broker: The TaskIQ broker instance.
        dsn: PostgreSQL connection string or callable. Ignored in pool mode.
        table_name: Table to store schedules in.
        pool: An existing connection pool to reuse.
        **connect_kwargs: Extra kwargs for connection pool creation.
    """
    if pool is not None:
        self._owns_pool = False
        self._database_pool = pool
    else:
        self._owns_pool = True

    super().__init__(
        broker=broker,
        dsn=dsn,
        table_name=table_name,
        **connect_kwargs,
    )

dsn property

dsn

Get the DSN string.

extract_scheduled_tasks_from_broker

extract_scheduled_tasks_from_broker()

Extract schedules from tasks that were registered in broker.

Returns:

  • list[ScheduledTask] –

    A list of ScheduledTask instances extracted from the task's labels.

Source code in src/taskiq_pg/_internal/schedule_source.py
def extract_scheduled_tasks_from_broker(self) -> list[ScheduledTask]:
    """
    Extract schedules from tasks that were registered in broker.

    Returns:
        A list of ScheduledTask instances extracted from the task's labels.
    """
    scheduled_tasks_for_creation: list[ScheduledTask] = []
    for task_name, task in self._broker.get_all_tasks().items():
        if "schedule" not in task.labels:
            logger.debug("Task %s has no schedule, skipping", task_name)
            continue
        if not isinstance(task.labels["schedule"], list):
            logger.warning(
                "Schedule for task %s is not a list, skipping",
                task_name,
            )
            continue
        for schedule in task.labels["schedule"]:
            try:
                new_schedule = ScheduledTask.model_validate(
                    {
                        "task_name": task_name,
                        "labels": schedule.get("labels", {}),
                        "args": schedule.get("args", []),
                        "kwargs": schedule.get("kwargs", {}),
                        "schedule_id": str(uuid.uuid4()),
                        "cron": schedule.get("cron", None),
                        "cron_offset": schedule.get("cron_offset", None),
                        "interval": schedule.get("interval", None),
                        "time": schedule.get("time", None),
                    },
                )
                scheduled_tasks_for_creation.append(new_schedule)
            except ValidationError:  # noqa: PERF203
                logger.exception(
                    "Schedule for task %s is not valid, skipping",
                    task_name,
                )
                continue
    return scheduled_tasks_for_creation

startup async

startup()

Initialize the schedule source.

Construct new connection pool (if not provided externally), create new table for schedules if not exists and fill table with schedules from task labels.

Source code in src/taskiq_pg/psqlpy/schedule_source.py
async def startup(self) -> None:
    """
    Initialize the schedule source.

    Construct new connection pool (if not provided externally), create new table for
    schedules if not exists and fill table with schedules from task labels.
    """
    if self._owns_pool:
        self._database_pool = ConnectionPool(
            dsn=self.dsn,
            **self._connect_kwargs,
        )
    async with self._database_pool.acquire() as connection:
        await connection.execute(
            CREATE_SCHEDULES_TABLE_QUERY.format(
                self._table_name,
            ),
        )
    scheduled_tasks_for_creation = self.extract_scheduled_tasks_from_broker()
    await self._update_schedules_on_startup(scheduled_tasks_for_creation)

shutdown async

shutdown()

Close the connection pool if was created by this schedule source.

Source code in src/taskiq_pg/psqlpy/schedule_source.py
async def shutdown(self) -> None:
    """Close the connection pool if was created by this schedule source."""
    if self._owns_pool and getattr(self, "_database_pool", None) is not None:
        self._database_pool.close()

get_schedules async

get_schedules()

Fetch schedules from the database.

Source code in src/taskiq_pg/psqlpy/schedule_source.py
async def get_schedules(self) -> list["ScheduledTask"]:
    """Fetch schedules from the database."""
    async with self._database_pool.acquire() as connection:
        rows_with_schedules = await connection.fetch(
            SELECT_SCHEDULES_QUERY.format(self._table_name),
        )
    schedules = []
    for row in rows_with_schedules.result():
        schedule = row["schedule"]
        schedules.append(
            ScheduledTask.model_validate(
                {
                    "schedule_id": str(row["id"]),
                    "task_name": row["task_name"],
                    "labels": schedule["labels"],
                    "args": schedule["args"],
                    "kwargs": schedule["kwargs"],
                    "cron": schedule["cron"],
                    "cron_offset": schedule["cron_offset"],
                    "time": schedule["time"],
                    "interval": schedule["interval"],
                },
            ),
        )
    return schedules

add_schedule async

add_schedule(schedule)

Add a new schedule.

Parameters:

  • schedule (ScheduledTask) –

    schedule to add.

Source code in src/taskiq_pg/psqlpy/schedule_source.py
async def add_schedule(self, schedule: "ScheduledTask") -> None:
    """
    Add a new schedule.

    Args:
        schedule: schedule to add.
    """
    async with self._database_pool.acquire() as connection:
        schedule_dict = schedule.model_dump(
            mode="json",
            exclude={"schedule_id", "task_name"},
        )
        await connection.execute(
            INSERT_SCHEDULE_QUERY.format(self._table_name),
            [
                uuid.UUID(schedule.schedule_id),
                schedule.task_name,
                JSONB(schedule_dict),
            ]
        )

delete_schedule async

delete_schedule(schedule_id)

Method to delete schedule by id.

This is useful for schedule cancelation.

Parameters:

  • schedule_id (str) –

    id of schedule to delete.

Source code in src/taskiq_pg/psqlpy/schedule_source.py
async def delete_schedule(self, schedule_id: str) -> None:
    """
    Method to delete schedule by id.

    This is useful for schedule cancelation.

    Args:
        schedule_id: id of schedule to delete.
    """
    async with self._database_pool.acquire() as connection:
        await connection.execute(
            DELETE_SCHEDULE_QUERY.format(self._table_name),
            [uuid.UUID(schedule_id)],
        )

post_send async

post_send(task)

Delete a task after it's completed.

Source code in src/taskiq_pg/psqlpy/schedule_source.py
async def post_send(self, task: ScheduledTask) -> None:
    """Delete a task after it's completed."""
    if task.time is not None:
        await self.delete_schedule(task.schedule_id)

broker

logger module-attribute

logger = logging.getLogger('taskiq.psqlpy_broker')

MessageRow dataclass

MessageRow(
    id,
    task_id,
    task_name,
    message,
    labels,
    status,
    created_at,
    claimed_at=None,
    attempts=0,
)

Message in db table.

id instance-attribute
id
task_id instance-attribute
task_id
task_name instance-attribute
task_name
message instance-attribute
message
labels instance-attribute
labels
status instance-attribute
status
created_at instance-attribute
created_at
claimed_at class-attribute instance-attribute
claimed_at = None
attempts class-attribute instance-attribute
attempts = 0

PSQLPyBroker

PSQLPyBroker(
    dsn="postgresql://postgres:postgres@localhost:5432/postgres",
    result_backend=None,
    task_id_generator=None,
    channel_name="taskiq",
    table_name="taskiq_messages",
    max_retry_attempts=5,
    write_kwargs=None,
    *,
    write_pool=None,
    listen_keepalive_interval=30.0,
    listen_keepalive_timeout=5.0,
    poll_interval=5.0,
    processing_timeout=None,
)

Bases: BasePostgresBroker

Broker that uses PostgreSQL and PSQLPy with LISTEN/NOTIFY.

Construct a new PSQLPyBroker.

The broker LISTENs on a connection from the write pool.

Parameters:

  • dsn (str | Callable[[], str], default: 'postgresql://postgres:postgres@localhost:5432/postgres' ) –

    PostgreSQL connection string or callable.

  • result_backend (AsyncResultBackend[_T] | None, default: None ) –

    Custom result backend.

  • task_id_generator (Callable[[], str] | None, default: None ) –

    Custom task_id generator.

  • channel_name (str, default: 'taskiq' ) –

    Name of the LISTEN/NOTIFY channel.

  • table_name (str, default: 'taskiq_messages' ) –

    Name of the table used to store messages.

  • max_retry_attempts (int, default: 5 ) –

    Maximum number of message processing attempts.

  • write_kwargs (dict[str, Any] | None, default: None ) –

    Extra kwargs forwarded to psqlpy.ConnectionPool()

  • write_pool (ConnectionPool | None, default: None ) –

    An existing pool to reuse for writes and LISTEN.

  • listen_keepalive_interval (float | None, default: 30.0 ) –

    How often, in seconds, to check the broker-owned LISTEN connection with a query; None disables the check.

  • listen_keepalive_timeout (float, default: 5.0 ) –

    How long, in seconds, to wait for the check before reconnecting.

  • poll_interval (float, default: 5.0 ) –

    How often, in seconds, to look for pending messages whose NOTIFY was missed.

  • processing_timeout (float | None, default: None ) –

    Seconds after which an unacknowledged claimed message is delivered again (at-least-once delivery); None keeps at-most-once delivery.

Source code in src/taskiq_pg/psqlpy/broker.py
def __init__(  # noqa: PLR0913
    self,
    dsn: str | Callable[[], str] = "postgresql://postgres:postgres@localhost:5432/postgres",
    result_backend: AsyncResultBackend[_T] | None = None,
    task_id_generator: Callable[[], str] | None = None,
    channel_name: str = "taskiq",
    table_name: str = "taskiq_messages",
    max_retry_attempts: int = 5,
    write_kwargs: dict[str, tp.Any] | None = None,
    *,
    write_pool: psqlpy.ConnectionPool | None = None,
    listen_keepalive_interval: float | None = 30.0,
    listen_keepalive_timeout: float = 5.0,
    poll_interval: float = 5.0,
    processing_timeout: float | None = None,
) -> None:
    """
    Construct a new PSQLPyBroker.

    The broker LISTENs on a connection from the write pool.

    Args:
        dsn: PostgreSQL connection string or callable.
        result_backend: Custom result backend.
        task_id_generator: Custom task_id generator.
        channel_name: Name of the LISTEN/NOTIFY channel.
        table_name: Name of the table used to store messages.
        max_retry_attempts: Maximum number of message processing attempts.
        write_kwargs: Extra kwargs forwarded to `psqlpy.ConnectionPool()`
        write_pool: An existing pool to reuse for writes and LISTEN.
        listen_keepalive_interval: How often, in seconds, to check the broker-owned LISTEN connection
            with a query; None disables the check.
        listen_keepalive_timeout: How long, in seconds, to wait for the check before reconnecting.
        poll_interval: How often, in seconds, to look for pending messages whose NOTIFY was missed.
        processing_timeout: Seconds after which an unacknowledged claimed message is delivered again
            (at-least-once delivery); None keeps at-most-once delivery.
    """
    super().__init__(
        dsn=dsn,
        result_backend=result_backend,
        task_id_generator=task_id_generator,
        channel_name=channel_name,
        table_name=table_name,
        max_retry_attempts=max_retry_attempts,
        write_kwargs=write_kwargs,
        listen_keepalive_interval=listen_keepalive_interval,
        listen_keepalive_timeout=listen_keepalive_timeout,
        poll_interval=poll_interval,
        processing_timeout=processing_timeout,
    )

    self._owns_write_pool = True
    if write_pool is not None:
        self._write_pool = write_pool
        self._owns_write_pool = False
dsn property
dsn

Get the DSN string.

channel_name instance-attribute
channel_name = channel_name
table_name instance-attribute
table_name = table_name
read_kwargs instance-attribute
read_kwargs = read_kwargs or {}
write_kwargs instance-attribute
write_kwargs = write_kwargs or {}
max_retry_attempts instance-attribute
max_retry_attempts = max_retry_attempts
listen_keepalive_interval instance-attribute
listen_keepalive_interval = listen_keepalive_interval
listen_keepalive_timeout instance-attribute
listen_keepalive_timeout = listen_keepalive_timeout
poll_interval instance-attribute
poll_interval = poll_interval
processing_timeout instance-attribute
processing_timeout = processing_timeout
startup async
startup()

Initialize the broker.

Source code in src/taskiq_pg/psqlpy/broker.py
async def startup(self) -> None:
    """Initialize the broker."""
    await super().startup()

    if self._owns_write_pool:
        self._write_pool = psqlpy.ConnectionPool(
            dsn=self.dsn,
            **self.write_kwargs,
        )

    # create messages table if it doesn't exist
    async with self._write_pool.acquire() as conn, conn.transaction() as transaction:
        await transaction.execute(schema_lock_query(self.table_name))
        result = await transaction.fetch(
            SCHEMA_STATUS_QUERY.format(table="$1", pending_index="$2", processing_index="$3"),
            [self.table_name, f"{self.table_name}_pending_idx", f"{self.table_name}_processing_idx"],
        )
        status = result.result()[0]
        if not status["has_table"]:
            await transaction.execute(CREATE_MESSAGE_TABLE_QUERY.format(self.table_name))
        if not status["has_delivery_columns"]:
            await transaction.execute(ADD_DELIVERY_COLUMNS_QUERY.format(table=self.table_name))
        if not status["has_pending_index"]:
            await transaction.execute(
                CREATE_PENDING_INDEX_QUERY.format(index=f"{self.table_name}_pending_idx", table=self.table_name),
            )
        if self.processing_timeout is not None and not status["has_processing_index"]:
            await transaction.execute(
                CREATE_PROCESSING_INDEX_QUERY.format(
                    index=f"{self.table_name}_processing_idx",
                    table=self.table_name,
                ),
            )

    self._queue = asyncio.Queue()

    # listen to notification channel
    self._listener = await self._start_listener()
    self._listener_task = asyncio.create_task(self._consume_notifications())
shutdown async
shutdown()

Close all connections on shutdown.

Source code in src/taskiq_pg/psqlpy/broker.py
async def shutdown(self) -> None:
    """Close all connections on shutdown."""
    await super().shutdown()
    if self._listener_task is not None:
        await cancel_and_wait(self._listener_task)
        self._listener_task = None
    # startup() may have failed before creating these
    listener = getattr(self, "_listener", None)
    if listener is not None:
        await listener.shutdown()
    if getattr(self, "_write_pool", None) is not None and self._owns_write_pool:
        self._write_pool.close()
kick async
kick(message)

Send message to the channel.

Inserts the message into the database and sends a NOTIFY.

:param message: Message to send.

Source code in src/taskiq_pg/psqlpy/broker.py
async def kick(self, message: BrokerMessage) -> None:
    """
    Send message to the channel.

    Inserts the message into the database and sends a NOTIFY.

    :param message: Message to send.
    """
    # Parse the delay before inserting, so a malformed label does not leave a message behind.
    delay_value = message.labels.get("delay")
    delay_seconds = None if delay_value is None else int(delay_value)

    async with self._write_pool.acquire() as conn:
        # insert message into db table
        message_inserted_id = tp.cast(
            "int",
            await conn.fetch_val(
                INSERT_MESSAGE_QUERY.format(self.table_name),
                [
                    message.task_id,
                    message.task_name,
                    message.message.decode(),
                    JSONB(message.labels),
                ],
            ),
        )

        if delay_seconds is not None:
            asyncio.create_task(  # noqa: RUF006
                self._schedule_notification(message_inserted_id, delay_seconds),
            )
        else:
            # Send NOTIFY with message ID as payload
            _ = await conn.execute(
                f"NOTIFY {self.channel_name}, '{message_inserted_id}'",
            )
listen async
listen()

Listen to the channel.

Yields messages as they are received.

:yields: AckableMessage instances.

Source code in src/taskiq_pg/psqlpy/broker.py
async def listen(self) -> AsyncGenerator[AckableMessage, None]:
    """
    Listen to the channel.

    Yields messages as they are received.

    :yields: AckableMessage instances.
    """
    loop = asyncio.get_running_loop()
    next_poll = loop.time()  # poll right away: messages may have been kicked while nobody listened
    while True:
        try:
            if loop.time() >= next_poll:
                next_poll = loop.time() + self.poll_interval
                await self._release_stale_messages()
                while (claimed := await self._claim_next_ready()) is not None:
                    yield self._ackable(claimed)
                continue
            try:
                payload = await asyncio.wait_for(self._queue.get(), next_poll - loop.time())
            except TimeoutError:
                continue
            if payload == POLL_NOW:
                next_poll = loop.time()
                continue
            claimed = await self._claim(int(payload))  # payload is the message id
            if claimed is not None:
                yield self._ackable(claimed)
        except Exception:
            logger.exception("Error processing message")
            continue

queries

CREATE_TABLE_QUERY module-attribute

CREATE_TABLE_QUERY = "\nCREATE TABLE IF NOT EXISTS {} (\n    task_id {} UNIQUE,\n    result BYTEA,\n    progress BYTEA\n)\n"

ADD_PROGRESS_COLUMN_QUERY module-attribute

ADD_PROGRESS_COLUMN_QUERY = "\nALTER TABLE {} ADD COLUMN IF NOT EXISTS progress BYTEA;\n"

CREATE_INDEX_QUERY module-attribute

CREATE_INDEX_QUERY = "\nCREATE INDEX IF NOT EXISTS {}_task_id_idx ON {} USING HASH (task_id)\n"

INSERT_RESULT_QUERY module-attribute

INSERT_RESULT_QUERY = "\nINSERT INTO {} VALUES ($1, $2, NULL)\nON CONFLICT (task_id)\nDO UPDATE\nSET result = $2\n"

INSERT_PROGRESS_QUERY module-attribute

INSERT_PROGRESS_QUERY = "\nINSERT INTO {} VALUES ($1, NULL, $2)\nON CONFLICT (task_id)\nDO UPDATE\nSET progress = $2\n"

SELECT_PROGRESS_QUERY module-attribute

SELECT_PROGRESS_QUERY = (
    "\nSELECT progress FROM {} WHERE task_id = $1\n"
)

IS_RESULT_EXISTS_QUERY module-attribute

IS_RESULT_EXISTS_QUERY = "\nSELECT EXISTS(\n    SELECT 1 FROM {} WHERE task_id = $1 and result IS NOT NULL\n)\n"

SELECT_RESULT_QUERY module-attribute

SELECT_RESULT_QUERY = (
    "\nSELECT result FROM {} WHERE task_id = $1\n"
)

DELETE_RESULT_QUERY module-attribute

DELETE_RESULT_QUERY = (
    "\nDELETE FROM {} WHERE task_id = $1\n"
)

CREATE_MESSAGE_TABLE_QUERY module-attribute

CREATE_MESSAGE_TABLE_QUERY = "\nCREATE TABLE IF NOT EXISTS {} (\n    id SERIAL PRIMARY KEY,\n    task_id VARCHAR NOT NULL,\n    task_name VARCHAR NOT NULL,\n    message TEXT NOT NULL,\n    labels JSONB NOT NULL,\n    status TEXT NOT NULL DEFAULT 'pending',\n    created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW()\n);\n"

INSERT_MESSAGE_QUERY module-attribute

INSERT_MESSAGE_QUERY = "\nINSERT INTO {} (task_id, task_name, message, labels)\nVALUES ($1, $2, $3, $4)\nRETURNING id\n"

CLAIM_MESSAGE_QUERY module-attribute

CLAIM_MESSAGE_QUERY = "\nUPDATE {} SET status = 'processing', claimed_at = now(), attempts = attempts + 1\nWHERE id = $1 AND status = 'pending'\nRETURNING *\n"

DELETE_MESSAGE_QUERY module-attribute

DELETE_MESSAGE_QUERY = (
    "DELETE FROM {} WHERE id = $1 AND attempts = $2"
)

CREATE_SCHEDULES_TABLE_QUERY module-attribute

CREATE_SCHEDULES_TABLE_QUERY = "\nCREATE TABLE IF NOT EXISTS {} (\n    id UUID PRIMARY KEY,\n    task_name VARCHAR(100) NOT NULL,\n    schedule JSONB NOT NULL,\n    created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),\n    updated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW()\n);\n"

INSERT_SCHEDULE_QUERY module-attribute

INSERT_SCHEDULE_QUERY = "\nINSERT INTO {} (id, task_name, schedule)\nVALUES ($1, $2, $3)\nON CONFLICT (id) DO UPDATE\nSET task_name = EXCLUDED.task_name,\n    schedule = EXCLUDED.schedule,\n    updated_at = NOW();\n"

SELECT_SCHEDULES_QUERY module-attribute

SELECT_SCHEDULES_QUERY = (
    "\nSELECT id, task_name, schedule\nFROM {};\n"
)

DELETE_ALL_SCHEDULES_QUERY module-attribute

DELETE_ALL_SCHEDULES_QUERY = '\nDELETE FROM {};\n'

DELETE_SCHEDULE_QUERY module-attribute

DELETE_SCHEDULE_QUERY = '\nDELETE FROM {} WHERE id = $1;\n'

CREATE_PENDING_INDEX_QUERY module-attribute

CREATE_PENDING_INDEX_QUERY = "CREATE INDEX IF NOT EXISTS {index} ON {table} (id) WHERE status = 'pending'"

CLAIM_NEXT_READY_MESSAGE_QUERY module-attribute

CLAIM_NEXT_READY_MESSAGE_QUERY = "\nUPDATE {table} SET status = 'processing', claimed_at = now(), attempts = attempts + 1\nWHERE id = (\n    SELECT id FROM {table}\n    WHERE status = 'pending'\n        AND created_at + make_interval(secs => CASE\n            -- a label that is not a plain number must not break the query for every other row\n            WHEN labels->>'delay' ~ '^[0-9]{{1,10}}(\\.[0-9]+)?$' THEN (labels->>'delay')::double precision\n            WHEN labels->>'delay' ~ '^[0-9]+(\\.[0-9]+)?$' THEN 1e10  -- over 300 years: never ready\n            ELSE 0\n        END) <= now()\n    ORDER BY id\n    LIMIT 1\n    FOR UPDATE SKIP LOCKED\n)\nRETURNING *\n"

ADD_DELIVERY_COLUMNS_QUERY module-attribute

ADD_DELIVERY_COLUMNS_QUERY = "\nALTER TABLE {table}\n    ADD COLUMN IF NOT EXISTS claimed_at TIMESTAMP WITH TIME ZONE,\n    ADD COLUMN IF NOT EXISTS attempts INTEGER NOT NULL DEFAULT 0\n"

CREATE_PROCESSING_INDEX_QUERY module-attribute

CREATE_PROCESSING_INDEX_QUERY = "CREATE INDEX IF NOT EXISTS {index} ON {table} (claimed_at) WHERE status = 'processing'"

RELEASE_STALE_MESSAGES_QUERY module-attribute

RELEASE_STALE_MESSAGES_QUERY = "\nUPDATE {table} SET status = 'pending'\nWHERE status = 'processing'\n    AND COALESCE(claimed_at, created_at) < now() - make_interval(secs => $1)\n    AND attempts < $2\n"

FAIL_EXHAUSTED_MESSAGES_QUERY module-attribute

FAIL_EXHAUSTED_MESSAGES_QUERY = "\nUPDATE {table} SET status = 'failed'\nWHERE status = 'processing'\n    AND COALESCE(claimed_at, created_at) < now() - make_interval(secs => $1)\n    AND attempts >= $2\nRETURNING id, task_id, task_name, attempts\n"

result_backend

PSQLPyResultBackend

PSQLPyResultBackend(
    dsn: Callable[[], str] | str | None = ...,
    keep_results: bool = ...,
    table_name: str = ...,
    field_for_task_id: Literal[
        "VarChar", "Text", "Uuid"
    ] = ...,
    serializer: TaskiqSerializer | None = ...,
    *,
    pool: None = ...,
    **connect_kwargs: Any,
)
PSQLPyResultBackend(
    dsn: Callable[[], str] | str | None = ...,
    keep_results: bool = ...,
    table_name: str = ...,
    field_for_task_id: Literal[
        "VarChar", "Text", "Uuid"
    ] = ...,
    serializer: TaskiqSerializer | None = ...,
    *,
    pool: ConnectionPool,
)
PSQLPyResultBackend(
    dsn="postgres://postgres:postgres@localhost:5432/postgres",
    keep_results=True,
    table_name="taskiq_results",
    field_for_task_id="VarChar",
    serializer=None,
    *,
    pool=None,
    **connect_kwargs,
)

Bases: BasePostgresResultBackend

Result backend for TaskIQ based on PSQLPy.

Construct a new PSQLPyResultBackend.

Parameters:

  • dsn (Callable[[], str] | str | None, default: 'postgres://postgres:postgres@localhost:5432/postgres' ) –

    PostgreSQL connection string or callable. Can be None if pool is provided.

  • keep_results (bool, default: True ) –

    Whether to keep results after reading.

  • table_name (str, default: 'taskiq_results' ) –

    Table to store results in.

  • field_for_task_id (Literal['VarChar', 'Text', 'Uuid'], default: 'VarChar' ) –

    Column type for task_id.

  • serializer (TaskiqSerializer | None, default: None ) –

    Serializer for task results.

  • pool (ConnectionPool | None, default: None ) –

    An existing connection pool to reuse.

  • **connect_kwargs (Any, default: {} ) –

    Extra kwargs for connection pool creation.

Source code in src/taskiq_pg/psqlpy/result_backend.py
def __init__(
    self,
    dsn: tp.Callable[[], str] | str | None = "postgres://postgres:postgres@localhost:5432/postgres",
    keep_results: bool = True,
    table_name: str = "taskiq_results",
    field_for_task_id: tp.Literal["VarChar", "Text", "Uuid"] = "VarChar",
    serializer: TaskiqSerializer | None = None,
    *,
    pool: ConnectionPool | None = None,
    **connect_kwargs: tp.Any,
) -> None:
    """
    Construct a new PSQLPyResultBackend.

    Args:
        dsn: PostgreSQL connection string or callable. Can be None if pool is provided.
        keep_results: Whether to keep results after reading.
        table_name: Table to store results in.
        field_for_task_id: Column type for task_id.
        serializer: Serializer for task results.
        pool: An existing connection pool to reuse.
        **connect_kwargs: Extra kwargs for connection pool creation.
    """
    self._owns_pool = True
    if pool is not None:
        self._owns_pool = False
        self._database_pool = pool

    super().__init__(
        dsn=dsn,
        keep_results=keep_results,
        table_name=table_name,
        field_for_task_id=field_for_task_id,
        serializer=serializer,
        **connect_kwargs,
    )
dsn property
dsn

Get the DSN string.

keep_results instance-attribute
keep_results = keep_results
table_name instance-attribute
table_name = table_name
field_for_task_id instance-attribute
field_for_task_id = field_for_task_id
connect_kwargs instance-attribute
connect_kwargs = connect_kwargs
serializer instance-attribute
serializer = serializer or PickleSerializer()
startup async
startup()

Initialize the result backend.

Construct new connection pool (if not provided externally) and create new table for results if not exists.

Source code in src/taskiq_pg/psqlpy/result_backend.py
async def startup(self) -> None:
    """
    Initialize the result backend.

    Construct new connection pool (if not provided externally) and create new table for results if not exists.
    """
    if self._owns_pool:
        self._database_pool = ConnectionPool(
            dsn=self.dsn,
            **self.connect_kwargs,
        )
    connection = await self._database_pool.connection()
    await connection.execute(
        querystring=queries.CREATE_TABLE_QUERY.format(
            self.table_name,
            self.field_for_task_id,
        ),
    )
    await connection.execute(
        querystring=queries.ADD_PROGRESS_COLUMN_QUERY.format(
            self.table_name,
        ),
    )
    await connection.execute(
        querystring=queries.CREATE_INDEX_QUERY.format(
            self.table_name,
            self.table_name,
        ),
    )
shutdown async
shutdown()

Close the connection pool if it was created by this result backend.

Source code in src/taskiq_pg/psqlpy/result_backend.py
async def shutdown(self) -> None:
    """Close the connection pool if it was created by this result backend."""
    if self._owns_pool and getattr(self, "_database_pool", None) is not None:
        self._database_pool.close()
set_result async
set_result(task_id, result)

Set result to the PostgreSQL table.

:param task_id: ID of the task. :param result: result of the task.

Source code in src/taskiq_pg/psqlpy/result_backend.py
async def set_result(
    self,
    task_id: str,
    result: TaskiqResult[ReturnType],
) -> None:
    """
    Set result to the PostgreSQL table.

    :param task_id: ID of the task.
    :param result: result of the task.
    """
    connection = await self._database_pool.connection()
    await connection.execute(
        querystring=queries.INSERT_RESULT_QUERY.format(
            self.table_name,
        ),
        parameters=[
            task_id,
            self.serializer.dumpb(result.model_dump(mode="json")),
        ],
    )
is_result_ready async
is_result_ready(task_id)

Returns whether the result is ready.

:param task_id: ID of the task.

:returns: True if the result is ready else False.

Source code in src/taskiq_pg/psqlpy/result_backend.py
async def is_result_ready(self, task_id: str) -> bool:
    """
    Returns whether the result is ready.

    :param task_id: ID of the task.

    :returns: True if the result is ready else False.
    """
    connection: tp.Final = await self._database_pool.connection()
    return tp.cast(
        "bool",
        await connection.fetch_val(
            querystring=queries.IS_RESULT_EXISTS_QUERY.format(
                self.table_name,
            ),
            parameters=[task_id],
        ),
    )
get_result async
get_result(task_id, with_logs=False)

Retrieve result from the task.

:param task_id: task's id. :param with_logs: if True it will download task's logs. :raises ResultIsMissingError: if there is no result when trying to get it. :return: TaskiqResult.

Source code in src/taskiq_pg/psqlpy/result_backend.py
async def get_result(
    self,
    task_id: str,
    with_logs: bool = False,
) -> TaskiqResult[ReturnType]:
    """
    Retrieve result from the task.

    :param task_id: task's id.
    :param with_logs: if True it will download task's logs.
    :raises ResultIsMissingError: if there is no result when trying to get it.
    :return: TaskiqResult.
    """
    connection: tp.Final = await self._database_pool.connection()
    try:
        result_in_bytes: tp.Final[bytes] = await connection.fetch_val(
            querystring=queries.SELECT_RESULT_QUERY.format(
                self.table_name,
            ),
            parameters=[task_id],
        )
    except BaseConnectionError as exc:
        msg = f"Cannot find record with task_id = {task_id} in PostgreSQL"
        raise ResultIsMissingError(msg) from exc

    if not self.keep_results:
        await connection.execute(
            querystring=queries.DELETE_RESULT_QUERY.format(
                self.table_name,
            ),
            parameters=[task_id],
        )

    taskiq_result: tp.Final = TaskiqResult[ReturnType].model_validate(
        self.serializer.loadb(result_in_bytes),
    )

    if not with_logs:
        taskiq_result.log = None

    return taskiq_result
set_progress async
set_progress(task_id, progress)

Saves progress.

:param task_id: task's id. :param progress: progress of execution.

Source code in src/taskiq_pg/psqlpy/result_backend.py
async def set_progress(
    self,
    task_id: str,
    progress: TaskProgress[tp.Any],
) -> None:
    """
    Saves progress.

    :param task_id: task's id.
    :param progress: progress of execution.
    """
    connection = await self._database_pool.connection()
    await connection.execute(
        querystring=queries.INSERT_PROGRESS_QUERY.format(
            self.table_name,
        ),
        parameters=[
            task_id,
            self.serializer.dumpb(progress.model_dump(mode="json")),
        ],
    )
get_progress async
get_progress(task_id)

Gets progress.

:param task_id: task's id.

Source code in src/taskiq_pg/psqlpy/result_backend.py
async def get_progress(
    self,
    task_id: str,
) -> TaskProgress[tp.Any] | None:
    """
    Gets progress.

    :param task_id: task's id.
    """
    connection: tp.Final = await self._database_pool.connection()
    try:
        progress_in_bytes = await connection.fetch_val(
            querystring=queries.SELECT_PROGRESS_QUERY.format(
                self.table_name,
            ),
            parameters=[task_id],
        )
    except BaseConnectionError as exc:
        msg = f"Cannot find record with task_id = {task_id} in PostgreSQL"
        raise ResultIsMissingError(msg) from exc
    if progress_in_bytes is None:
        return None
    return TaskProgress[tp.Any].model_validate(
        self.serializer.loadb(progress_in_bytes),
    )

schedule_source

logger module-attribute

logger = getLogger('taskiq_pg.psqlpy_schedule_source')

PSQLPyScheduleSource

PSQLPyScheduleSource(
    broker: AsyncBroker,
    dsn: str | Callable[[], str] = ...,
    table_name: str = ...,
    *,
    pool: None = ...,
    **connect_kwargs: Any,
)
PSQLPyScheduleSource(
    broker: AsyncBroker,
    dsn: str | Callable[[], str] = ...,
    table_name: str = ...,
    *,
    pool: ConnectionPool,
)
PSQLPyScheduleSource(
    broker,
    dsn="postgresql://postgres:postgres@localhost:5432/postgres",
    table_name="taskiq_schedules",
    *,
    pool=None,
    **connect_kwargs,
)

Bases: BasePostgresScheduleSource

Schedule source that uses psqlpy to store schedules in PostgreSQL.

Construct a new PSQLPyScheduleSource.

Parameters:

  • broker (AsyncBroker) –

    The TaskIQ broker instance.

  • dsn (str | Callable[[], str], default: 'postgresql://postgres:postgres@localhost:5432/postgres' ) –

    PostgreSQL connection string or callable. Ignored in pool mode.

  • table_name (str, default: 'taskiq_schedules' ) –

    Table to store schedules in.

  • pool (ConnectionPool | None, default: None ) –

    An existing connection pool to reuse.

  • **connect_kwargs (Any, default: {} ) –

    Extra kwargs for connection pool creation.

Source code in src/taskiq_pg/psqlpy/schedule_source.py
def __init__(
    self,
    broker: AsyncBroker,
    dsn: "str | tp.Callable[[], str]" = "postgresql://postgres:postgres@localhost:5432/postgres",
    table_name: str = "taskiq_schedules",
    *,
    pool: ConnectionPool | None = None,
    **connect_kwargs: tp.Any,
) -> None:
    """
    Construct a new PSQLPyScheduleSource.

    Args:
        broker: The TaskIQ broker instance.
        dsn: PostgreSQL connection string or callable. Ignored in pool mode.
        table_name: Table to store schedules in.
        pool: An existing connection pool to reuse.
        **connect_kwargs: Extra kwargs for connection pool creation.
    """
    if pool is not None:
        self._owns_pool = False
        self._database_pool = pool
    else:
        self._owns_pool = True

    super().__init__(
        broker=broker,
        dsn=dsn,
        table_name=table_name,
        **connect_kwargs,
    )
dsn property
dsn

Get the DSN string.

startup async
startup()

Initialize the schedule source.

Construct new connection pool (if not provided externally), create new table for schedules if not exists and fill table with schedules from task labels.

Source code in src/taskiq_pg/psqlpy/schedule_source.py
async def startup(self) -> None:
    """
    Initialize the schedule source.

    Construct new connection pool (if not provided externally), create new table for
    schedules if not exists and fill table with schedules from task labels.
    """
    if self._owns_pool:
        self._database_pool = ConnectionPool(
            dsn=self.dsn,
            **self._connect_kwargs,
        )
    async with self._database_pool.acquire() as connection:
        await connection.execute(
            CREATE_SCHEDULES_TABLE_QUERY.format(
                self._table_name,
            ),
        )
    scheduled_tasks_for_creation = self.extract_scheduled_tasks_from_broker()
    await self._update_schedules_on_startup(scheduled_tasks_for_creation)
shutdown async
shutdown()

Close the connection pool if was created by this schedule source.

Source code in src/taskiq_pg/psqlpy/schedule_source.py
async def shutdown(self) -> None:
    """Close the connection pool if was created by this schedule source."""
    if self._owns_pool and getattr(self, "_database_pool", None) is not None:
        self._database_pool.close()
get_schedules async
get_schedules()

Fetch schedules from the database.

Source code in src/taskiq_pg/psqlpy/schedule_source.py
async def get_schedules(self) -> list["ScheduledTask"]:
    """Fetch schedules from the database."""
    async with self._database_pool.acquire() as connection:
        rows_with_schedules = await connection.fetch(
            SELECT_SCHEDULES_QUERY.format(self._table_name),
        )
    schedules = []
    for row in rows_with_schedules.result():
        schedule = row["schedule"]
        schedules.append(
            ScheduledTask.model_validate(
                {
                    "schedule_id": str(row["id"]),
                    "task_name": row["task_name"],
                    "labels": schedule["labels"],
                    "args": schedule["args"],
                    "kwargs": schedule["kwargs"],
                    "cron": schedule["cron"],
                    "cron_offset": schedule["cron_offset"],
                    "time": schedule["time"],
                    "interval": schedule["interval"],
                },
            ),
        )
    return schedules
add_schedule async
add_schedule(schedule)

Add a new schedule.

Parameters:

  • schedule (ScheduledTask) –

    schedule to add.

Source code in src/taskiq_pg/psqlpy/schedule_source.py
async def add_schedule(self, schedule: "ScheduledTask") -> None:
    """
    Add a new schedule.

    Args:
        schedule: schedule to add.
    """
    async with self._database_pool.acquire() as connection:
        schedule_dict = schedule.model_dump(
            mode="json",
            exclude={"schedule_id", "task_name"},
        )
        await connection.execute(
            INSERT_SCHEDULE_QUERY.format(self._table_name),
            [
                uuid.UUID(schedule.schedule_id),
                schedule.task_name,
                JSONB(schedule_dict),
            ]
        )
delete_schedule async
delete_schedule(schedule_id)

Method to delete schedule by id.

This is useful for schedule cancelation.

Parameters:

  • schedule_id (str) –

    id of schedule to delete.

Source code in src/taskiq_pg/psqlpy/schedule_source.py
async def delete_schedule(self, schedule_id: str) -> None:
    """
    Method to delete schedule by id.

    This is useful for schedule cancelation.

    Args:
        schedule_id: id of schedule to delete.
    """
    async with self._database_pool.acquire() as connection:
        await connection.execute(
            DELETE_SCHEDULE_QUERY.format(self._table_name),
            [uuid.UUID(schedule_id)],
        )
post_send async
post_send(task)

Delete a task after it's completed.

Source code in src/taskiq_pg/psqlpy/schedule_source.py
async def post_send(self, task: ScheduledTask) -> None:
    """Delete a task after it's completed."""
    if task.time is not None:
        await self.delete_schedule(task.schedule_id)
extract_scheduled_tasks_from_broker
extract_scheduled_tasks_from_broker()

Extract schedules from tasks that were registered in broker.

Returns:

  • list[ScheduledTask] –

    A list of ScheduledTask instances extracted from the task's labels.

Source code in src/taskiq_pg/_internal/schedule_source.py
def extract_scheduled_tasks_from_broker(self) -> list[ScheduledTask]:
    """
    Extract schedules from tasks that were registered in broker.

    Returns:
        A list of ScheduledTask instances extracted from the task's labels.
    """
    scheduled_tasks_for_creation: list[ScheduledTask] = []
    for task_name, task in self._broker.get_all_tasks().items():
        if "schedule" not in task.labels:
            logger.debug("Task %s has no schedule, skipping", task_name)
            continue
        if not isinstance(task.labels["schedule"], list):
            logger.warning(
                "Schedule for task %s is not a list, skipping",
                task_name,
            )
            continue
        for schedule in task.labels["schedule"]:
            try:
                new_schedule = ScheduledTask.model_validate(
                    {
                        "task_name": task_name,
                        "labels": schedule.get("labels", {}),
                        "args": schedule.get("args", []),
                        "kwargs": schedule.get("kwargs", {}),
                        "schedule_id": str(uuid.uuid4()),
                        "cron": schedule.get("cron", None),
                        "cron_offset": schedule.get("cron_offset", None),
                        "interval": schedule.get("interval", None),
                        "time": schedule.get("time", None),
                    },
                )
                scheduled_tasks_for_creation.append(new_schedule)
            except ValidationError:  # noqa: PERF203
                logger.exception(
                    "Schedule for task %s is not valid, skipping",
                    task_name,
                )
                continue
    return scheduled_tasks_for_creation

psycopg

PsycopgBroker

PsycopgBroker(
    dsn: str | Callable[[], str] = ...,
    result_backend: AsyncResultBackend[_T] | None = ...,
    task_id_generator: Callable[[], str] | None = ...,
    channel_name: str = ...,
    table_name: str = ...,
    max_retry_attempts: int = ...,
    read_kwargs: dict[str, Any] | None = ...,
    write_kwargs: dict[str, Any] | None = ...,
    *,
    write_pool: None = ...,
    read_connection: None = ...,
    listen_keepalive_interval: float | None = ...,
    listen_keepalive_timeout: float = ...,
    poll_interval: float = ...,
    processing_timeout: float | None = ...,
)
PsycopgBroker(
    dsn: str | Callable[[], str] = ...,
    result_backend: AsyncResultBackend[_T] | None = ...,
    task_id_generator: Callable[[], str] | None = ...,
    channel_name: str = ...,
    table_name: str = ...,
    max_retry_attempts: int = ...,
    read_kwargs: dict[str, Any] | None = ...,
    write_kwargs: dict[str, Any] | None = ...,
    *,
    write_pool: AsyncConnectionPool,
    read_connection: None = ...,
    listen_keepalive_interval: float | None = ...,
    listen_keepalive_timeout: float = ...,
    poll_interval: float = ...,
    processing_timeout: float | None = ...,
)
PsycopgBroker(
    dsn: str | Callable[[], str] = ...,
    result_backend: AsyncResultBackend[_T] | None = ...,
    task_id_generator: Callable[[], str] | None = ...,
    channel_name: str = ...,
    table_name: str = ...,
    max_retry_attempts: int = ...,
    read_kwargs: dict[str, Any] | None = ...,
    write_kwargs: dict[str, Any] | None = ...,
    *,
    write_pool: None = ...,
    read_connection: AsyncConnection,
    listen_keepalive_interval: float | None = ...,
    listen_keepalive_timeout: float = ...,
    poll_interval: float = ...,
    processing_timeout: float | None = ...,
)
PsycopgBroker(
    dsn: str | Callable[[], str] = ...,
    result_backend: AsyncResultBackend[_T] | None = ...,
    task_id_generator: Callable[[], str] | None = ...,
    channel_name: str = ...,
    table_name: str = ...,
    max_retry_attempts: int = ...,
    read_kwargs: dict[str, Any] | None = ...,
    write_kwargs: dict[str, Any] | None = ...,
    *,
    write_pool: AsyncConnectionPool,
    read_connection: AsyncConnection,
    listen_keepalive_interval: float | None = ...,
    listen_keepalive_timeout: float = ...,
    poll_interval: float = ...,
    processing_timeout: float | None = ...,
)
PsycopgBroker(
    dsn="postgresql://postgres:postgres@localhost:5432/postgres",
    result_backend=None,
    task_id_generator=None,
    channel_name="taskiq",
    table_name="taskiq_messages",
    max_retry_attempts=5,
    read_kwargs=None,
    write_kwargs=None,
    *,
    write_pool=None,
    read_connection=None,
    listen_keepalive_interval=30.0,
    listen_keepalive_timeout=5.0,
    poll_interval=5.0,
    processing_timeout=None,
)

Bases: BasePostgresBroker

Broker that uses PostgreSQL and psycopg with LISTEN/NOTIFY.

Construct a new PsycopgBroker.

Parameters:

  • dsn (str | Callable[[], str], default: 'postgresql://postgres:postgres@localhost:5432/postgres' ) –

    PostgreSQL connection string or callable.

  • result_backend (AsyncResultBackend[_T] | None, default: None ) –

    Custom result backend.

  • task_id_generator (Callable[[], str] | None, default: None ) –

    Custom task_id generator.

  • channel_name (str, default: 'taskiq' ) –

    Name of the LISTEN/NOTIFY channel.

  • table_name (str, default: 'taskiq_messages' ) –

    Name of the table used to store messages.

  • max_retry_attempts (int, default: 5 ) –

    Maximum number of message processing attempts.

  • read_kwargs (dict[str, Any] | None, default: None ) –

    Extra kwargs forwarded to AsyncConnection.connect().

  • write_kwargs (dict[str, Any] | None, default: None ) –

    Extra kwargs forwarded to AsyncConnectionPool().

  • write_pool (AsyncConnectionPool | None, default: None ) –

    An existing connection pool to reuse for writes.

  • read_connection (AsyncConnection | None, default: None ) –

    An existing connection to reuse for LISTEN. Its lifecycle, including reconnection, remains the caller's responsibility.

  • listen_keepalive_interval (float | None, default: 30.0 ) –

    How often, in seconds, to check the broker-owned LISTEN connection with a query; None disables the check.

  • listen_keepalive_timeout (float, default: 5.0 ) –

    How long, in seconds, to wait for the check before reconnecting.

  • poll_interval (float, default: 5.0 ) –

    How often, in seconds, to look for pending messages whose NOTIFY was missed.

  • processing_timeout (float | None, default: None ) –

    Seconds after which an unacknowledged claimed message is delivered again (at-least-once delivery); None keeps at-most-once delivery.

Source code in src/taskiq_pg/psycopg/broker.py
def __init__(  # noqa: PLR0913
    self,
    dsn: str | Callable[[], str] = "postgresql://postgres:postgres@localhost:5432/postgres",
    result_backend: AsyncResultBackend[_T] | None = None,
    task_id_generator: Callable[[], str] | None = None,
    channel_name: str = "taskiq",
    table_name: str = "taskiq_messages",
    max_retry_attempts: int = 5,
    read_kwargs: dict[str, tp.Any] | None = None,
    write_kwargs: dict[str, tp.Any] | None = None,
    *,
    write_pool: AsyncConnectionPool | None = None,
    read_connection: AsyncConnection | None = None,
    listen_keepalive_interval: float | None = 30.0,
    listen_keepalive_timeout: float = 5.0,
    poll_interval: float = 5.0,
    processing_timeout: float | None = None,
) -> None:
    """
    Construct a new PsycopgBroker.

    Args:
        dsn: PostgreSQL connection string or callable.
        result_backend: Custom result backend.
        task_id_generator: Custom task_id generator.
        channel_name: Name of the LISTEN/NOTIFY channel.
        table_name: Name of the table used to store messages.
        max_retry_attempts: Maximum number of message processing attempts.
        read_kwargs: Extra kwargs forwarded to `AsyncConnection.connect()`.
        write_kwargs: Extra kwargs forwarded to `AsyncConnectionPool()`.
        write_pool: An existing connection pool to reuse for writes.
        read_connection: An existing connection to reuse for LISTEN.
            Its lifecycle, including reconnection, remains the caller's responsibility.
        listen_keepalive_interval: How often, in seconds, to check the broker-owned LISTEN connection
            with a query; None disables the check.
        listen_keepalive_timeout: How long, in seconds, to wait for the check before reconnecting.
        poll_interval: How often, in seconds, to look for pending messages whose NOTIFY was missed.
        processing_timeout: Seconds after which an unacknowledged claimed message is delivered again
            (at-least-once delivery); None keeps at-most-once delivery.
    """
    super().__init__(
        dsn=dsn,
        result_backend=result_backend,
        task_id_generator=task_id_generator,
        channel_name=channel_name,
        table_name=table_name,
        max_retry_attempts=max_retry_attempts,
        read_kwargs=read_kwargs,
        write_kwargs=write_kwargs,
        listen_keepalive_interval=listen_keepalive_interval,
        listen_keepalive_timeout=listen_keepalive_timeout,
        poll_interval=poll_interval,
        processing_timeout=processing_timeout,
    )

    self._owns_write_pool = True
    if write_pool is not None:
        self._write_pool = write_pool
        self._owns_write_pool = False

    self._owns_read_conn = True
    if read_connection is not None:
        self._read_conn = read_connection
        self._owns_read_conn = False

    self._notify_tasks: set[asyncio.Task[None]] = set()

dsn property

dsn

Get the DSN string.

channel_name instance-attribute

channel_name = channel_name

table_name instance-attribute

table_name = table_name

read_kwargs instance-attribute

read_kwargs = read_kwargs or {}

write_kwargs instance-attribute

write_kwargs = write_kwargs or {}

max_retry_attempts instance-attribute

max_retry_attempts = max_retry_attempts

listen_keepalive_interval instance-attribute

listen_keepalive_interval = listen_keepalive_interval

listen_keepalive_timeout instance-attribute

listen_keepalive_timeout = listen_keepalive_timeout

poll_interval instance-attribute

poll_interval = poll_interval

processing_timeout instance-attribute

processing_timeout = processing_timeout

startup async

startup()

Initialize the broker.

Source code in src/taskiq_pg/psycopg/broker.py
async def startup(self) -> None:
    """Initialize the broker."""
    await super().startup()
    self._closing = False

    if self._owns_write_pool:
        self._write_pool = AsyncConnectionPool(
            conninfo=self.dsn if self.dsn is not None else "",
            open=False,
            **self.write_kwargs,
        )

    if self._write_pool.closed:
        await self._write_pool.open()

    async with (
        self._write_pool.connection() as connection,
        connection.transaction(),
        connection.cursor() as cursor,
    ):
        await cursor.execute(schema_lock_query(self.table_name))
        # psycopg quotes the names in DDL, so they are quoted here too
        await cursor.execute(
            SCHEMA_STATUS_QUERY.format(
                table="quote_ident(%(table)s)",
                pending_index="quote_ident(%(pending_index)s)",
                processing_index="quote_ident(%(processing_index)s)",
            ),
            {
                "table": self.table_name,
                "pending_index": f"{self.table_name}_pending_idx",
                "processing_index": f"{self.table_name}_processing_idx",
            },
        )
        status = await cursor.fetchone()
        if status is None:
            msg = "schema status query returned no row"
            raise RuntimeError(msg)
        has_table, has_delivery_columns, has_pending_index, has_processing_index = status
        if not has_table:
            await cursor.execute(sql.SQL(CREATE_MESSAGE_TABLE_QUERY).format(sql.Identifier(self.table_name)))
        if not has_delivery_columns:
            await cursor.execute(
                sql.SQL(ADD_DELIVERY_COLUMNS_QUERY).format(table=sql.Identifier(self.table_name)),
            )
        if not has_pending_index:
            await cursor.execute(
                sql.SQL(CREATE_PENDING_INDEX_QUERY).format(
                    index=sql.Identifier(f"{self.table_name}_pending_idx"),
                    table=sql.Identifier(self.table_name),
                ),
            )
        if self.processing_timeout is not None and not has_processing_index:
            await cursor.execute(
                sql.SQL(CREATE_PROCESSING_INDEX_QUERY).format(
                    index=sql.Identifier(f"{self.table_name}_processing_idx"),
                    table=sql.Identifier(self.table_name),
                ),
            )

    if self._owns_read_conn:
        await self._connect_listener()
    else:
        await self._read_conn.execute(sql.SQL("LISTEN {}").format(sql.Identifier(self.channel_name)))
        self._notifies_iter = self._read_conn.notifies()

shutdown async

shutdown()

Close all connections on shutdown.

Source code in src/taskiq_pg/psycopg/broker.py
async def shutdown(self) -> None:
    """Close all connections on shutdown."""
    self._closing = True
    for task in list(self._notify_tasks):
        await cancel_and_wait(task)
    await super().shutdown()
    # startup() may have failed before creating any of these
    notifies_iter = getattr(self, "_notifies_iter", None)
    if notifies_iter is not None:
        with suppress(RuntimeError):  # RuntimeError: aclose(): asynchronous generator is already running
            await notifies_iter.aclose()
    if getattr(self, "_read_conn", None) is not None and self._owns_read_conn:
        await self._read_conn.notifies().aclose()
        await self._close_read_conn()
    if getattr(self, "_write_pool", None) is not None and self._owns_write_pool:
        await self._write_pool.close()

kick async

kick(message)

Send message to the channel.

Inserts the message into the database and sends a NOTIFY.

:param message: Message to send.

Source code in src/taskiq_pg/psycopg/broker.py
async def kick(self, message: BrokerMessage) -> None:
    """
    Send message to the channel.

    Inserts the message into the database and sends a NOTIFY.

    :param message: Message to send.
    """
    # Parse the delay before inserting, so a malformed label does not leave a message behind.
    delay_value = message.labels.get("delay")
    delay_seconds = None if delay_value is None else int(delay_value)

    async with self._write_pool.connection() as connection, connection.cursor() as cursor:
        # insert message into db table
        await cursor.execute(
            sql.SQL(INSERT_MESSAGE_QUERY).format(sql.Identifier(self.table_name)),
            [
                message.task_id,
                message.task_name,
                message.message.decode(),
                json.dumps(message.labels),
            ],
        )
        row = await cursor.fetchone()
        if row is None:
            msg = "failed to insert message"
            raise RuntimeError(msg)
        message_inserted_id = int(row[0])

        if delay_seconds is None:
            # Send NOTIFY with message ID as payload
            await cursor.execute(
                sql.SQL("NOTIFY {}, {}").format(
                    sql.Identifier(self.channel_name),
                    sql.Literal(str(message_inserted_id)),
                ),
            )
            return

    # The insert is committed once the connection is returned to the pool,
    # so a delayed NOTIFY sent from here never reaches a worker before the row is visible.
    # The NOTIFY is only the fast path: workers also poll for messages whose delay has elapsed,
    # so the message is delivered even if this producer exits before sending it.
    task = asyncio.create_task(self._schedule_notification(message_inserted_id, delay_seconds))
    self._notify_tasks.add(task)
    task.add_done_callback(self._notify_tasks.discard)

listen async

listen()

Listen to the channel.

Yields messages as they are received.

:yields: AckableMessage instances.

Source code in src/taskiq_pg/psycopg/broker.py
async def listen(self) -> AsyncGenerator[AckableMessage, None]:
    """
    Listen to the channel.

    Yields messages as they are received.

    :yields: AckableMessage instances.
    """
    async for payload in self._listen_context():
        if payload == POLL_NOW:
            await self._release_stale_messages()
            while (claimed := await self._claim_next_ready()) is not None:
                yield self._ackable(claimed)
            continue
        claimed = await self._claim(int(payload))  # payload is the message id
        if claimed is not None:
            yield self._ackable(claimed)

PsycopgResultBackend

PsycopgResultBackend(
    dsn: Callable[[], str] | str | None = ...,
    keep_results: bool = ...,
    table_name: str = ...,
    field_for_task_id: Literal[
        "VarChar", "Text", "Uuid"
    ] = ...,
    serializer: TaskiqSerializer | None = ...,
    *,
    pool: None = ...,
    **connect_kwargs: Any,
)
PsycopgResultBackend(
    dsn: Callable[[], str] | str | None = ...,
    keep_results: bool = ...,
    table_name: str = ...,
    field_for_task_id: Literal[
        "VarChar", "Text", "Uuid"
    ] = ...,
    serializer: TaskiqSerializer | None = ...,
    *,
    pool: AsyncConnectionPool,
)
PsycopgResultBackend(
    dsn="postgres://postgres:postgres@localhost:5432/postgres",
    keep_results=True,
    table_name="taskiq_results",
    field_for_task_id="VarChar",
    serializer=None,
    *,
    pool=None,
    **connect_kwargs,
)

Bases: BasePostgresResultBackend

Result backend for TaskIQ based on psycopg.

Construct a new PsycopgResultBackend.

Parameters:

  • dsn (Callable[[], str] | str | None, default: 'postgres://postgres:postgres@localhost:5432/postgres' ) –

    PostgreSQL connection string or callable. Can be None if pool is provided.

  • keep_results (bool, default: True ) –

    Whether to keep results after reading.

  • table_name (str, default: 'taskiq_results' ) –

    Table to store results in.

  • field_for_task_id (Literal['VarChar', 'Text', 'Uuid'], default: 'VarChar' ) –

    Column type for task_id.

  • serializer (TaskiqSerializer | None, default: None ) –

    Serializer for task results.

  • pool (AsyncConnectionPool | None, default: None ) –

    An existing connection pool to reuse.

  • **connect_kwargs (Any, default: {} ) –

    Extra kwargs for connection pool creation.

Source code in src/taskiq_pg/psycopg/result_backend.py
def __init__(
    self,
    dsn: tp.Callable[[], str] | str | None = "postgres://postgres:postgres@localhost:5432/postgres",
    keep_results: bool = True,
    table_name: str = "taskiq_results",
    field_for_task_id: tp.Literal["VarChar", "Text", "Uuid"] = "VarChar",
    serializer: TaskiqSerializer | None = None,
    *,
    pool: AsyncConnectionPool | None = None,
    **connect_kwargs: tp.Any,
) -> None:
    """
    Construct a new PsycopgResultBackend.

    Args:
        dsn: PostgreSQL connection string or callable. Can be None if pool is provided.
        keep_results: Whether to keep results after reading.
        table_name: Table to store results in.
        field_for_task_id: Column type for task_id.
        serializer: Serializer for task results.
        pool: An existing connection pool to reuse.
        **connect_kwargs: Extra kwargs for connection pool creation.
    """
    self._owns_pool = True
    if pool is not None:
        self._owns_pool = False
        self._database_pool = pool

    super().__init__(
        dsn=dsn,
        keep_results=keep_results,
        table_name=table_name,
        field_for_task_id=field_for_task_id,
        serializer=serializer,
        **connect_kwargs,
    )

dsn property

dsn

Get the DSN string.

keep_results instance-attribute

keep_results = keep_results

table_name instance-attribute

table_name = table_name

field_for_task_id instance-attribute

field_for_task_id = field_for_task_id

connect_kwargs instance-attribute

connect_kwargs = connect_kwargs

serializer instance-attribute

serializer = serializer or PickleSerializer()

startup async

startup()

Initialize the result backend.

Construct new connection pool (if not provided externally) and create new table for results if not exists.

Source code in src/taskiq_pg/psycopg/result_backend.py
async def startup(self) -> None:
    """
    Initialize the result backend.

    Construct new connection pool (if not provided externally) and create new table
    for results if not exists.
    """
    if self._owns_pool:
        self._database_pool = AsyncConnectionPool(
            conninfo=self.dsn if self.dsn is not None else "",
            open=False,
            **self.connect_kwargs,
        )

    if self._database_pool.closed:
        await self._database_pool.open()

    async with self._database_pool.connection() as connection, connection.cursor() as cursor:
        await cursor.execute(
            query=sql.SQL(queries.CREATE_TABLE_QUERY).format(
                sql.Identifier(self.table_name),
                sql.SQL(self.field_for_task_id),
            ),
        )
        await cursor.execute(
            query=sql.SQL(queries.ADD_PROGRESS_COLUMN_QUERY).format(
                sql.Identifier(self.table_name),
            ),
        )
        await cursor.execute(
            query=sql.SQL(queries.CREATE_INDEX_QUERY).format(
                sql.Identifier(self.table_name + "_task_id_idx"),
                sql.Identifier(self.table_name),
            ),
        )

shutdown async

shutdown()

Close the connection pool (only if owned by this backend).

Source code in src/taskiq_pg/psycopg/result_backend.py
async def shutdown(self) -> None:
    """Close the connection pool (only if owned by this backend)."""
    if self._owns_pool and getattr(self, "_database_pool", None) is not None:
        await self._database_pool.close()

set_result async

set_result(task_id, result)

Set result to the PostgreSQL table.

:param task_id: ID of the task. :param result: result of the task.

Source code in src/taskiq_pg/psycopg/result_backend.py
async def set_result(
    self,
    task_id: str,
    result: TaskiqResult[ReturnType],
) -> None:
    """
    Set result to the PostgreSQL table.

    :param task_id: ID of the task.
    :param result: result of the task.
    """
    async with self._database_pool.connection() as connection, connection.cursor() as cursor:
        await cursor.execute(
            query=sql.SQL(queries.INSERT_RESULT_QUERY).format(
                sql.Identifier(self.table_name),
            ),
            params=[
                task_id,
                self.serializer.dumpb(result.model_dump(mode="json")),
            ],
        )

is_result_ready async

is_result_ready(task_id)

Returns whether the result is ready.

:param task_id: ID of the task.

:returns: True if the result is ready else False.

Source code in src/taskiq_pg/psycopg/result_backend.py
async def is_result_ready(self, task_id: str) -> bool:
    """
    Returns whether the result is ready.

    :param task_id: ID of the task.

    :returns: True if the result is ready else False.
    """
    async with self._database_pool.connection() as connection, connection.cursor() as cursor:
        execute_result = await cursor.execute(
            query=sql.SQL(queries.IS_RESULT_EXISTS_QUERY).format(
                sql.Identifier(self.table_name),
            ),
            params=[task_id],
        )
        row = await execute_result.fetchone()
        return bool(row and row[0])

get_result async

get_result(task_id, with_logs=False)

Retrieve result from the task.

:param task_id: task's id. :param with_logs: if True it will download task's logs. :raises ResultIsMissingError: if there is no result when trying to get it. :return: TaskiqResult.

Source code in src/taskiq_pg/psycopg/result_backend.py
async def get_result(
    self,
    task_id: str,
    with_logs: bool = False,
) -> TaskiqResult[ReturnType]:
    """
    Retrieve result from the task.

    :param task_id: task's id.
    :param with_logs: if True it will download task's logs.
    :raises ResultIsMissingError: if there is no result when trying to get it.
    :return: TaskiqResult.
    """
    async with self._database_pool.connection() as connection, connection.cursor() as cursor:
        execute_result = await cursor.execute(
            query=sql.SQL(queries.SELECT_RESULT_QUERY).format(
                sql.Identifier(self.table_name),
            ),
            params=[task_id],
        )
        result = await execute_result.fetchone()
        if result is None:
            msg = f"Cannot find record with task_id = {task_id} in PostgreSQL"
            raise ResultIsMissingError(msg)
        result_in_bytes: tp.Final = result[0]

        if not self.keep_results:
            await cursor.execute(
                query=sql.SQL(queries.DELETE_RESULT_QUERY).format(
                    sql.Identifier(self.table_name),
                ),
                params=[task_id],
            )

        taskiq_result: tp.Final = TaskiqResult[ReturnType].model_validate(
            self.serializer.loadb(result_in_bytes),
        )

        if not with_logs:
            taskiq_result.log = None

        return taskiq_result

set_progress async

set_progress(task_id, progress)

Saves progress.

:param task_id: task's id. :param progress: progress of execution.

Source code in src/taskiq_pg/psycopg/result_backend.py
async def set_progress(
    self,
    task_id: str,
    progress: TaskProgress[tp.Any],
) -> None:
    """
    Saves progress.

    :param task_id: task's id.
    :param progress: progress of execution.
    """
    async with self._database_pool.connection() as connection, connection.cursor() as cursor:
        await cursor.execute(
            query=sql.SQL(queries.INSERT_PROGRESS_QUERY).format(
                sql.Identifier(self.table_name),
            ),
            params=[
                task_id,
                self.serializer.dumpb(progress.model_dump(mode="json")),
                self.serializer.dumpb(progress.model_dump(mode="json")),
            ],
        )

get_progress async

get_progress(task_id)

Gets progress.

:param task_id: task's id.

Source code in src/taskiq_pg/psycopg/result_backend.py
async def get_progress(
    self,
    task_id: str,
) -> TaskProgress[tp.Any] | None:
    """
    Gets progress.

    :param task_id: task's id.
    """
    async with self._database_pool.connection() as connection, connection.cursor() as cursor:
        execute_result = await cursor.execute(
            query=sql.SQL(queries.SELECT_PROGRESS_QUERY).format(
                sql.Identifier(self.table_name),
            ),
            params=[task_id],
        )
        progress_in_bytes = await execute_result.fetchone()
        if progress_in_bytes is None or progress_in_bytes[0] is None:
            return None
        return TaskProgress[tp.Any].model_validate(
            self.serializer.loadb(progress_in_bytes[0]),
        )

PsycopgScheduleSource

PsycopgScheduleSource(
    broker: AsyncBroker,
    dsn: str | Callable[[], str] = ...,
    table_name: str = ...,
    *,
    pool: None = ...,
    **connect_kwargs: Any,
)
PsycopgScheduleSource(
    broker: AsyncBroker,
    dsn: str | Callable[[], str] = ...,
    table_name: str = ...,
    *,
    pool: AsyncConnectionPool,
)
PsycopgScheduleSource(
    broker,
    dsn="postgresql://postgres:postgres@localhost:5432/postgres",
    table_name="taskiq_schedules",
    *,
    pool=None,
    **connect_kwargs,
)

Bases: BasePostgresScheduleSource

Schedule source that uses psycopg to store schedules in PostgreSQL.

Construct a new PsycopgScheduleSource.

Parameters:

  • broker (AsyncBroker) –

    The TaskIQ broker instance.

  • dsn (str | Callable[[], str], default: 'postgresql://postgres:postgres@localhost:5432/postgres' ) –

    PostgreSQL connection string or callable. Ignored in pool mode.

  • table_name (str, default: 'taskiq_schedules' ) –

    Table to store schedules in.

  • pool (AsyncConnectionPool | None, default: None ) –

    An existing connection pool to reuse.

  • **connect_kwargs (Any, default: {} ) –

    Extra kwargs for connection pool creation.

Source code in src/taskiq_pg/psycopg/schedule_source.py
def __init__(
    self,
    broker: AsyncBroker,
    dsn: str | tp.Callable[[], str] = "postgresql://postgres:postgres@localhost:5432/postgres",
    table_name: str = "taskiq_schedules",
    *,
    pool: AsyncConnectionPool | None = None,
    **connect_kwargs: tp.Any,
) -> None:
    """
    Construct a new PsycopgScheduleSource.

    Args:
        broker: The TaskIQ broker instance.
        dsn: PostgreSQL connection string or callable. Ignored in pool mode.
        table_name: Table to store schedules in.
        pool: An existing connection pool to reuse.
        **connect_kwargs: Extra kwargs for connection pool creation.

    """
    self._owns_pool = True
    if pool is not None:
        self._owns_pool = False
        self._database_pool = pool

    super().__init__(
        broker=broker,
        dsn=dsn,
        table_name=table_name,
        **connect_kwargs,
    )

dsn property

dsn

Get the DSN string.

extract_scheduled_tasks_from_broker

extract_scheduled_tasks_from_broker()

Extract schedules from tasks that were registered in broker.

Returns:

  • list[ScheduledTask] –

    A list of ScheduledTask instances extracted from the task's labels.

Source code in src/taskiq_pg/_internal/schedule_source.py
def extract_scheduled_tasks_from_broker(self) -> list[ScheduledTask]:
    """
    Extract schedules from tasks that were registered in broker.

    Returns:
        A list of ScheduledTask instances extracted from the task's labels.
    """
    scheduled_tasks_for_creation: list[ScheduledTask] = []
    for task_name, task in self._broker.get_all_tasks().items():
        if "schedule" not in task.labels:
            logger.debug("Task %s has no schedule, skipping", task_name)
            continue
        if not isinstance(task.labels["schedule"], list):
            logger.warning(
                "Schedule for task %s is not a list, skipping",
                task_name,
            )
            continue
        for schedule in task.labels["schedule"]:
            try:
                new_schedule = ScheduledTask.model_validate(
                    {
                        "task_name": task_name,
                        "labels": schedule.get("labels", {}),
                        "args": schedule.get("args", []),
                        "kwargs": schedule.get("kwargs", {}),
                        "schedule_id": str(uuid.uuid4()),
                        "cron": schedule.get("cron", None),
                        "cron_offset": schedule.get("cron_offset", None),
                        "interval": schedule.get("interval", None),
                        "time": schedule.get("time", None),
                    },
                )
                scheduled_tasks_for_creation.append(new_schedule)
            except ValidationError:  # noqa: PERF203
                logger.exception(
                    "Schedule for task %s is not valid, skipping",
                    task_name,
                )
                continue
    return scheduled_tasks_for_creation

startup async

startup()

Initialize the schedule source.

Construct new connection pool (if not provided externally), create new table for schedules if not exists and fill table with schedules from task labels.

Source code in src/taskiq_pg/psycopg/schedule_source.py
async def startup(self) -> None:
    """
    Initialize the schedule source.

    Construct new connection pool (if not provided externally), create new table for
    schedules if not exists and fill table with schedules from task labels.
    """
    if self._owns_pool:
        self._database_pool = AsyncConnectionPool(
            conninfo=self.dsn if self.dsn is not None else "",
            open=False,
            **self._connect_kwargs,
        )

    if self._database_pool.closed:
        await self._database_pool.open()

    async with self._database_pool.connection() as connection, connection.cursor() as cursor:
        await cursor.execute(
            sql.SQL(CREATE_SCHEDULES_TABLE_QUERY).format(sql.Identifier(self._table_name)),
        )
    scheduled_tasks_for_creation = self.extract_scheduled_tasks_from_broker()
    await self._update_schedules_on_startup(scheduled_tasks_for_creation)

shutdown async

shutdown()

Close the connection pool (only if owned by this source).

Source code in src/taskiq_pg/psycopg/schedule_source.py
async def shutdown(self) -> None:
    """Close the connection pool (only if owned by this source)."""
    if self._owns_pool and getattr(self, "_database_pool", None) is not None:
        await self._database_pool.close()

get_schedules async

get_schedules()

Fetch schedules from the database.

Source code in src/taskiq_pg/psycopg/schedule_source.py
async def get_schedules(self) -> list["ScheduledTask"]:
    """Fetch schedules from the database."""
    schedules = []
    async with self._database_pool.connection() as connection, connection.cursor() as cursor:
        rows_with_schedules = await cursor.execute(
            sql.SQL(SELECT_SCHEDULES_QUERY).format(sql.Identifier(self._table_name)),
        )
        rows = await rows_with_schedules.fetchall()
        for schedule_id, task_name, schedule in rows:
            schedules.append(
                ScheduledTask.model_validate(
                    {
                        "schedule_id": str(schedule_id),
                        "task_name": task_name,
                        "labels": schedule["labels"],
                        "args": schedule["args"],
                        "kwargs": schedule["kwargs"],
                        "cron": schedule["cron"],
                        "cron_offset": schedule["cron_offset"],
                        "time": schedule["time"],
                        "interval": schedule["interval"],
                    },
                ),
            )
    return schedules

add_schedule async

add_schedule(schedule)

Add a new schedule.

Parameters:

  • schedule (ScheduledTask) –

    schedule to add.

Source code in src/taskiq_pg/psycopg/schedule_source.py
async def add_schedule(self, schedule: "ScheduledTask") -> None:
    """
    Add a new schedule.

    Args:
        schedule: schedule to add.
    """
    async with self._database_pool.connection() as connection, connection.cursor() as cursor:
        await cursor.execute(
            sql.SQL(INSERT_SCHEDULE_QUERY).format(sql.Identifier(self._table_name)),
            [
                uuid.UUID(schedule.schedule_id),
                schedule.task_name,
                schedule.model_dump_json(
                    exclude={"schedule_id", "task_name"},
                ),
            ],
        )

delete_schedule async

delete_schedule(schedule_id)

Method to delete schedule by id.

This is useful for schedule cancelation.

Parameters:

  • schedule_id (str) –

    id of schedule to delete.

Source code in src/taskiq_pg/psycopg/schedule_source.py
async def delete_schedule(self, schedule_id: str) -> None:
    """
    Method to delete schedule by id.

    This is useful for schedule cancelation.

    Args:
        schedule_id: id of schedule to delete.
    """
    async with self._database_pool.connection() as connection, connection.cursor() as cursor:
        await cursor.execute(
            sql.SQL(DELETE_SCHEDULE_QUERY).format(sql.Identifier(self._table_name)),
            [schedule_id],
        )

post_send async

post_send(task)

Delete a task after it's completed.

Source code in src/taskiq_pg/psycopg/schedule_source.py
async def post_send(self, task: ScheduledTask) -> None:
    """Delete a task after it's completed."""
    if task.time is not None:
        await self.delete_schedule(task.schedule_id)

broker

logger module-attribute

logger = logging.getLogger('taskiq.psycopg_broker')

PsycopgBroker

PsycopgBroker(
    dsn: str | Callable[[], str] = ...,
    result_backend: AsyncResultBackend[_T] | None = ...,
    task_id_generator: Callable[[], str] | None = ...,
    channel_name: str = ...,
    table_name: str = ...,
    max_retry_attempts: int = ...,
    read_kwargs: dict[str, Any] | None = ...,
    write_kwargs: dict[str, Any] | None = ...,
    *,
    write_pool: None = ...,
    read_connection: None = ...,
    listen_keepalive_interval: float | None = ...,
    listen_keepalive_timeout: float = ...,
    poll_interval: float = ...,
    processing_timeout: float | None = ...,
)
PsycopgBroker(
    dsn: str | Callable[[], str] = ...,
    result_backend: AsyncResultBackend[_T] | None = ...,
    task_id_generator: Callable[[], str] | None = ...,
    channel_name: str = ...,
    table_name: str = ...,
    max_retry_attempts: int = ...,
    read_kwargs: dict[str, Any] | None = ...,
    write_kwargs: dict[str, Any] | None = ...,
    *,
    write_pool: AsyncConnectionPool,
    read_connection: None = ...,
    listen_keepalive_interval: float | None = ...,
    listen_keepalive_timeout: float = ...,
    poll_interval: float = ...,
    processing_timeout: float | None = ...,
)
PsycopgBroker(
    dsn: str | Callable[[], str] = ...,
    result_backend: AsyncResultBackend[_T] | None = ...,
    task_id_generator: Callable[[], str] | None = ...,
    channel_name: str = ...,
    table_name: str = ...,
    max_retry_attempts: int = ...,
    read_kwargs: dict[str, Any] | None = ...,
    write_kwargs: dict[str, Any] | None = ...,
    *,
    write_pool: None = ...,
    read_connection: AsyncConnection,
    listen_keepalive_interval: float | None = ...,
    listen_keepalive_timeout: float = ...,
    poll_interval: float = ...,
    processing_timeout: float | None = ...,
)
PsycopgBroker(
    dsn: str | Callable[[], str] = ...,
    result_backend: AsyncResultBackend[_T] | None = ...,
    task_id_generator: Callable[[], str] | None = ...,
    channel_name: str = ...,
    table_name: str = ...,
    max_retry_attempts: int = ...,
    read_kwargs: dict[str, Any] | None = ...,
    write_kwargs: dict[str, Any] | None = ...,
    *,
    write_pool: AsyncConnectionPool,
    read_connection: AsyncConnection,
    listen_keepalive_interval: float | None = ...,
    listen_keepalive_timeout: float = ...,
    poll_interval: float = ...,
    processing_timeout: float | None = ...,
)
PsycopgBroker(
    dsn="postgresql://postgres:postgres@localhost:5432/postgres",
    result_backend=None,
    task_id_generator=None,
    channel_name="taskiq",
    table_name="taskiq_messages",
    max_retry_attempts=5,
    read_kwargs=None,
    write_kwargs=None,
    *,
    write_pool=None,
    read_connection=None,
    listen_keepalive_interval=30.0,
    listen_keepalive_timeout=5.0,
    poll_interval=5.0,
    processing_timeout=None,
)

Bases: BasePostgresBroker

Broker that uses PostgreSQL and psycopg with LISTEN/NOTIFY.

Construct a new PsycopgBroker.

Parameters:

  • dsn (str | Callable[[], str], default: 'postgresql://postgres:postgres@localhost:5432/postgres' ) –

    PostgreSQL connection string or callable.

  • result_backend (AsyncResultBackend[_T] | None, default: None ) –

    Custom result backend.

  • task_id_generator (Callable[[], str] | None, default: None ) –

    Custom task_id generator.

  • channel_name (str, default: 'taskiq' ) –

    Name of the LISTEN/NOTIFY channel.

  • table_name (str, default: 'taskiq_messages' ) –

    Name of the table used to store messages.

  • max_retry_attempts (int, default: 5 ) –

    Maximum number of message processing attempts.

  • read_kwargs (dict[str, Any] | None, default: None ) –

    Extra kwargs forwarded to AsyncConnection.connect().

  • write_kwargs (dict[str, Any] | None, default: None ) –

    Extra kwargs forwarded to AsyncConnectionPool().

  • write_pool (AsyncConnectionPool | None, default: None ) –

    An existing connection pool to reuse for writes.

  • read_connection (AsyncConnection | None, default: None ) –

    An existing connection to reuse for LISTEN. Its lifecycle, including reconnection, remains the caller's responsibility.

  • listen_keepalive_interval (float | None, default: 30.0 ) –

    How often, in seconds, to check the broker-owned LISTEN connection with a query; None disables the check.

  • listen_keepalive_timeout (float, default: 5.0 ) –

    How long, in seconds, to wait for the check before reconnecting.

  • poll_interval (float, default: 5.0 ) –

    How often, in seconds, to look for pending messages whose NOTIFY was missed.

  • processing_timeout (float | None, default: None ) –

    Seconds after which an unacknowledged claimed message is delivered again (at-least-once delivery); None keeps at-most-once delivery.

Source code in src/taskiq_pg/psycopg/broker.py
def __init__(  # noqa: PLR0913
    self,
    dsn: str | Callable[[], str] = "postgresql://postgres:postgres@localhost:5432/postgres",
    result_backend: AsyncResultBackend[_T] | None = None,
    task_id_generator: Callable[[], str] | None = None,
    channel_name: str = "taskiq",
    table_name: str = "taskiq_messages",
    max_retry_attempts: int = 5,
    read_kwargs: dict[str, tp.Any] | None = None,
    write_kwargs: dict[str, tp.Any] | None = None,
    *,
    write_pool: AsyncConnectionPool | None = None,
    read_connection: AsyncConnection | None = None,
    listen_keepalive_interval: float | None = 30.0,
    listen_keepalive_timeout: float = 5.0,
    poll_interval: float = 5.0,
    processing_timeout: float | None = None,
) -> None:
    """
    Construct a new PsycopgBroker.

    Args:
        dsn: PostgreSQL connection string or callable.
        result_backend: Custom result backend.
        task_id_generator: Custom task_id generator.
        channel_name: Name of the LISTEN/NOTIFY channel.
        table_name: Name of the table used to store messages.
        max_retry_attempts: Maximum number of message processing attempts.
        read_kwargs: Extra kwargs forwarded to `AsyncConnection.connect()`.
        write_kwargs: Extra kwargs forwarded to `AsyncConnectionPool()`.
        write_pool: An existing connection pool to reuse for writes.
        read_connection: An existing connection to reuse for LISTEN.
            Its lifecycle, including reconnection, remains the caller's responsibility.
        listen_keepalive_interval: How often, in seconds, to check the broker-owned LISTEN connection
            with a query; None disables the check.
        listen_keepalive_timeout: How long, in seconds, to wait for the check before reconnecting.
        poll_interval: How often, in seconds, to look for pending messages whose NOTIFY was missed.
        processing_timeout: Seconds after which an unacknowledged claimed message is delivered again
            (at-least-once delivery); None keeps at-most-once delivery.
    """
    super().__init__(
        dsn=dsn,
        result_backend=result_backend,
        task_id_generator=task_id_generator,
        channel_name=channel_name,
        table_name=table_name,
        max_retry_attempts=max_retry_attempts,
        read_kwargs=read_kwargs,
        write_kwargs=write_kwargs,
        listen_keepalive_interval=listen_keepalive_interval,
        listen_keepalive_timeout=listen_keepalive_timeout,
        poll_interval=poll_interval,
        processing_timeout=processing_timeout,
    )

    self._owns_write_pool = True
    if write_pool is not None:
        self._write_pool = write_pool
        self._owns_write_pool = False

    self._owns_read_conn = True
    if read_connection is not None:
        self._read_conn = read_connection
        self._owns_read_conn = False

    self._notify_tasks: set[asyncio.Task[None]] = set()
dsn property
dsn

Get the DSN string.

channel_name instance-attribute
channel_name = channel_name
table_name instance-attribute
table_name = table_name
read_kwargs instance-attribute
read_kwargs = read_kwargs or {}
write_kwargs instance-attribute
write_kwargs = write_kwargs or {}
max_retry_attempts instance-attribute
max_retry_attempts = max_retry_attempts
listen_keepalive_interval instance-attribute
listen_keepalive_interval = listen_keepalive_interval
listen_keepalive_timeout instance-attribute
listen_keepalive_timeout = listen_keepalive_timeout
poll_interval instance-attribute
poll_interval = poll_interval
processing_timeout instance-attribute
processing_timeout = processing_timeout
startup async
startup()

Initialize the broker.

Source code in src/taskiq_pg/psycopg/broker.py
async def startup(self) -> None:
    """Initialize the broker."""
    await super().startup()
    self._closing = False

    if self._owns_write_pool:
        self._write_pool = AsyncConnectionPool(
            conninfo=self.dsn if self.dsn is not None else "",
            open=False,
            **self.write_kwargs,
        )

    if self._write_pool.closed:
        await self._write_pool.open()

    async with (
        self._write_pool.connection() as connection,
        connection.transaction(),
        connection.cursor() as cursor,
    ):
        await cursor.execute(schema_lock_query(self.table_name))
        # psycopg quotes the names in DDL, so they are quoted here too
        await cursor.execute(
            SCHEMA_STATUS_QUERY.format(
                table="quote_ident(%(table)s)",
                pending_index="quote_ident(%(pending_index)s)",
                processing_index="quote_ident(%(processing_index)s)",
            ),
            {
                "table": self.table_name,
                "pending_index": f"{self.table_name}_pending_idx",
                "processing_index": f"{self.table_name}_processing_idx",
            },
        )
        status = await cursor.fetchone()
        if status is None:
            msg = "schema status query returned no row"
            raise RuntimeError(msg)
        has_table, has_delivery_columns, has_pending_index, has_processing_index = status
        if not has_table:
            await cursor.execute(sql.SQL(CREATE_MESSAGE_TABLE_QUERY).format(sql.Identifier(self.table_name)))
        if not has_delivery_columns:
            await cursor.execute(
                sql.SQL(ADD_DELIVERY_COLUMNS_QUERY).format(table=sql.Identifier(self.table_name)),
            )
        if not has_pending_index:
            await cursor.execute(
                sql.SQL(CREATE_PENDING_INDEX_QUERY).format(
                    index=sql.Identifier(f"{self.table_name}_pending_idx"),
                    table=sql.Identifier(self.table_name),
                ),
            )
        if self.processing_timeout is not None and not has_processing_index:
            await cursor.execute(
                sql.SQL(CREATE_PROCESSING_INDEX_QUERY).format(
                    index=sql.Identifier(f"{self.table_name}_processing_idx"),
                    table=sql.Identifier(self.table_name),
                ),
            )

    if self._owns_read_conn:
        await self._connect_listener()
    else:
        await self._read_conn.execute(sql.SQL("LISTEN {}").format(sql.Identifier(self.channel_name)))
        self._notifies_iter = self._read_conn.notifies()
shutdown async
shutdown()

Close all connections on shutdown.

Source code in src/taskiq_pg/psycopg/broker.py
async def shutdown(self) -> None:
    """Close all connections on shutdown."""
    self._closing = True
    for task in list(self._notify_tasks):
        await cancel_and_wait(task)
    await super().shutdown()
    # startup() may have failed before creating any of these
    notifies_iter = getattr(self, "_notifies_iter", None)
    if notifies_iter is not None:
        with suppress(RuntimeError):  # RuntimeError: aclose(): asynchronous generator is already running
            await notifies_iter.aclose()
    if getattr(self, "_read_conn", None) is not None and self._owns_read_conn:
        await self._read_conn.notifies().aclose()
        await self._close_read_conn()
    if getattr(self, "_write_pool", None) is not None and self._owns_write_pool:
        await self._write_pool.close()
kick async
kick(message)

Send message to the channel.

Inserts the message into the database and sends a NOTIFY.

:param message: Message to send.

Source code in src/taskiq_pg/psycopg/broker.py
async def kick(self, message: BrokerMessage) -> None:
    """
    Send message to the channel.

    Inserts the message into the database and sends a NOTIFY.

    :param message: Message to send.
    """
    # Parse the delay before inserting, so a malformed label does not leave a message behind.
    delay_value = message.labels.get("delay")
    delay_seconds = None if delay_value is None else int(delay_value)

    async with self._write_pool.connection() as connection, connection.cursor() as cursor:
        # insert message into db table
        await cursor.execute(
            sql.SQL(INSERT_MESSAGE_QUERY).format(sql.Identifier(self.table_name)),
            [
                message.task_id,
                message.task_name,
                message.message.decode(),
                json.dumps(message.labels),
            ],
        )
        row = await cursor.fetchone()
        if row is None:
            msg = "failed to insert message"
            raise RuntimeError(msg)
        message_inserted_id = int(row[0])

        if delay_seconds is None:
            # Send NOTIFY with message ID as payload
            await cursor.execute(
                sql.SQL("NOTIFY {}, {}").format(
                    sql.Identifier(self.channel_name),
                    sql.Literal(str(message_inserted_id)),
                ),
            )
            return

    # The insert is committed once the connection is returned to the pool,
    # so a delayed NOTIFY sent from here never reaches a worker before the row is visible.
    # The NOTIFY is only the fast path: workers also poll for messages whose delay has elapsed,
    # so the message is delivered even if this producer exits before sending it.
    task = asyncio.create_task(self._schedule_notification(message_inserted_id, delay_seconds))
    self._notify_tasks.add(task)
    task.add_done_callback(self._notify_tasks.discard)
listen async
listen()

Listen to the channel.

Yields messages as they are received.

:yields: AckableMessage instances.

Source code in src/taskiq_pg/psycopg/broker.py
async def listen(self) -> AsyncGenerator[AckableMessage, None]:
    """
    Listen to the channel.

    Yields messages as they are received.

    :yields: AckableMessage instances.
    """
    async for payload in self._listen_context():
        if payload == POLL_NOW:
            await self._release_stale_messages()
            while (claimed := await self._claim_next_ready()) is not None:
                yield self._ackable(claimed)
            continue
        claimed = await self._claim(int(payload))  # payload is the message id
        if claimed is not None:
            yield self._ackable(claimed)

queries

CREATE_TABLE_QUERY module-attribute

CREATE_TABLE_QUERY = "\nCREATE TABLE IF NOT EXISTS {} (\n    task_id {} UNIQUE,\n    result BYTEA,\n    progress BYTEA\n)\n"

ADD_PROGRESS_COLUMN_QUERY module-attribute

ADD_PROGRESS_COLUMN_QUERY = "\nALTER TABLE {} ADD COLUMN IF NOT EXISTS progress BYTEA;\n"

CREATE_INDEX_QUERY module-attribute

CREATE_INDEX_QUERY = "\nCREATE INDEX IF NOT EXISTS {} ON {} USING HASH (task_id)\n"

INSERT_RESULT_QUERY module-attribute

INSERT_RESULT_QUERY = "\nINSERT INTO {} VALUES (%s, %s)\nON CONFLICT (task_id)\nDO UPDATE\nSET result = EXCLUDED.result;\n"

INSERT_PROGRESS_QUERY module-attribute

INSERT_PROGRESS_QUERY = "\nINSERT INTO {} VALUES (%s, NULL, %s)\nON CONFLICT (task_id)\nDO UPDATE\nSET progress = %s\n"

SELECT_PROGRESS_QUERY module-attribute

SELECT_PROGRESS_QUERY = (
    "\nSELECT progress FROM {} WHERE task_id = %s\n"
)

IS_RESULT_EXISTS_QUERY module-attribute

IS_RESULT_EXISTS_QUERY = "\nSELECT EXISTS(\n    SELECT 1 FROM {} WHERE task_id = %s and result IS NOT NULL\n);\n"

SELECT_RESULT_QUERY module-attribute

SELECT_RESULT_QUERY = (
    "\nSELECT result FROM {} WHERE task_id = %s;\n"
)

DELETE_RESULT_QUERY module-attribute

DELETE_RESULT_QUERY = (
    "\nDELETE FROM {} WHERE task_id = %s;\n"
)

CREATE_MESSAGE_TABLE_QUERY module-attribute

CREATE_MESSAGE_TABLE_QUERY = "\nCREATE TABLE IF NOT EXISTS {} (\n    id SERIAL PRIMARY KEY,\n    task_id VARCHAR NOT NULL,\n    task_name VARCHAR NOT NULL,\n    message TEXT NOT NULL,\n    labels JSONB NOT NULL,\n    status TEXT NOT NULL DEFAULT 'pending',\n    created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW()\n);\n"

INSERT_MESSAGE_QUERY module-attribute

INSERT_MESSAGE_QUERY = "\nINSERT INTO {} (task_id, task_name, message, labels)\nVALUES (%s, %s, %s, %s)\nRETURNING id\n"

CLAIM_MESSAGE_QUERY module-attribute

CLAIM_MESSAGE_QUERY = "\nUPDATE {} SET status = 'processing', claimed_at = now(), attempts = attempts + 1\nWHERE id = %s AND status = 'pending'\nRETURNING id, message, attempts\n"

DELETE_MESSAGE_QUERY module-attribute

DELETE_MESSAGE_QUERY = (
    "DELETE FROM {} WHERE id = %s AND attempts = %s"
)

CREATE_SCHEDULES_TABLE_QUERY module-attribute

CREATE_SCHEDULES_TABLE_QUERY = "\nCREATE TABLE IF NOT EXISTS {} (\n    id UUID PRIMARY KEY,\n    task_name VARCHAR(100) NOT NULL,\n    schedule JSONB NOT NULL,\n    created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),\n    updated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW()\n);\n"

INSERT_SCHEDULE_QUERY module-attribute

INSERT_SCHEDULE_QUERY = "\nINSERT INTO {} (id, task_name, schedule)\nVALUES (%s, %s, %s)\nON CONFLICT (id) DO UPDATE\nSET task_name = EXCLUDED.task_name,\n    schedule = EXCLUDED.schedule,\n    updated_at = NOW();\n"

SELECT_SCHEDULES_QUERY module-attribute

SELECT_SCHEDULES_QUERY = (
    "\nSELECT id, task_name, schedule\nFROM {};\n"
)

DELETE_ALL_SCHEDULES_QUERY module-attribute

DELETE_ALL_SCHEDULES_QUERY = '\nDELETE FROM {};\n'

DELETE_SCHEDULE_QUERY module-attribute

DELETE_SCHEDULE_QUERY = '\nDELETE FROM {} WHERE id = %s;\n'

CREATE_PENDING_INDEX_QUERY module-attribute

CREATE_PENDING_INDEX_QUERY = "CREATE INDEX IF NOT EXISTS {index} ON {table} (id) WHERE status = 'pending'"

CLAIM_NEXT_READY_MESSAGE_QUERY module-attribute

CLAIM_NEXT_READY_MESSAGE_QUERY = "\nUPDATE {table} SET status = 'processing', claimed_at = now(), attempts = attempts + 1\nWHERE id = (\n    SELECT id FROM {table}\n    WHERE status = 'pending'\n        AND created_at + make_interval(secs => CASE\n            -- a label that is not a plain number must not break the query for every other row\n            WHEN labels->>'delay' ~ '^[0-9]{{1,10}}(\\.[0-9]+)?$' THEN (labels->>'delay')::double precision\n            WHEN labels->>'delay' ~ '^[0-9]+(\\.[0-9]+)?$' THEN 1e10  -- over 300 years: never ready\n            ELSE 0\n        END) <= now()\n    ORDER BY id\n    LIMIT 1\n    FOR UPDATE SKIP LOCKED\n)\nRETURNING id, message, attempts\n"

ADD_DELIVERY_COLUMNS_QUERY module-attribute

ADD_DELIVERY_COLUMNS_QUERY = "\nALTER TABLE {table}\n    ADD COLUMN IF NOT EXISTS claimed_at TIMESTAMP WITH TIME ZONE,\n    ADD COLUMN IF NOT EXISTS attempts INTEGER NOT NULL DEFAULT 0\n"

CREATE_PROCESSING_INDEX_QUERY module-attribute

CREATE_PROCESSING_INDEX_QUERY = "CREATE INDEX IF NOT EXISTS {index} ON {table} (claimed_at) WHERE status = 'processing'"

RELEASE_STALE_MESSAGES_QUERY module-attribute

RELEASE_STALE_MESSAGES_QUERY = "\nUPDATE {table} SET status = 'pending'\nWHERE status = 'processing'\n    AND COALESCE(claimed_at, created_at) < now() - make_interval(secs => %s)\n    AND attempts < %s\n"

FAIL_EXHAUSTED_MESSAGES_QUERY module-attribute

FAIL_EXHAUSTED_MESSAGES_QUERY = "\nUPDATE {table} SET status = 'failed'\nWHERE status = 'processing'\n    AND COALESCE(claimed_at, created_at) < now() - make_interval(secs => %s)\n    AND attempts >= %s\nRETURNING id, task_id, task_name, attempts\n"

result_backend

PsycopgResultBackend

PsycopgResultBackend(
    dsn: Callable[[], str] | str | None = ...,
    keep_results: bool = ...,
    table_name: str = ...,
    field_for_task_id: Literal[
        "VarChar", "Text", "Uuid"
    ] = ...,
    serializer: TaskiqSerializer | None = ...,
    *,
    pool: None = ...,
    **connect_kwargs: Any,
)
PsycopgResultBackend(
    dsn: Callable[[], str] | str | None = ...,
    keep_results: bool = ...,
    table_name: str = ...,
    field_for_task_id: Literal[
        "VarChar", "Text", "Uuid"
    ] = ...,
    serializer: TaskiqSerializer | None = ...,
    *,
    pool: AsyncConnectionPool,
)
PsycopgResultBackend(
    dsn="postgres://postgres:postgres@localhost:5432/postgres",
    keep_results=True,
    table_name="taskiq_results",
    field_for_task_id="VarChar",
    serializer=None,
    *,
    pool=None,
    **connect_kwargs,
)

Bases: BasePostgresResultBackend

Result backend for TaskIQ based on psycopg.

Construct a new PsycopgResultBackend.

Parameters:

  • dsn (Callable[[], str] | str | None, default: 'postgres://postgres:postgres@localhost:5432/postgres' ) –

    PostgreSQL connection string or callable. Can be None if pool is provided.

  • keep_results (bool, default: True ) –

    Whether to keep results after reading.

  • table_name (str, default: 'taskiq_results' ) –

    Table to store results in.

  • field_for_task_id (Literal['VarChar', 'Text', 'Uuid'], default: 'VarChar' ) –

    Column type for task_id.

  • serializer (TaskiqSerializer | None, default: None ) –

    Serializer for task results.

  • pool (AsyncConnectionPool | None, default: None ) –

    An existing connection pool to reuse.

  • **connect_kwargs (Any, default: {} ) –

    Extra kwargs for connection pool creation.

Source code in src/taskiq_pg/psycopg/result_backend.py
def __init__(
    self,
    dsn: tp.Callable[[], str] | str | None = "postgres://postgres:postgres@localhost:5432/postgres",
    keep_results: bool = True,
    table_name: str = "taskiq_results",
    field_for_task_id: tp.Literal["VarChar", "Text", "Uuid"] = "VarChar",
    serializer: TaskiqSerializer | None = None,
    *,
    pool: AsyncConnectionPool | None = None,
    **connect_kwargs: tp.Any,
) -> None:
    """
    Construct a new PsycopgResultBackend.

    Args:
        dsn: PostgreSQL connection string or callable. Can be None if pool is provided.
        keep_results: Whether to keep results after reading.
        table_name: Table to store results in.
        field_for_task_id: Column type for task_id.
        serializer: Serializer for task results.
        pool: An existing connection pool to reuse.
        **connect_kwargs: Extra kwargs for connection pool creation.
    """
    self._owns_pool = True
    if pool is not None:
        self._owns_pool = False
        self._database_pool = pool

    super().__init__(
        dsn=dsn,
        keep_results=keep_results,
        table_name=table_name,
        field_for_task_id=field_for_task_id,
        serializer=serializer,
        **connect_kwargs,
    )
dsn property
dsn

Get the DSN string.

keep_results instance-attribute
keep_results = keep_results
table_name instance-attribute
table_name = table_name
field_for_task_id instance-attribute
field_for_task_id = field_for_task_id
connect_kwargs instance-attribute
connect_kwargs = connect_kwargs
serializer instance-attribute
serializer = serializer or PickleSerializer()
startup async
startup()

Initialize the result backend.

Construct new connection pool (if not provided externally) and create new table for results if not exists.

Source code in src/taskiq_pg/psycopg/result_backend.py
async def startup(self) -> None:
    """
    Initialize the result backend.

    Construct new connection pool (if not provided externally) and create new table
    for results if not exists.
    """
    if self._owns_pool:
        self._database_pool = AsyncConnectionPool(
            conninfo=self.dsn if self.dsn is not None else "",
            open=False,
            **self.connect_kwargs,
        )

    if self._database_pool.closed:
        await self._database_pool.open()

    async with self._database_pool.connection() as connection, connection.cursor() as cursor:
        await cursor.execute(
            query=sql.SQL(queries.CREATE_TABLE_QUERY).format(
                sql.Identifier(self.table_name),
                sql.SQL(self.field_for_task_id),
            ),
        )
        await cursor.execute(
            query=sql.SQL(queries.ADD_PROGRESS_COLUMN_QUERY).format(
                sql.Identifier(self.table_name),
            ),
        )
        await cursor.execute(
            query=sql.SQL(queries.CREATE_INDEX_QUERY).format(
                sql.Identifier(self.table_name + "_task_id_idx"),
                sql.Identifier(self.table_name),
            ),
        )
shutdown async
shutdown()

Close the connection pool (only if owned by this backend).

Source code in src/taskiq_pg/psycopg/result_backend.py
async def shutdown(self) -> None:
    """Close the connection pool (only if owned by this backend)."""
    if self._owns_pool and getattr(self, "_database_pool", None) is not None:
        await self._database_pool.close()
set_result async
set_result(task_id, result)

Set result to the PostgreSQL table.

:param task_id: ID of the task. :param result: result of the task.

Source code in src/taskiq_pg/psycopg/result_backend.py
async def set_result(
    self,
    task_id: str,
    result: TaskiqResult[ReturnType],
) -> None:
    """
    Set result to the PostgreSQL table.

    :param task_id: ID of the task.
    :param result: result of the task.
    """
    async with self._database_pool.connection() as connection, connection.cursor() as cursor:
        await cursor.execute(
            query=sql.SQL(queries.INSERT_RESULT_QUERY).format(
                sql.Identifier(self.table_name),
            ),
            params=[
                task_id,
                self.serializer.dumpb(result.model_dump(mode="json")),
            ],
        )
is_result_ready async
is_result_ready(task_id)

Returns whether the result is ready.

:param task_id: ID of the task.

:returns: True if the result is ready else False.

Source code in src/taskiq_pg/psycopg/result_backend.py
async def is_result_ready(self, task_id: str) -> bool:
    """
    Returns whether the result is ready.

    :param task_id: ID of the task.

    :returns: True if the result is ready else False.
    """
    async with self._database_pool.connection() as connection, connection.cursor() as cursor:
        execute_result = await cursor.execute(
            query=sql.SQL(queries.IS_RESULT_EXISTS_QUERY).format(
                sql.Identifier(self.table_name),
            ),
            params=[task_id],
        )
        row = await execute_result.fetchone()
        return bool(row and row[0])
get_result async
get_result(task_id, with_logs=False)

Retrieve result from the task.

:param task_id: task's id. :param with_logs: if True it will download task's logs. :raises ResultIsMissingError: if there is no result when trying to get it. :return: TaskiqResult.

Source code in src/taskiq_pg/psycopg/result_backend.py
async def get_result(
    self,
    task_id: str,
    with_logs: bool = False,
) -> TaskiqResult[ReturnType]:
    """
    Retrieve result from the task.

    :param task_id: task's id.
    :param with_logs: if True it will download task's logs.
    :raises ResultIsMissingError: if there is no result when trying to get it.
    :return: TaskiqResult.
    """
    async with self._database_pool.connection() as connection, connection.cursor() as cursor:
        execute_result = await cursor.execute(
            query=sql.SQL(queries.SELECT_RESULT_QUERY).format(
                sql.Identifier(self.table_name),
            ),
            params=[task_id],
        )
        result = await execute_result.fetchone()
        if result is None:
            msg = f"Cannot find record with task_id = {task_id} in PostgreSQL"
            raise ResultIsMissingError(msg)
        result_in_bytes: tp.Final = result[0]

        if not self.keep_results:
            await cursor.execute(
                query=sql.SQL(queries.DELETE_RESULT_QUERY).format(
                    sql.Identifier(self.table_name),
                ),
                params=[task_id],
            )

        taskiq_result: tp.Final = TaskiqResult[ReturnType].model_validate(
            self.serializer.loadb(result_in_bytes),
        )

        if not with_logs:
            taskiq_result.log = None

        return taskiq_result
set_progress async
set_progress(task_id, progress)

Saves progress.

:param task_id: task's id. :param progress: progress of execution.

Source code in src/taskiq_pg/psycopg/result_backend.py
async def set_progress(
    self,
    task_id: str,
    progress: TaskProgress[tp.Any],
) -> None:
    """
    Saves progress.

    :param task_id: task's id.
    :param progress: progress of execution.
    """
    async with self._database_pool.connection() as connection, connection.cursor() as cursor:
        await cursor.execute(
            query=sql.SQL(queries.INSERT_PROGRESS_QUERY).format(
                sql.Identifier(self.table_name),
            ),
            params=[
                task_id,
                self.serializer.dumpb(progress.model_dump(mode="json")),
                self.serializer.dumpb(progress.model_dump(mode="json")),
            ],
        )
get_progress async
get_progress(task_id)

Gets progress.

:param task_id: task's id.

Source code in src/taskiq_pg/psycopg/result_backend.py
async def get_progress(
    self,
    task_id: str,
) -> TaskProgress[tp.Any] | None:
    """
    Gets progress.

    :param task_id: task's id.
    """
    async with self._database_pool.connection() as connection, connection.cursor() as cursor:
        execute_result = await cursor.execute(
            query=sql.SQL(queries.SELECT_PROGRESS_QUERY).format(
                sql.Identifier(self.table_name),
            ),
            params=[task_id],
        )
        progress_in_bytes = await execute_result.fetchone()
        if progress_in_bytes is None or progress_in_bytes[0] is None:
            return None
        return TaskProgress[tp.Any].model_validate(
            self.serializer.loadb(progress_in_bytes[0]),
        )

schedule_source

logger module-attribute

logger = getLogger('taskiq_pg.psycopg_schedule_source')

PsycopgScheduleSource

PsycopgScheduleSource(
    broker: AsyncBroker,
    dsn: str | Callable[[], str] = ...,
    table_name: str = ...,
    *,
    pool: None = ...,
    **connect_kwargs: Any,
)
PsycopgScheduleSource(
    broker: AsyncBroker,
    dsn: str | Callable[[], str] = ...,
    table_name: str = ...,
    *,
    pool: AsyncConnectionPool,
)
PsycopgScheduleSource(
    broker,
    dsn="postgresql://postgres:postgres@localhost:5432/postgres",
    table_name="taskiq_schedules",
    *,
    pool=None,
    **connect_kwargs,
)

Bases: BasePostgresScheduleSource

Schedule source that uses psycopg to store schedules in PostgreSQL.

Construct a new PsycopgScheduleSource.

Parameters:

  • broker (AsyncBroker) –

    The TaskIQ broker instance.

  • dsn (str | Callable[[], str], default: 'postgresql://postgres:postgres@localhost:5432/postgres' ) –

    PostgreSQL connection string or callable. Ignored in pool mode.

  • table_name (str, default: 'taskiq_schedules' ) –

    Table to store schedules in.

  • pool (AsyncConnectionPool | None, default: None ) –

    An existing connection pool to reuse.

  • **connect_kwargs (Any, default: {} ) –

    Extra kwargs for connection pool creation.

Source code in src/taskiq_pg/psycopg/schedule_source.py
def __init__(
    self,
    broker: AsyncBroker,
    dsn: str | tp.Callable[[], str] = "postgresql://postgres:postgres@localhost:5432/postgres",
    table_name: str = "taskiq_schedules",
    *,
    pool: AsyncConnectionPool | None = None,
    **connect_kwargs: tp.Any,
) -> None:
    """
    Construct a new PsycopgScheduleSource.

    Args:
        broker: The TaskIQ broker instance.
        dsn: PostgreSQL connection string or callable. Ignored in pool mode.
        table_name: Table to store schedules in.
        pool: An existing connection pool to reuse.
        **connect_kwargs: Extra kwargs for connection pool creation.

    """
    self._owns_pool = True
    if pool is not None:
        self._owns_pool = False
        self._database_pool = pool

    super().__init__(
        broker=broker,
        dsn=dsn,
        table_name=table_name,
        **connect_kwargs,
    )
dsn property
dsn

Get the DSN string.

startup async
startup()

Initialize the schedule source.

Construct new connection pool (if not provided externally), create new table for schedules if not exists and fill table with schedules from task labels.

Source code in src/taskiq_pg/psycopg/schedule_source.py
async def startup(self) -> None:
    """
    Initialize the schedule source.

    Construct new connection pool (if not provided externally), create new table for
    schedules if not exists and fill table with schedules from task labels.
    """
    if self._owns_pool:
        self._database_pool = AsyncConnectionPool(
            conninfo=self.dsn if self.dsn is not None else "",
            open=False,
            **self._connect_kwargs,
        )

    if self._database_pool.closed:
        await self._database_pool.open()

    async with self._database_pool.connection() as connection, connection.cursor() as cursor:
        await cursor.execute(
            sql.SQL(CREATE_SCHEDULES_TABLE_QUERY).format(sql.Identifier(self._table_name)),
        )
    scheduled_tasks_for_creation = self.extract_scheduled_tasks_from_broker()
    await self._update_schedules_on_startup(scheduled_tasks_for_creation)
shutdown async
shutdown()

Close the connection pool (only if owned by this source).

Source code in src/taskiq_pg/psycopg/schedule_source.py
async def shutdown(self) -> None:
    """Close the connection pool (only if owned by this source)."""
    if self._owns_pool and getattr(self, "_database_pool", None) is not None:
        await self._database_pool.close()
get_schedules async
get_schedules()

Fetch schedules from the database.

Source code in src/taskiq_pg/psycopg/schedule_source.py
async def get_schedules(self) -> list["ScheduledTask"]:
    """Fetch schedules from the database."""
    schedules = []
    async with self._database_pool.connection() as connection, connection.cursor() as cursor:
        rows_with_schedules = await cursor.execute(
            sql.SQL(SELECT_SCHEDULES_QUERY).format(sql.Identifier(self._table_name)),
        )
        rows = await rows_with_schedules.fetchall()
        for schedule_id, task_name, schedule in rows:
            schedules.append(
                ScheduledTask.model_validate(
                    {
                        "schedule_id": str(schedule_id),
                        "task_name": task_name,
                        "labels": schedule["labels"],
                        "args": schedule["args"],
                        "kwargs": schedule["kwargs"],
                        "cron": schedule["cron"],
                        "cron_offset": schedule["cron_offset"],
                        "time": schedule["time"],
                        "interval": schedule["interval"],
                    },
                ),
            )
    return schedules
add_schedule async
add_schedule(schedule)

Add a new schedule.

Parameters:

  • schedule (ScheduledTask) –

    schedule to add.

Source code in src/taskiq_pg/psycopg/schedule_source.py
async def add_schedule(self, schedule: "ScheduledTask") -> None:
    """
    Add a new schedule.

    Args:
        schedule: schedule to add.
    """
    async with self._database_pool.connection() as connection, connection.cursor() as cursor:
        await cursor.execute(
            sql.SQL(INSERT_SCHEDULE_QUERY).format(sql.Identifier(self._table_name)),
            [
                uuid.UUID(schedule.schedule_id),
                schedule.task_name,
                schedule.model_dump_json(
                    exclude={"schedule_id", "task_name"},
                ),
            ],
        )
delete_schedule async
delete_schedule(schedule_id)

Method to delete schedule by id.

This is useful for schedule cancelation.

Parameters:

  • schedule_id (str) –

    id of schedule to delete.

Source code in src/taskiq_pg/psycopg/schedule_source.py
async def delete_schedule(self, schedule_id: str) -> None:
    """
    Method to delete schedule by id.

    This is useful for schedule cancelation.

    Args:
        schedule_id: id of schedule to delete.
    """
    async with self._database_pool.connection() as connection, connection.cursor() as cursor:
        await cursor.execute(
            sql.SQL(DELETE_SCHEDULE_QUERY).format(sql.Identifier(self._table_name)),
            [schedule_id],
        )
post_send async
post_send(task)

Delete a task after it's completed.

Source code in src/taskiq_pg/psycopg/schedule_source.py
async def post_send(self, task: ScheduledTask) -> None:
    """Delete a task after it's completed."""
    if task.time is not None:
        await self.delete_schedule(task.schedule_id)
extract_scheduled_tasks_from_broker
extract_scheduled_tasks_from_broker()

Extract schedules from tasks that were registered in broker.

Returns:

  • list[ScheduledTask] –

    A list of ScheduledTask instances extracted from the task's labels.

Source code in src/taskiq_pg/_internal/schedule_source.py
def extract_scheduled_tasks_from_broker(self) -> list[ScheduledTask]:
    """
    Extract schedules from tasks that were registered in broker.

    Returns:
        A list of ScheduledTask instances extracted from the task's labels.
    """
    scheduled_tasks_for_creation: list[ScheduledTask] = []
    for task_name, task in self._broker.get_all_tasks().items():
        if "schedule" not in task.labels:
            logger.debug("Task %s has no schedule, skipping", task_name)
            continue
        if not isinstance(task.labels["schedule"], list):
            logger.warning(
                "Schedule for task %s is not a list, skipping",
                task_name,
            )
            continue
        for schedule in task.labels["schedule"]:
            try:
                new_schedule = ScheduledTask.model_validate(
                    {
                        "task_name": task_name,
                        "labels": schedule.get("labels", {}),
                        "args": schedule.get("args", []),
                        "kwargs": schedule.get("kwargs", {}),
                        "schedule_id": str(uuid.uuid4()),
                        "cron": schedule.get("cron", None),
                        "cron_offset": schedule.get("cron_offset", None),
                        "interval": schedule.get("interval", None),
                        "time": schedule.get("time", None),
                    },
                )
                scheduled_tasks_for_creation.append(new_schedule)
            except ValidationError:  # noqa: PERF203
                logger.exception(
                    "Schedule for task %s is not valid, skipping",
                    task_name,
                )
                continue
    return scheduled_tasks_for_creation