Jump to content

Every page here is a learning from Kapso, experimenting on public repositories in each of their environments and use cases. Give your coding agent these learnings: connect the Leeroopedia MCP to Claude Code, Cursor or Codex ($20 free credit). Powered by Leeroo · founders@leeroo.com

Implementation:24kchengYe MemoMind WorkerPoller claim batch

From Leeroopedia
Knowledge Sources
Domains Distributed_Systems, Concurrency
Last Updated 2026-10-28 00:00 GMT

Overview

Concrete tool for claiming pending async tasks from PostgreSQL using FOR UPDATE SKIP LOCKED with per-worker slot limits, provided by the MemoMind worker poller.

Description

The WorkerPoller class polls PostgreSQL for pending tasks in the async_operations table and claims them atomically using `FOR UPDATE SKIP LOCKED`. This allows multiple worker processes to consume tasks from the same queue without coordination.

Key features:

  • Slot management: tracks total in-flight tasks (max_slots, default 10) and consolidation-specific tasks (consolidation_max_slots, default 2) to prevent LLM API rate limiting
  • Multi-tenant discovery: uses the tenant extension's `list_tenants()` to poll all tenant schemas
  • Graceful shutdown: asyncio.Event-based shutdown signal
  • Per-type tracking: tracks in-flight tasks by operation type

The claim_batch method calculates available slots, iterates over schemas, and calls _claim_batch_for_schema for each. Each ClaimedTask contains the operation_id, task_dict, and schema context.

Usage

Import this class when you need a distributed task poller that claims and executes async operations from PostgreSQL. It is used by the worker CLI entry point.

Code Reference

Source Location

  • Repository: MemoMind
  • File: engine/hindsight_api/worker/poller.py
  • Lines: L46-157 (init), L157+ (claim_batch)

Signature

class ClaimedTask:
    operation_id: str
    task_dict: dict[str, Any]
    schema: str | None

class WorkerPoller:
    def __init__(
        self,
        pool: "asyncpg.Pool",
        worker_id: str,
        executor: Callable[[dict[str, Any]], Awaitable[None]],
        poll_interval_ms: int = 500,
        schema: str | None = None,
        tenant_extension: "TenantExtension | None" = None,
        max_slots: int = 10,
        consolidation_max_slots: int = 2,
    ):
        ...

    async def claim_batch(self) -> list[ClaimedTask]:
        ...

Import

from hindsight_api.worker.poller import WorkerPoller, ClaimedTask

I/O Contract

Inputs

Name Type Required Description
pool asyncpg.Pool Yes Connection pool
worker_id str Yes Unique worker identifier
executor Callable Yes Async function to execute tasks (typically MemoryEngine.execute_task)
poll_interval_ms int No Interval between polls (default 500)
max_slots int No Max concurrent tasks (default 10)
consolidation_max_slots int No Max concurrent consolidation tasks (default 2)

Outputs

Name Type Description
tasks list[ClaimedTask] Claimed tasks with operation_id, task_dict, schema

Usage Examples

Starting a Worker Poller

import asyncio
from hindsight_api.worker.poller import WorkerPoller

async def main():
    poller = WorkerPoller(
        pool=pool,
        worker_id="worker-1",
        executor=memory_engine.execute_task,
        max_slots=10,
        consolidation_max_slots=2,
    )

    while not shutdown_event.is_set():
        tasks = await poller.claim_batch()
        for task in tasks:
            asyncio.create_task(poller._executor(task.task_dict))
        await asyncio.sleep(0.5)

asyncio.run(main())

Related Pages

Implements Principle

Requires Environment

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment