Skip to content

ServerKit is a lightweight, modern server control panel for managing web applications, databases, and services on your VPS or dedicated server. Built with Python/Flask backend and React frontend.

refactor(scale): ContainerScaleService owns its rollback

Juan Denis1316feaad340
diff --git a/backend/app/api/apps.py b/backend/app/api/apps.py
index ddc9008f..6c2b8108 100644
--- a/backend/app/api/apps.py
+++ b/backend/app/api/apps.py
@@ -1888,7 +1888,7 @@ def update_scale_policy(app_id):
try:
policy = ContainerScaleService.set_policy(app_id, **data)
except ValueError as exc:
- db.session.rollback()
+ # The service rolls back its own dirty session before raising.
return jsonify({'error': str(exc)}), 400
return jsonify(policy.to_dict())
diff --git a/backend/app/services/container_scale_service.py b/backend/app/services/container_scale_service.py
index b07861b2..6eeb5adc 100644
--- a/backend/app/services/container_scale_service.py
+++ b/backend/app/services/container_scale_service.py
@@ -31,7 +31,18 @@ class ContainerScaleService:
@classmethod
def set_policy(cls, application_id, **fields):
policy = cls.get_or_create_policy(application_id)
+ try:
+ cls._apply_policy_fields(policy, fields)
+ db.session.commit()
+ except ValueError:
+ # Validation raises after fields were already assigned to the ORM
+ # row; roll back here so callers never inherit a dirty session.
+ db.session.rollback()
+ raise
+ return policy
+ @classmethod
+ def _apply_policy_fields(cls, policy, fields):
if fields.get('enabled') is not None:
enabled = fields['enabled']
if not isinstance(enabled, bool):
@@ -58,8 +69,6 @@ class ContainerScaleService:
raise ValueError('cpu_low_percent must be less than cpu_high_percent')
if policy.enabled and not policy.service_name:
raise ValueError('service_name is required when auto-scale is enabled')
- db.session.commit()
- return policy
@classmethod
def _service_cpu(cls, app, policy):