feat: Implement admin router and orchestrator for managing profiles and builds
- Added `adminRouter` for handling profile management, including listing, upserting, and updating statuses. - Introduced `BuildRunner` interface and `PnpmBuildRunner` class for executing build commands. - Created `GatewayOrchestrator` to manage profile states, reconcile processes, and handle build queues. - Implemented `Pm2ProcessManager` for managing processes using PM2. - Developed `GatewayProfileRepository` for interacting with the database to manage profiles. - Added utility functions for resolving workspace roots and managing process definitions. - Included tests for profile reconciliation logic.
This commit is contained in:
@@ -17,6 +17,47 @@ selection is required because it drives unit sets and DB settings.
|
||||
- Prefer `legacy/hwe/ts/util/LiteHashDRBG.ts` and `legacy/hwe/ts/util/RNG.ts`
|
||||
- Seed composition should include hidden base seed plus action context
|
||||
|
||||
## Gateway Orchestration (Single Host Draft)
|
||||
|
||||
Gateway API is the single source of truth for profile state and reconciles
|
||||
PM2-managed processes on boot and on a short interval. The DB owns the desired
|
||||
state; PM2 is treated as the actuator. The runtime state is grouped so that
|
||||
`game-api` + `turn-daemon` are either on together or off together.
|
||||
|
||||
### DB-Owned Profile State
|
||||
|
||||
Profiles are tracked by `profileName` (= `${profile}:${scenario}`).
|
||||
Gateway loads the profile table on boot, then reconciles PM2 to match.
|
||||
|
||||
- `완료됨` (COMPLETED): build output exists; processes should be off.
|
||||
- `예약됨` (RESERVED): start is scheduled for a timestamp; processes off.
|
||||
- `가동중` (RUNNING): both `game-api` and `turn-daemon` should be on.
|
||||
- `정지됨` (STOPPED): processes off; may be resumed later.
|
||||
- `비활성화` (DISABLED): excluded from orchestration; start forbidden.
|
||||
|
||||
### Boot Reconciliation
|
||||
|
||||
1) Load profile rows from DB.
|
||||
2) List PM2 processes and map `profileName -> running state`.
|
||||
3) For each profile:
|
||||
- If desired `RUNNING` and any process is missing, start missing processes.
|
||||
- If desired not `RUNNING` and any process is running, stop both processes.
|
||||
4) Persist errors to DB for audit.
|
||||
|
||||
### Internal Scheduler (Gateway Cron)
|
||||
|
||||
Gateway runs a lightweight cron loop (setInterval) that:
|
||||
- Promotes `RESERVED` profiles whose `scheduledStartAt <= now` to `RUNNING`.
|
||||
- Triggers a reconcile immediately after promotion.
|
||||
- Optionally drains a build queue (see build workflow).
|
||||
|
||||
### Build Workflow (Admin)
|
||||
|
||||
- Admin triggers a build request for a profile.
|
||||
- Gateway queues a build job, runs `pnpm --filter @sammo-ts/game-api build`
|
||||
and `pnpm --filter @sammo-ts/game-engine build`, then marks build success/failure.
|
||||
- On success, profile status remains `COMPLETED` (or stays `RUNNING` if already on).
|
||||
|
||||
## Current Implementation Status
|
||||
|
||||
- Turn daemon lifecycle + in-memory state live in `app/game-engine` with DB flush hooks.
|
||||
|
||||
@@ -14,7 +14,6 @@ Move items into the main docs once they are finalized.
|
||||
- [AI suggestion] Implement diplomacy/state transitions and monthly/command-based updates beyond read-only maps.
|
||||
- [AI suggestion] Integrate war/battle pipeline into turn processing (troop movement/war resolution hooks, not just isolated sim jobs).
|
||||
- [AI suggestion] Expand turn command catalog beyond the current subset (general/nation commands).
|
||||
- [AI suggestion] Replace in-memory control queue with distributed control/lock primitives for multi-instance daemon operation.
|
||||
|
||||
## Runtime and Operations (Lower Priority)
|
||||
|
||||
@@ -23,6 +22,8 @@ Move items into the main docs once they are finalized.
|
||||
- Document existing status/health endpoint requirements for ops and the current daemon loop behavior.
|
||||
- Document tick budget settings (wall time, max generals, catch-up cap) and partial progress persistence.
|
||||
- Document admin controls (pause/resume/manual run) and how they interact with lock/state.
|
||||
- [AI suggestion] Define gateway admin build/daemon control approach (direct orchestration vs supervisor like systemd/pm2), security model, audit logging, and safe rollback/stop/start workflows.
|
||||
- [AI suggestion] Specify single-host gateway orchestration: boot reconciliation from DB (완료/예약/가동중/정지됨/비활성화), desired-state mapping, and pm2-managed process lifecycles for api/daemon.
|
||||
- Turn daemon vs API server priority policy under load
|
||||
- Recovery behavior after partial flush or crash
|
||||
- Observability: metrics, logs, and alerts for turn processing
|
||||
|
||||
Reference in New Issue
Block a user