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.

feat(jobs): run template installs as a deploy.install unified job (Phase 5)

Juan Denis85265df138da
diff --git a/backend/app/__init__.py b/backend/app/__init__.py
index 5166802a..59af5bfd 100644
--- a/backend/app/__init__.py
+++ b/backend/app/__init__.py
@@ -437,6 +437,9 @@ def create_app(config_name=None):
from app.jobs import start_job_system
from app.jobs.builtin_handlers import register_builtin_handlers, seed_builtin_schedules
register_builtin_handlers()
+ # Register event-driven job handlers (deployment installs, etc.).
+ from app.services.deployment_job_service import DeploymentJobService
+ DeploymentJobService.register_jobs()
start_job_system(app, seed=seed_builtin_schedules)
# Request body size limit
diff --git a/backend/app/services/deployment_job_service.py b/backend/app/services/deployment_job_service.py
index 8493f0ab..41706faa 100644
--- a/backend/app/services/deployment_job_service.py
+++ b/backend/app/services/deployment_job_service.py
@@ -1,13 +1,10 @@
"""Deployment job orchestration service."""
import os
-import threading
import uuid
from datetime import datetime
from typing import Dict, Optional
-from flask import current_app, has_app_context
-
from app import db
from app.models import Application, Server
from app.models.deployment_job import DeploymentJob
@@ -16,6 +13,9 @@ from app.services.docker_service import DockerService
from app.services.template_service import TemplateService
from app.services.telemetry_service import generate_correlation_id
+# Unified job kind for asynchronous template installs (see register_jobs()).
+JOB_KIND = 'deploy.install'
+
class DeploymentJobService:
"""Creates and runs deployment jobs with persistent logs."""
@@ -75,7 +75,7 @@ class DeploymentJobService:
if wait:
cls.run_job(job.id)
else:
- cls._start_background_job(job.id)
+ cls._enqueue_install(job)
return {
'success': True,
@@ -177,22 +177,51 @@ class DeploymentJobService:
return {'success': True, 'job': job.to_dict(include_logs=True), **result}
+ # ------------------------------------------------------------------
+ # Unified job system integration (kind: deploy.install)
+ # ------------------------------------------------------------------
+ @classmethod
+ def _enqueue_install(cls, job: DeploymentJob):
+ """Hand the deployment to the unified job system for async execution.
+
+ Replaces the former one-off daemon thread: the work now persists on the
+ Queue Bus and is run by the single JobConsumer, so it survives a restart
+ and is observable via /api/v1/jobs. Installs create containers/files/an
+ app row and are NOT idempotent, so max_attempts=1 (no auto-retry).
+ """
+ from app.jobs.service import JobService
+ return JobService.enqueue(
+ JOB_KIND,
+ payload={'deployment_job_id': job.id},
+ max_attempts=1,
+ owner_type='deployment_job',
+ owner_id=job.id,
+ correlation_id=job.correlation_id,
+ )
+
+ @staticmethod
+ def _run_install_job(unified_job):
+ """Unified-job handler for ``deploy.install``. Drives run_job and surfaces
+ a failed deployment as a raised error so the unified job is marked failed
+ too (the DeploymentJob row already carries the detailed status/logs)."""
+ deployment_job_id = (unified_job.get_payload() or {}).get('deployment_job_id')
+ if not deployment_job_id:
+ raise ValueError('deploy.install job missing deployment_job_id')
+ result = DeploymentJobService.run_job(deployment_job_id)
+ if not result.get('success'):
+ raise RuntimeError(result.get('error') or 'Deployment failed')
+ return {
+ 'deployment_job_id': deployment_job_id,
+ 'app_id': result.get('app_id'),
+ 'app_name': result.get('app_name'),
+ }
+
@classmethod
- def _start_background_job(cls, job_id: str):
- flask_app = current_app._get_current_object() if has_app_context() else None
-
- def _target():
- if flask_app:
- with flask_app.app_context():
- cls.run_job(job_id)
- else:
- from app import create_app
- app = create_app()
- with app.app_context():
- cls.run_job(job_id)
-
- thread = threading.Thread(target=_target, daemon=True)
- thread.start()
+ def register_jobs(cls):
+ """Register deployment handlers with the unified job registry. Called once
+ at app startup (see app/__init__.py)."""
+ from app.jobs import registry
+ registry.register(JOB_KIND, cls._run_install_job, replace=True)
@staticmethod
def _normalize_server_id(server_id: Optional[str]) -> Optional[str]:
diff --git a/backend/tests/test_jobs.py b/backend/tests/test_jobs.py
index 89b0317e..081edb75 100644
--- a/backend/tests/test_jobs.py
+++ b/backend/tests/test_jobs.py
@@ -211,3 +211,91 @@ class TestApi:
resp = client.get('/api/v1/jobs/scheduled', headers=auth_headers)
assert resp.status_code == 200
assert any(s['name'] == 'api-sched' for s in resp.get_json()['scheduled'])
+
+
+class TestDeploymentInstallHandler:
+ """Phase 5 — template installs run as a 'deploy.install' unified job rather
+ than a one-off thread. run_job is patched so these exercise the wiring, not
+ real Docker/templates."""
+
+ def test_register_jobs_adds_handler(self, app):
+ from app.services.deployment_job_service import DeploymentJobService, JOB_KIND
+ DeploymentJobService.register_jobs()
+ assert JOB_KIND == 'deploy.install'
+ assert registry.is_registered('deploy.install')
+
+ def test_handler_success_marks_job_succeeded(self, app, monkeypatch):
+ from app.services.deployment_job_service import DeploymentJobService
+ calls = {}
+
+ def fake_run_job(job_id):
+ calls['job_id'] = job_id
+ return {'success': True, 'app_id': 7, 'app_name': 'blog'}
+
+ monkeypatch.setattr(DeploymentJobService, 'run_job', staticmethod(fake_run_job))
+ DeploymentJobService.register_jobs()
+
+ unified = JobService.enqueue('deploy.install', {'deployment_job_id': 'dep-1'}, max_attempts=1)
+ _drain_once()
+
+ refreshed = Job.query.get(unified.id)
+ assert refreshed.status == Job.STATUS_SUCCEEDED
+ assert refreshed.get_result()['app_id'] == 7
+ assert calls['job_id'] == 'dep-1'
+
+ def test_handler_failure_marks_job_failed(self, app, monkeypatch):
+ from app.services.deployment_job_service import DeploymentJobService
+
+ monkeypatch.setattr(DeploymentJobService, 'run_job',
+ staticmethod(lambda job_id: {'success': False, 'error': 'compose boom'}))
+ DeploymentJobService.register_jobs()
+
+ unified = JobService.enqueue('deploy.install', {'deployment_job_id': 'dep-2'}, max_attempts=1)
+ _drain_once()
+
+ refreshed = Job.query.get(unified.id)
+ assert refreshed.status == Job.STATUS_FAILED
+ assert 'compose boom' in (refreshed.error_message or '')
+
+ def test_install_template_enqueues_job_instead_of_thread(self, app, monkeypatch):
+ from app.services.deployment_job_service import DeploymentJobService
+ from app.services.template_service import TemplateService
+ from app.models.deployment_job import DeploymentJob
+
+ plan = {
+ 'app_name': 'demo', 'app_path': '/tmp/serverkit-nonexistent-demo-xyz',
+ 'port': 8123, 'template_name': 'nginx', 'template_id': 'nginx',
+ 'steps': [{'type': 'log', 'name': 'step 1'}],
+ }
+ monkeypatch.setattr(
+ TemplateService, 'build_install_plan',
+ staticmethod(lambda **kw: {'success': True, 'plan': plan, 'app_path': plan['app_path']}))
+
+ ran = {'called': False}
+
+ def fake_run_job(job_id):
+ ran['called'] = True
+ return {'success': True}
+
+ monkeypatch.setattr(DeploymentJobService, 'run_job', staticmethod(fake_run_job))
+ DeploymentJobService.register_jobs()
+
+ result = DeploymentJobService.install_template(
+ template_id='nginx', app_name='demo', user_variables={}, user_id=1,
+ server_id='local', wait=False,
+ )
+ assert result['success'] is True
+ dep_id = result['job_id']
+
+ # The DeploymentJob exists and is still pending — proof it was enqueued,
+ # not run inline on a thread (no consumer runs under testing config).
+ dep = DeploymentJob.query.get(dep_id)
+ assert dep is not None and dep.status == 'pending'
+ assert ran['called'] is False
+
+ # A unified deploy.install job points back at it, with shared correlation.
+ unified = Job.query.filter_by(kind='deploy.install', owner_id=dep_id).first()
+ assert unified is not None
+ assert unified.get_payload() == {'deployment_job_id': dep_id}
+ assert unified.max_attempts == 1
+ assert unified.correlation_id == dep.correlation_id