Refactor code structure for improved readability and maintainability
This commit is contained in:
49
memory_gateway/worker_v2.py
Normal file
49
memory_gateway/worker_v2.py
Normal file
@ -0,0 +1,49 @@
|
||||
"""Lightweight v2 outbox worker entrypoint.
|
||||
|
||||
Usage:
|
||||
python -m memory_gateway.worker_v2 --limit 100 --worker-id local-worker --lease-seconds 300
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import argparse
|
||||
import asyncio
|
||||
import json
|
||||
from typing import Sequence
|
||||
from uuid import uuid4
|
||||
|
||||
from .services_v2 import v2_service
|
||||
|
||||
|
||||
def build_parser() -> argparse.ArgumentParser:
|
||||
parser = argparse.ArgumentParser(description="Process Memory Gateway v2 outbox events once.")
|
||||
parser.add_argument("--limit", type=int, default=100, help="Maximum pending events to claim and process.")
|
||||
parser.add_argument("--worker-id", default=None, help="Stable worker id recorded in outbox lease fields.")
|
||||
parser.add_argument("--lease-seconds", type=int, default=300, help="Lease duration for claimed events.")
|
||||
return parser
|
||||
|
||||
|
||||
async def run_once(limit: int, worker_id: str | None, lease_seconds: int) -> dict[str, object]:
|
||||
worker_id = worker_id or f"worker_{uuid4().hex[:12]}"
|
||||
response = await v2_service.process_pending_outbox_events_summary(
|
||||
limit=limit,
|
||||
worker_id=worker_id,
|
||||
lease_seconds=lease_seconds,
|
||||
)
|
||||
return response.model_dump(mode="json")
|
||||
|
||||
|
||||
def main(argv: Sequence[str] | None = None) -> int:
|
||||
args = build_parser().parse_args(argv)
|
||||
payload = asyncio.run(
|
||||
run_once(
|
||||
limit=args.limit,
|
||||
worker_id=args.worker_id,
|
||||
lease_seconds=args.lease_seconds,
|
||||
)
|
||||
)
|
||||
print(json.dumps(payload, ensure_ascii=False, sort_keys=True))
|
||||
return 0
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
raise SystemExit(main())
|
||||
Reference in New Issue
Block a user