mirror of
https://github.com/jhd3197/ServerKit.git
synced 2026-11-30 04:05:58 +09:00
- ContainerScalePolicy model (per-app: enabled, service_name, min/max replicas, cpu high/low %, cooldown, current_replicas). - ContainerScaleService: evaluate() scales a compose service up/down by one replica when average CPU crosses the high/low thresholds, within bounds and outside the cooldown; scale_to() for manual; sweep() for cron. Uses `docker compose --scale`; local + scale-capable services only. - apps.py: GET/PUT /<id>/scale-policy, POST /<id>/scale, POST /<id>/scale/evaluate, POST /scale-sweep (admin). - 10 tests: bounds clamping, scale up/down, hold-at-max, cooldown, disabled, unknown-cpu, manual endpoint, auth. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
41 lines
2.0 KiB
Python
41 lines
2.0 KiB
Python
from datetime import datetime
|
|
from app import db
|
|
|
|
|
|
class ContainerScalePolicy(db.Model):
|
|
"""Per-application horizontal auto-scaling policy. When enabled, the named
|
|
compose service is scaled between min/max replicas based on average CPU,
|
|
with a cooldown between actions. Requires a scale-capable service (no fixed
|
|
host port or container_name). One row per application."""
|
|
__tablename__ = 'container_scale_policies'
|
|
|
|
id = db.Column(db.Integer, primary_key=True)
|
|
application_id = db.Column(db.Integer, db.ForeignKey('applications.id'),
|
|
nullable=False, unique=True, index=True)
|
|
enabled = db.Column(db.Boolean, default=False, nullable=False)
|
|
service_name = db.Column(db.String(100)) # compose service to scale
|
|
min_replicas = db.Column(db.Integer, default=1, nullable=False)
|
|
max_replicas = db.Column(db.Integer, default=3, nullable=False)
|
|
cpu_high_percent = db.Column(db.Integer, default=75, nullable=False)
|
|
cpu_low_percent = db.Column(db.Integer, default=25, nullable=False)
|
|
cooldown_seconds = db.Column(db.Integer, default=300, nullable=False)
|
|
current_replicas = db.Column(db.Integer, default=1, nullable=False)
|
|
last_scaled_at = db.Column(db.DateTime)
|
|
updated_at = db.Column(db.DateTime, default=datetime.utcnow, onupdate=datetime.utcnow)
|
|
|
|
application = db.relationship('Application', backref=db.backref('scale_policy', uselist=False))
|
|
|
|
def to_dict(self):
|
|
return {
|
|
'application_id': self.application_id,
|
|
'enabled': self.enabled,
|
|
'service_name': self.service_name,
|
|
'min_replicas': self.min_replicas,
|
|
'max_replicas': self.max_replicas,
|
|
'cpu_high_percent': self.cpu_high_percent,
|
|
'cpu_low_percent': self.cpu_low_percent,
|
|
'cooldown_seconds': self.cooldown_seconds,
|
|
'current_replicas': self.current_replicas,
|
|
'last_scaled_at': self.last_scaled_at.isoformat() if self.last_scaled_at else None,
|
|
}
|