Skip to content

Commit c761131

Browse files
committed
add auto start service
1 parent cdd1019 commit c761131

56 files changed

Lines changed: 23 additions & 14 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

cms/service/ResourceService.py

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@
4040
from cms import config, get_safe_shard, ServiceCoord
4141
from cms.io import Service, rpc_method
4242

43+
from cms.db import SessionGen, Contest
44+
from sqlalchemy import select
45+
4346

4447
logger = logging.getLogger(__name__)
4548

@@ -250,33 +253,29 @@ def _restart_services(self):
250253
if proc is None or not proc.is_running():
251254
# We give contest_id even if the service doesn't need
252255
# it, since it causes no trouble.
256+
257+
active_contest_ids = self._get_active_contest_ids()
258+
253259
logger.info("Restarting (%s, %s)...",
254260
service.name, service.shard)
255261
command = os.path.join(BIN_PATH, "cms%s" % service.name)
256262

257-
MAX_AUTO_MAPPED_CONTESTS = 30
258-
259263

260264
args = [command, "%d" % service.shard]
261265

262266
contest_specific_services = ["ContestWebServer", "ProxyService", "EvaluationService"]
263267

264268
if self.contest_id is not None:
265269
if service.name in contest_specific_services:
270+
target_contest_id = service.shard + 1
266271

267-
# *** ตรวจสอบว่า Shard ID อยู่ในขอบเขตที่เรามี Contest หรือไม่ ***
268-
if service.shard < MAX_AUTO_MAPPED_CONTESTS:
269-
derived_contest_id = service.shard + 1
270-
logger.info("Auto-mapping %s Shard %d to Contest ID %d (ignoring admin-selected ID %d)",
271-
service.name, service.shard, derived_contest_id, self.contest_id)
272-
args += ["-c", str(derived_contest_id)]
273-
time.sleep(1)
272+
# *** ตรวจสอบว่า target_contest_id มีอยู่จริงใน Database หรือไม่ ***
273+
if target_contest_id in active_contest_ids:
274+
logger.info("Auto-mapping %s Shard %d to Active Contest ID %d",
275+
service.name, service.shard, target_contest_id)
276+
args += ["-c", str(target_contest_id)]
274277
else:
275-
# *** ถ้า Shard ID เกิน (เช่น Shard 4, 5, ...) ให้ข้ามไปเลย (continue) ***
276-
#logger.warning("Skipping %s Shard %d: No auto-map contest ID available (Max: %d)",
277-
# service.name, service.shard, MAX_AUTO_MAPPED_CONTESTS)
278-
time.sleep(1)
279-
continue # <-- นี่คือส่วนที่สำคัญที่สุด: สั่งให้ข้ามการ Restart
278+
continue
280279
else:
281280
args += ["-c", "ALL"]
282281
try:
@@ -500,3 +499,13 @@ def toggle_autorestart(self, service: str) -> bool | None:
500499
service.name, service.shard, self._will_restart[service])
501500

502501
return self._will_restart[service]
502+
def _get_active_contest_ids(self):
503+
"""ดึง ID ของการแข่งขันทั้งหมดที่มีอยู่ในระบบจาก Database"""
504+
try:
505+
with SessionGen() as session:
506+
query = select(Contest.id)
507+
contest_ids = session.execute(query).scalars().all()
508+
return set(contest_ids)
509+
except Exception as e:
510+
logger.error("Failed to fetch contest IDs from DB: %s", e)
511+
return set()
0 Bytes
Binary file not shown.

docker/pg_data/base/16387/1259

0 Bytes
Binary file not shown.

docker/pg_data/base/16387/16443

0 Bytes
Binary file not shown.

docker/pg_data/base/16387/16448

0 Bytes
Binary file not shown.

docker/pg_data/base/16387/16451

0 Bytes
Binary file not shown.

docker/pg_data/base/16387/16533

0 Bytes
Binary file not shown.

docker/pg_data/base/16387/16563

0 Bytes
Binary file not shown.

docker/pg_data/base/16387/16581

0 Bytes
Binary file not shown.

docker/pg_data/base/16387/16616

0 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)