|
1 | 1 | """ |
2 | 2 | NNG hub: central control plane, task dispatcher, and lease manager. |
3 | 3 |
|
4 | | -Run as a standalone process:: |
| 4 | +Run as a taskiq sub-command:: |
5 | 5 |
|
6 | | - taskiq-nng-hub --control-addr ipc:///tmp/taskiq-nng.ipc |
| 6 | + taskiq nng-hub --control-addr ipc:///tmp/taskiq-nng.ipc |
7 | 7 |
|
8 | 8 | Or embed it in an application for testing:: |
9 | 9 |
|
|
14 | 14 | """ |
15 | 15 | from __future__ import annotations |
16 | 16 |
|
17 | | -import argparse |
18 | 17 | import asyncio |
19 | 18 | import base64 |
20 | 19 | import logging |
21 | | -import os |
22 | | -import signal |
23 | 20 | import time |
24 | 21 | import uuid |
25 | 22 | from contextlib import suppress |
@@ -369,91 +366,3 @@ async def _reaper_loop(self) -> None: |
369 | 366 | raise |
370 | 367 | except Exception: |
371 | 368 | logger.exception("Reaper loop error") |
372 | | - |
373 | | - |
374 | | -# ── standalone CLI entry point ──────────────────────────────────────────────── |
375 | | - |
376 | | -def _build_config() -> HubConfig: |
377 | | - p = argparse.ArgumentParser( |
378 | | - description="taskiq-nng-hub — NNG task router, dispatcher, and lease manager", |
379 | | - formatter_class=argparse.ArgumentDefaultsHelpFormatter, |
380 | | - ) |
381 | | - p.add_argument( |
382 | | - "--control-addr", |
383 | | - default=os.getenv("NNG_CONTROL_ADDR", "ipc:///tmp/taskiq-nng.ipc"), |
384 | | - help="NNG address the hub listens on. Env: NNG_CONTROL_ADDR", |
385 | | - ) |
386 | | - p.add_argument( |
387 | | - "--max-pending", |
388 | | - type=int, |
389 | | - default=int(os.getenv("NNG_MAX_PENDING", "10000")), |
390 | | - ) |
391 | | - p.add_argument( |
392 | | - "--heartbeat-timeout", |
393 | | - type=float, |
394 | | - default=float(os.getenv("NNG_HEARTBEAT_TIMEOUT", "15.0")), |
395 | | - help="Seconds of silence before a worker is declared dead.", |
396 | | - ) |
397 | | - p.add_argument( |
398 | | - "--lease-timeout", |
399 | | - type=float, |
400 | | - default=float(os.getenv("NNG_LEASE_TIMEOUT", "20.0")), |
401 | | - help="Seconds before an unacked task lease is reaped.", |
402 | | - ) |
403 | | - p.add_argument( |
404 | | - "--routing-policy", |
405 | | - choices=["least_loaded", "p2c", "round_robin"], |
406 | | - default=os.getenv("NNG_ROUTING_POLICY", "least_loaded"), |
407 | | - ) |
408 | | - p.add_argument( |
409 | | - "--control-concurrency", |
410 | | - type=int, |
411 | | - default=int(os.getenv("NNG_CONTROL_CONCURRENCY", "16")), |
412 | | - help="Number of concurrent Rep0 contexts.", |
413 | | - ) |
414 | | - p.add_argument( |
415 | | - "--log-level", |
416 | | - default=os.getenv("NNG_LOG_LEVEL", "INFO"), |
417 | | - choices=["DEBUG", "INFO", "WARNING", "ERROR"], |
418 | | - ) |
419 | | - args = p.parse_args() |
420 | | - logging.basicConfig( |
421 | | - level=getattr(logging, args.log_level), |
422 | | - format="%(asctime)s %(name)-24s %(levelname)-8s %(message)s", |
423 | | - ) |
424 | | - return HubConfig( |
425 | | - control_addr=args.control_addr, |
426 | | - max_pending=args.max_pending, |
427 | | - heartbeat_timeout=args.heartbeat_timeout, |
428 | | - lease_timeout=args.lease_timeout, |
429 | | - routing_policy=args.routing_policy, |
430 | | - control_concurrency=args.control_concurrency, |
431 | | - ) |
432 | | - |
433 | | - |
434 | | -async def _run(config: HubConfig) -> None: |
435 | | - hub = NNGHub(config) |
436 | | - loop = asyncio.get_running_loop() |
437 | | - stop_event = asyncio.Event() |
438 | | - |
439 | | - def _on_signal() -> None: |
440 | | - logger.info("Shutdown signal received") |
441 | | - stop_event.set() |
442 | | - |
443 | | - for sig in (signal.SIGTERM, signal.SIGINT): |
444 | | - loop.add_signal_handler(sig, _on_signal) |
445 | | - |
446 | | - await hub.start() |
447 | | - try: |
448 | | - await stop_event.wait() |
449 | | - finally: |
450 | | - await hub.stop() |
451 | | - |
452 | | - |
453 | | -def main() -> None: |
454 | | - """Entry point for the ``taskiq-nng-hub`` CLI command.""" |
455 | | - config = _build_config() |
456 | | - try: |
457 | | - asyncio.run(_run(config)) |
458 | | - except KeyboardInterrupt: |
459 | | - pass |
0 commit comments