Scheduler Service
Overview
The Scheduler is a microservice that consumes RabbitMQ queues and executes container operations. It uses the Strategy pattern to support different cloud providers.
Architecture
packages/scheduler/
├── index.ts # Entry: sets up 4 queue consumers
├── container/
│ ├── index.ts # Queue consumer setup, strategy routing
│ └── controllers/
│ ├── new-container.ts # Strategy: deploy new container
│ ├── build-image.ts # Strategy: build Docker image
│ ├── destroy-container.ts # Strategy: destroy container
│ └── spot-terminate.ts # Strategy: handle spot termination
├── library/
│ ├── instance.ts # Abstract strategy interface
│ ├── instance.aws.ts # AWS on-demand implementation
│ └── instance.aws.spot.ts # AWS spot implementation
└── lua/ # Redis Lua scripts
Strategy Pattern
Queue Consumer Setup
Strategy: New Container
- Receive message from
new-containerqueue - Select available instance (via
InstanceStrategy) - If no instance available, create one
- Lock the builder instance (Redlock)
- SSH into builder:
git clone,docker build,docker pushto ECR - Release builder lock
- SSH into target instance:
docker pull,docker run - Update MongoDB container status to
running - Publish logs via Redis pub/sub
Strategy: Build Image
Similar to new-container but only builds the Docker image without deploying. Used for webhook-triggered rebuilds.
Strategy: Destroy Container
- SSH into the instance hosting the container
docker stop+docker rm- Optionally create a checkpoint and upload to S3
- Remove from reverse proxy
- Update MongoDB status to
terminated - Clean up instance if no more containers
Strategy: Spot Terminate
Triggered by CloudWatch → Lambda → RabbitMQ when a spot instance receives a termination notice:
- Identify all containers on the terminating instance
- For each container: create checkpoint → upload to S3
- Update MongoDB status to
checkpoint - Request new instance via
InstanceStrategy - Restore containers from checkpoints on new instance