Graceful Restart API
The Graceful Restart API enables zero-downtime deployments for the duel arena streaming server. When a graceful restart is requested, the server waits for the current duel to complete before restarting, ensuring no interruption to active duels or streams.Endpoints
POST /admin/graceful-restart
Request a server restart after the current duel ends. Authentication: Requiresx-admin-code header matching ADMIN_CODE environment variable.
Request:
- If no duel is active: Server restarts immediately via SIGTERM
- If duel is in progress: Server waits until RESOLUTION phase completes
- PM2 automatically restarts the server with new code
- No interruption to active duels or streams
200 OK: Restart request accepted401 Unauthorized: Missing or invalid admin code500 Internal Server Error: Failed to request restart
GET /admin/restart-status
Check if a graceful restart is pending. Authentication: Requiresx-admin-code header matching ADMIN_CODE environment variable.
Request:
restartPending(boolean): Whether a graceful restart has been requestedduelActive(boolean): Whether a duel is currently in progresscurrentPhase(string): Current duel phase (MATCHMAKING, COUNTDOWN, FIGHTING, RESOLUTION, IDLE)
200 OK: Status retrieved successfully401 Unauthorized: Missing or invalid admin code500 Internal Server Error: Failed to retrieve status
Programmatic API
StreamingDuelScheduler.requestGracefulRestart()
Request a graceful restart programmatically from within the server code. Usage:- Sets internal flag to trigger restart after current duel
- If no duel active: Triggers immediate restart
- If duel in progress: Waits for RESOLUTION phase to complete
- Restart is handled by PM2 process manager
Use Cases
Zero-Downtime Deployments
Deploy new code to the duel arena streaming server without interrupting active duels:Automated Deployment Pipeline
Integrate graceful restart into your CI/CD pipeline:Configuration
Environment Variables:ecosystem.config.cjs):
Implementation Details
The graceful restart system is implemented inStreamingDuelScheduler:
- Request Handling: Admin route sets
restartPendingflag - Duel Monitoring: Scheduler checks flag at end of each cycle
- Phase Detection: Waits for RESOLUTION phase to complete
- Process Termination: Sends SIGTERM to current process
- PM2 Restart: PM2 automatically restarts with new code
packages/server/src/systems/StreamingDuelScheduler/index.ts- Main scheduler logicpackages/server/src/startup/routes/admin-routes.ts- API endpointsecosystem.config.cjs- PM2 configuration
Troubleshooting
Restart not triggering:- Check
ADMIN_CODEenvironment variable is set - Verify admin code in request matches server configuration
- Check PM2 logs:
pm2 logs duel-arena
- Verify duel is in FIGHTING phase (not IDLE or MATCHMAKING)
- Check scheduler logs for phase transitions
- Ensure
STREAMING_DUEL_ENABLED=truein environment
- Check PM2 configuration:
pm2 show duel-arena - Verify
autorestart: truein ecosystem.config.cjs - Check PM2 process status:
pm2 status
Related Documentation
- Vast.ai Deployment - Full deployment script
- Streaming Status Check - Health monitoring
- PM2 Configuration - Process manager settings
- AGENTS.md - WebGPU streaming architecture