Running the Runtime
Development
Production
AGENTS.md
Lifecycle Management
Startup
The runtime initializes in this order:- Load configuration from
config.tomland environment variables - Initialize tracing with configured log level
- Connect to PostgreSQL and run migrations
- Connect to Redis with exponential backoff
- Initialize V8 once per process
- Create worker pool (default 4 workers, max 64)
- Initialize workers sequentially to avoid V8 race conditions
- Load SDK bundle into all workers
- Restore deployments from database
- Deploy guild scripts to appropriate workers
- Start Discord client (Serenity)
- Start HTTP API server (Axum on port 3000)
apps/runtime/src/main.rs:40
Workers are initialized sequentially to prevent V8 initialization race conditions.
Shutdown
Graceful shutdown sequence:- Receive SIGTERM or SIGINT
- Migrate guilds between workers (load balancing)
- Send shutdown to each worker
- Join worker threads (wait for completion)
- Drop runtime states (cleanup V8 isolates)
apps/runtime/src/runtime/mod.rs:325
Guild Deployments
Deploying a Script
- Validate bundle size and file count
- Store in PostgreSQL with source map
- Invalidate Redis cache for guild
- Route to worker via guild hash
- Create V8 isolate for guild
- Execute bootstrap code
- Load SDK bundle
- Load guild script
- Extract dispatch function
- Register event handlers
apps/runtime/src/runtime/worker.rs:474
Rollback
Deployments can be rolled back by redeploying a previous bundle.flora does not maintain deployment history. Store deployment bundles externally for rollback capability.
Worker Management
Worker Pool Sizing
Workers are CPU-bound (V8 execution). Size based on CPU cores.- More workers = more memory (each has V8 instance)
- More workers = better parallelism
- Max workers: 64 (hard limit)
apps/runtime/src/runtime/constants.rs
Guild Migration
Guilds can be migrated between workers for load balancing.- Begin migration (queue new events)
- Migrate out from source worker
- Quiesce event loop (wait for pending ops)
- Verify isolate is idle
- Transfer ownership
- Migrate in to target worker
- Accept ownership
- Re-enter V8 context
- Finish migration (replay queued events)
apps/runtime/src/runtime/mod.rs:112
Monitoring
Metrics
Metrics exposed via Prometheus format at/metrics.
Available metrics:
flora_dispatch_success- Event dispatch successesflora_dispatch_error- Event dispatch errorsflora_dispatch_duration- Event dispatch durationflora_timeout_error- Timeout errorsflora_oom_error- Out-of-memory errorsflora_isolate_restarted- Isolate restart countflora_migration_success- Successful migrationsflora_migration_timeout- Migration timeoutsflora_migration_quiesce_duration- Migration quiesce duration
apps/runtime/src/metrics.rs
Logs
Structured logging viatracing.
flora:runtime- Runtime lifecycle eventsflora:discord- Discord eventsflora:deployments- Deployment operationsflora:kv- KV operations
apps/runtime/src/handlers/logs.rs
Health Checks
apps/runtime/src/handlers/health.rs
Troubleshooting
Runtime Won’t Start
Check required configuration:High Memory Usage
Causes:- Too many workers (each has V8 instance)
- Too many Sled instances in cache
- Memory leaks in guild scripts
Timeouts
Dispatch timeouts:Frequent timeouts indicate slow guild scripts. Profile and optimize user code.
Isolate Crashes
Common causes:- Out-of-memory (OOM)
- Infinite loops
- Unhandled promise rejections
- Increase V8 heap size (requires code change)
- Add timeout protection to user code
- Restart guild runtime
Backup and Recovery
Database Backups
KV Backups
apps/runtime/src/services/kv/service.rs:293
Secret Backups
Scaling
Vertical Scaling
Increase resources on single instance.Horizontal Scaling
flora runtime currently does not support horizontal scaling (multiple instances). Guild state is tied to worker isolates.
- Shard guilds across runtime instances
- Shared state via distributed cache
- Leader election for worker coordination
Maintenance
Updating Dependencies
AGENTS.md
Database Migrations
Migrations are applied automatically at startup.Updating V8
V8 is embedded in Deno Core. Update viadeno_core dependency.
Production Checklist
- Set all required environment variables
- Use
cookie_secure = truefor HTTPS - Configure appropriate timeouts
- Set up database backups
- Set up KV backups
- Monitor metrics endpoint
- Configure log aggregation
- Test disaster recovery procedure
- Document secret rotation process
- Set up health check monitoring
- Configure firewall rules
- Use TLS for PostgreSQL and Redis
- Rotate API secrets regularly
- Test deployment rollback
- Monitor worker CPU usage
- Set up alerting for OOM errors