refactor: extract _init_schema and _startup_pools to remove duplication in maintenance.py
This commit is contained in:
+38
-62
@@ -120,49 +120,56 @@ def try_acquire_maintenance_leader() -> bool:
|
|||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
def run_maintenance_service() -> None:
|
def _init_schema() -> None:
|
||||||
logger.info("maintenance_service_bootstrap_started")
|
|
||||||
with open("/tmp/portal-schema.lock", "w") as lock_file:
|
with open("/tmp/portal-schema.lock", "w") as lock_file:
|
||||||
fcntl.flock(lock_file.fileno(), fcntl.LOCK_EX)
|
fcntl.flock(lock_file.fileno(), fcntl.LOCK_EX)
|
||||||
Base.metadata.create_all(bind=engine)
|
Base.metadata.create_all(bind=engine)
|
||||||
ensure_schema_compatibility()
|
ensure_schema_compatibility()
|
||||||
fcntl.flock(lock_file.fileno(), fcntl.LOCK_UN)
|
fcntl.flock(lock_file.fileno(), fcntl.LOCK_UN)
|
||||||
|
|
||||||
ensure_icons_dir()
|
ensure_icons_dir()
|
||||||
bootstrap_admin()
|
bootstrap_admin()
|
||||||
|
|
||||||
|
|
||||||
|
def _startup_pools(db) -> None:
|
||||||
|
ensure_universal_pool()
|
||||||
|
ensure_web_pool()
|
||||||
|
for svc in db.scalars(
|
||||||
|
select(Service).where(
|
||||||
|
Service.active == True,
|
||||||
|
Service.type.in_([ServiceType.WEB, ServiceType.RDP]),
|
||||||
|
)
|
||||||
|
).all():
|
||||||
|
if svc.type == ServiceType.WEB and WEB_POOL_SIZE <= 0:
|
||||||
|
ensure_warm_pool(svc)
|
||||||
|
elif svc.type == ServiceType.RDP:
|
||||||
|
slots = db.scalars(select(RdpSlot).where(RdpSlot.service_id == svc.id)).all()
|
||||||
|
for slot in slots:
|
||||||
|
try:
|
||||||
|
cname = _rdp_slot_container_name(svc.slug, slot.id)
|
||||||
|
try:
|
||||||
|
c = docker_client().containers.get(cname)
|
||||||
|
if c.status != "running":
|
||||||
|
c.start()
|
||||||
|
except docker.errors.NotFound:
|
||||||
|
start_rdp_slot_container(slot, svc)
|
||||||
|
slot.container_name = cname
|
||||||
|
except Exception:
|
||||||
|
logger.exception("startup_rdp_slot_start_failed slot_id=%s", slot.id)
|
||||||
|
if slots:
|
||||||
|
db.commit()
|
||||||
|
|
||||||
|
|
||||||
|
def run_maintenance_service() -> None:
|
||||||
|
logger.info("maintenance_service_bootstrap_started")
|
||||||
|
_init_schema()
|
||||||
|
|
||||||
maintenance_lock = open("/tmp/portal-maintenance.lock", "w")
|
maintenance_lock = open("/tmp/portal-maintenance.lock", "w")
|
||||||
fcntl.flock(maintenance_lock.fileno(), fcntl.LOCK_EX)
|
fcntl.flock(maintenance_lock.fileno(), fcntl.LOCK_EX)
|
||||||
logger.info("maintenance_service_leader_acquired")
|
logger.info("maintenance_service_leader_acquired")
|
||||||
|
|
||||||
db = SessionLocal()
|
db = SessionLocal()
|
||||||
try:
|
try:
|
||||||
ensure_universal_pool()
|
_startup_pools(db)
|
||||||
ensure_web_pool()
|
|
||||||
for svc in db.scalars(
|
|
||||||
select(Service).where(
|
|
||||||
Service.active == True,
|
|
||||||
Service.type.in_([ServiceType.WEB, ServiceType.RDP]),
|
|
||||||
)
|
|
||||||
).all():
|
|
||||||
if svc.type == ServiceType.WEB and WEB_POOL_SIZE <= 0:
|
|
||||||
ensure_warm_pool(svc)
|
|
||||||
elif svc.type == ServiceType.RDP:
|
|
||||||
slots = db.scalars(select(RdpSlot).where(RdpSlot.service_id == svc.id)).all()
|
|
||||||
for slot in slots:
|
|
||||||
try:
|
|
||||||
cname = _rdp_slot_container_name(svc.slug, slot.id)
|
|
||||||
try:
|
|
||||||
c = docker_client().containers.get(cname)
|
|
||||||
if c.status != "running":
|
|
||||||
c.start()
|
|
||||||
except docker.errors.NotFound:
|
|
||||||
start_rdp_slot_container(slot, svc)
|
|
||||||
slot.container_name = cname
|
|
||||||
except Exception:
|
|
||||||
logger.exception("startup_rdp_slot_start_failed slot_id=%s", slot.id)
|
|
||||||
if slots:
|
|
||||||
db.commit()
|
|
||||||
finally:
|
finally:
|
||||||
db.close()
|
db.close()
|
||||||
|
|
||||||
@@ -171,13 +178,7 @@ def run_maintenance_service() -> None:
|
|||||||
|
|
||||||
|
|
||||||
def on_startup() -> None:
|
def on_startup() -> None:
|
||||||
with open("/tmp/portal-schema.lock", "w") as lock_file:
|
_init_schema()
|
||||||
fcntl.flock(lock_file.fileno(), fcntl.LOCK_EX)
|
|
||||||
Base.metadata.create_all(bind=engine)
|
|
||||||
ensure_schema_compatibility()
|
|
||||||
fcntl.flock(lock_file.fileno(), fcntl.LOCK_UN)
|
|
||||||
ensure_icons_dir()
|
|
||||||
bootstrap_admin()
|
|
||||||
if not try_acquire_maintenance_leader():
|
if not try_acquire_maintenance_leader():
|
||||||
logger.info("maintenance_leader_skipped")
|
logger.info("maintenance_leader_skipped")
|
||||||
return
|
return
|
||||||
@@ -185,32 +186,7 @@ def on_startup() -> None:
|
|||||||
if ENABLE_STARTUP_MAINTENANCE:
|
if ENABLE_STARTUP_MAINTENANCE:
|
||||||
db = SessionLocal()
|
db = SessionLocal()
|
||||||
try:
|
try:
|
||||||
ensure_universal_pool()
|
_startup_pools(db)
|
||||||
ensure_web_pool()
|
|
||||||
for svc in db.scalars(
|
|
||||||
select(Service).where(
|
|
||||||
Service.active == True,
|
|
||||||
Service.type.in_([ServiceType.WEB, ServiceType.RDP]),
|
|
||||||
)
|
|
||||||
).all():
|
|
||||||
if svc.type == ServiceType.WEB and WEB_POOL_SIZE <= 0:
|
|
||||||
ensure_warm_pool(svc)
|
|
||||||
elif svc.type == ServiceType.RDP:
|
|
||||||
slots = db.scalars(select(RdpSlot).where(RdpSlot.service_id == svc.id)).all()
|
|
||||||
for slot in slots:
|
|
||||||
try:
|
|
||||||
cname = _rdp_slot_container_name(svc.slug, slot.id)
|
|
||||||
try:
|
|
||||||
c = docker_client().containers.get(cname)
|
|
||||||
if c.status != "running":
|
|
||||||
c.start()
|
|
||||||
except docker.errors.NotFound:
|
|
||||||
start_rdp_slot_container(slot, svc)
|
|
||||||
slot.container_name = cname
|
|
||||||
except Exception:
|
|
||||||
logger.exception("startup_rdp_slot_start_failed slot_id=%s", slot.id)
|
|
||||||
if slots:
|
|
||||||
db.commit()
|
|
||||||
finally:
|
finally:
|
||||||
db.close()
|
db.close()
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user