worker.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. from __future__ import annotations
  2. import os
  3. import socket
  4. import time
  5. import traceback
  6. from dataclasses import dataclass
  7. from math import ceil
  8. from uuid import uuid4
  9. from sqlalchemy.orm import Session, sessionmaker
  10. from core_shared import try_build_redis_client
  11. from core_shared.task_queue import RUNTIME_NODE_RUN_QUEUE, build_task_queue_consumer
  12. from app.application.services import build_runtime_application_service
  13. from app.bootstrap.settings import RuntimeServiceSettings
  14. from app.db.session import build_session_factory
  15. @dataclass(frozen=True)
  16. class RuntimeWorkerStats:
  17. worker_key: str
  18. executed_count: int = 0
  19. idle_count: int = 0
  20. error_count: int = 0
  21. class RuntimeWorker:
  22. def __init__(
  23. self,
  24. *,
  25. settings: RuntimeServiceSettings,
  26. session_factory: sessionmaker[Session],
  27. worker_key: str,
  28. ) -> None:
  29. self.settings = settings
  30. self.session_factory = session_factory
  31. self.worker_key = worker_key
  32. self.redis_client = try_build_redis_client(settings.redis_url)
  33. self.task_queue = build_task_queue_consumer(
  34. client=self.redis_client,
  35. queue_name=RUNTIME_NODE_RUN_QUEUE,
  36. )
  37. def run_forever(self) -> RuntimeWorkerStats:
  38. executed_count = 0
  39. idle_count = 0
  40. error_count = 0
  41. while True:
  42. try:
  43. executed = self.run_once()
  44. except Exception:
  45. error_count += 1
  46. traceback.print_exc()
  47. executed = False
  48. if executed:
  49. executed_count += 1
  50. idle_count = 0
  51. else:
  52. idle_count += 1
  53. if self.task_queue is None:
  54. time.sleep(self.settings.worker_poll_interval_seconds)
  55. if self.settings.worker_max_idle_cycles is not None:
  56. if idle_count >= self.settings.worker_max_idle_cycles:
  57. return RuntimeWorkerStats(
  58. worker_key=self.worker_key,
  59. executed_count=executed_count,
  60. idle_count=idle_count,
  61. error_count=error_count,
  62. )
  63. def run_once(self) -> bool:
  64. self._wait_for_queue_signal()
  65. db = self.session_factory()
  66. try:
  67. service = build_runtime_application_service(db=db, settings=self.settings)
  68. result = service.execute_next_claimed_node_run(
  69. worker_key=self.worker_key,
  70. lease_seconds=self.settings.worker_lease_seconds,
  71. redis_client=self.redis_client,
  72. )
  73. return result is not None
  74. finally:
  75. db.close()
  76. def _wait_for_queue_signal(self) -> None:
  77. if self.task_queue is None:
  78. return
  79. try:
  80. self.task_queue.dequeue(
  81. timeout_seconds=max(1, ceil(self.settings.worker_poll_interval_seconds)),
  82. )
  83. except Exception:
  84. return
  85. def build_worker_key() -> str:
  86. configured_key = os.getenv("AGENT_PLATFORM_WORKER_KEY")
  87. if configured_key:
  88. return configured_key
  89. hostname = socket.gethostname()
  90. return f"{hostname}-{uuid4().hex[:8]}"
  91. def main() -> None:
  92. settings = RuntimeServiceSettings()
  93. worker = RuntimeWorker(
  94. settings=settings,
  95. session_factory=build_session_factory(settings),
  96. worker_key=build_worker_key(),
  97. )
  98. stats = worker.run_forever()
  99. print(
  100. "runtime-worker stopped "
  101. f"worker_key={stats.worker_key} "
  102. f"executed_count={stats.executed_count} "
  103. f"idle_count={stats.idle_count} "
  104. f"error_count={stats.error_count}",
  105. flush=True,
  106. )
  107. if __name__ == "__main__":
  108. main()