Files
ServerKit/backend/app/bootstrap/runtime.py
T
6e096fa286 Add real preview deployments and strengthen core boundaries (#184)
* fix(security): honour the failed-login time window (#180)

check_failed_logins computed a cutoff from since_hours and never used
it, so the Security -> Events "Failed login attempts (24h)" card and
its alert counted every failure still in /var/log/auth.log or
/var/log/secure: up to a week of SSH attempts under the usual weekly
log rotation.

Each matching line is now dated from its stamp, either RFC 3339
(rsyslog's high-precision format) or the traditional BSD syslog stamp
(no year, so a December line read in January is placed in the previous
year), and lines older than the cutoff are skipped. A line whose stamp
is not recognised is still counted rather than hidden. The log paths
move to SecurityService.AUTH_LOGS so the tests can point at a temp
file.

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>

* refactor: separate backend startup phases and guard ordering

* refactor: route file operations through capability-checked adapters

* chore: bump version to 1.11.25 [skip ci]

* refactor: split File Manager state and views with regression guards

* test: enforce core boundaries and document extension rules

* test: cover File Manager tree races and listing failures

* fix: preserve File Manager deep links on initial mount

* refactor: keep File Manager download operations in the action hook

* fix: start agent maintenance only in the guarded runtime phase

* test: capture nginx rendering contract before extraction

* refactor: separate nginx configuration rendering from host operations

* test: enforce pure nginx configuration boundaries

* fix: require provisioned runtime before advertising previews

* feat: expose value-free effective environment provenance through app access boundary

* feat: add effective environment inspector with browser regression checks

* feat: provision isolated generic previews with retryable resource cleanup

* feat: queue preview automation and preserve cleanup ownership through app deletion

* feat: add pinned preview deployment controls and queued lifecycle feedback

* fix: preserve container port semantics for previews of slot deployments

* fix: preserve search focus and ignore stale list responses

* feat: run approved assistant deployments through persistent recipe operations

* test: run search and assistant deployment regressions in CI

* fix: refresh preview API and controller boundary inventories

* docs: refresh controller migration count after preview extraction

* fix: bind file editor previews to their storage target

* chore: bump version to 1.11.26 [skip ci]

---------

Co-authored-by: Max Freedom Pollard <272618364+MaxFreedomPollard@users.noreply.github.com>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
2026-10-24 15:31:39 -04:00

87 lines
4.3 KiB
Python

"""Background runtime startup; skipped for one-shot CLI invocations."""
import os
import sys
def start_runtime(app):
# Background daemons (metrics collector, queue consumers, analytics
# flush, the job system, the linked-panel client) only make sense in a
# long-running server process. When the app is loaded by a Flask CLI
# one-shot — crucially `flask db upgrade` during an update — they must
# NOT start: they query the database before migrations have run, and
# any failure there (a corrupt DB, or pre-migration schema the new code
# doesn't match yet) aborts the CLI command and sinks the whole update.
# SERVERKIT_SKIP_BACKGROUND=1 forces the same skip; the updater sets it
# as an explicit contract when running migrations.
_cli_one_shot = (
os.path.basename(sys.argv[0] or '').startswith('flask')
and (len(sys.argv) < 2 or sys.argv[1] != 'run')
)
from app.utils.env import env_bool
_skip_background = env_bool('SERVERKIT_SKIP_BACKGROUND') or _cli_one_shot
if not _skip_background:
# Before any consumer polls: release what the previous process
# left in flight, or it stays stranded forever.
if not app.config.get('TESTING'):
from app.jobs.service import JobService
try:
JobService.recover_interrupted()
except Exception:
import logging
logging.getLogger(__name__).exception('Interrupted-job recovery failed')
from app.bootstrap.agents import start_agent_runtime
start_agent_runtime(app)
# Start metrics history collection in background
from app.services.metrics_history_service import MetricsHistoryService
if not MetricsHistoryService.is_running():
MetricsHistoryService.start_collection(app)
# Start queue-bus webhook consumer
from app.queue_bus.consumers import start_webhook_consumer
start_webhook_consumer(app)
# Start queue-bus notification consumer (delivers in-app/email/chat)
from app.notifications.consumer import start_notification_consumer
start_notification_consumer(app)
# Start the API analytics flush thread (a 5s buffer flush — a real-time
# stream, deliberately NOT modeled as a job).
from app.middleware.api_analytics import start_analytics_flush_thread
start_analytics_flush_thread(app)
# Start the unified job system: ONE consumer runs every enqueued Job and
# ONE scheduler ticks all periodic work. This supersedes the former set
# of per-domain daemon scheduler threads (auto-sync, snapshot-retention,
# workflow, health-check, wp-update, api-background, pairing-prune,
# registrar-expiry) — they are now ScheduledJob rows backed by the
# built-in handlers in app/jobs/builtin_handlers.py.
from app.jobs import start_job_system
from app.bootstrap.jobs import register_job_handlers
seed_builtin_schedules = register_job_handlers(app)
start_job_system(app, seed=seed_builtin_schedules)
# Threshold alerting and the uptime sampler are switched on from
# the UI; without this they silently stop at the next restart
# while their status still reads "enabled".
# Best effort: a resume failure must never stop the panel booting.
if not app.config.get('TESTING'):
from app.services.monitoring_service import MonitoringService
from app.services.uptime_service import UptimeService
for resume in (lambda: MonitoringService.resume_if_enabled(app),
UptimeService.resume_if_enabled):
try:
resume()
except Exception: # noqa: BLE001
app.logger.exception('Could not resume a background monitor at boot')
# Resume the embedded agent when this panel is linked to a master
# ServerKit panel (ServerKit-to-ServerKit peering).
from app.services.linked_panel_service import LinkedPanelService
LinkedPanelService.start_client_if_linked(app)
# Hold the outbound ServerKit Cloud relay connection when this
# panel is paired.
from app.services import connect_client
connect_client.start_client_if_paired(app)