47 lines
1.4 KiB
Python
47 lines
1.4 KiB
Python
import logging
|
|
import time
|
|
|
|
from app.core.config import settings
|
|
from app.core.logging import configure_logging
|
|
from app.core.runtime import validate_runtime_settings
|
|
from app.db.session import SessionLocal, init_db
|
|
from app.services.service_control import ServiceControlService
|
|
|
|
|
|
logger = logging.getLogger("server_panel.worker")
|
|
|
|
def run_worker_loop() -> None:
|
|
service = ServiceControlService()
|
|
|
|
while True:
|
|
try:
|
|
with SessionLocal() as db:
|
|
# The worker claims one pending action at a time so a second worker
|
|
# cannot pick the same row after it has been moved to `running`.
|
|
action = service.process_next_action(db)
|
|
if action is None:
|
|
time.sleep(settings.action_worker_poll_interval_seconds)
|
|
continue
|
|
|
|
logger.info(
|
|
"Processed action id=%s status=%s target=%s",
|
|
action.id,
|
|
action.status,
|
|
action.target_name,
|
|
)
|
|
except Exception: # pragma: no cover - defensive top-level worker loop
|
|
logger.exception("Worker loop iteration failed.")
|
|
time.sleep(settings.action_worker_poll_interval_seconds)
|
|
|
|
|
|
def main() -> None:
|
|
configure_logging()
|
|
validate_runtime_settings(settings)
|
|
init_db()
|
|
logger.info("Starting action worker in %s mode.", settings.action_execution_mode)
|
|
run_worker_loop()
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|