mirror of
https://github.com/jhd3197/ServerKit.git
synced 2026-12-04 22:21:21 +09:00
Make a managed WordPress site appear on a status page with a real uptime % and auto-incidents, driven by an autonomous server-side health poller. Scheduler: a single-worker-guarded daemon thread (_start_health_check_ scheduler, 300s) runs EnvironmentHealthService.check_health for every RUNNING production site (skips archived/stopped so an intentional stop is not an outage). This keeps health_status fresh autonomously — so #27's transition alerts fire without the browser health card open — and drives bound status-page components. HealthCheck samples are pruned to 90 days (once/day) to bound growth. Schema (Alembic 010 + the boot-time _fix_missing_columns auto-add): - status_components.wordpress_site_id (-> wordpress_sites) - status_incidents.component_id (-> status_components) The existing HealthCheck table is reused as the uptime sample store. StatusPageService.sync_component_from_health maps the health verdict (healthy->operational, degraded->degraded, unhealthy->major_outage; unknown skipped), records a HealthCheck, recomputes a real COUNT-based uptime % (24h/7d/30d/90d; only fully-healthy checks count), and auto-opens an incident on ENTERING a major outage / auto-resolves on LEAVING it (operational OR degraded, so a degraded intermediate never strands the incident). Routes GET/POST/DELETE /sites/<id>/status-page read the binding + attach (production-only) / detach (resolves & unlinks the incident first -> no dangling FK on PostgreSQL, no stale public incident). New Uptime tab on the site detail (health + uptime % + attach/detach). Security: internal probe config (check_target, intervals) is stripped from the unauthenticated public-page projection, and a health-driven component stores no internal localhost:port target. Review fixes (3-lens adversarial pass): incident-resolve edge for the degraded-recovery path; detach FK/orphan cleanup in delete_component; public check_target leak; production-only attach; uptime-prune; degraded-uptime semantics documented. Verified: backend py_compile, frontend lint + build. Not runtime-exercised on a real Docker host (Windows dev). Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
57 lines
2.2 KiB
Python
57 lines
2.2 KiB
Python
"""Link status components to managed WordPress sites + incidents to components (#26).
|
|
|
|
Adds:
|
|
- status_components.wordpress_site_id (nullable FK -> wordpress_sites.id)
|
|
- status_incidents.component_id (nullable FK -> status_components.id)
|
|
|
|
These let a managed WordPress site appear on a status page as a health-driven
|
|
component (real uptime %) and let an auto-opened incident be auto-resolved on
|
|
recovery.
|
|
|
|
Idempotent: MigrationService._fix_missing_columns may have already added these
|
|
columns on boot before Alembic runs, so guard on the live schema. Columns are
|
|
added as plain INTEGERs (no enforced FK), matching the auto-add path and the
|
|
existing migration style.
|
|
|
|
Revision ID: 010_wordpress_status_monitoring
|
|
Revises: 009_wordpress_site_tags
|
|
Create Date: 2026-06-01
|
|
"""
|
|
from alembic import op
|
|
import sqlalchemy as sa
|
|
|
|
revision = '010_wordpress_status_monitoring'
|
|
down_revision = '009_wordpress_site_tags'
|
|
branch_labels = None
|
|
depends_on = None
|
|
|
|
|
|
def _has_col(inspector, table, col):
|
|
if table not in inspector.get_table_names():
|
|
return False
|
|
return col in {c['name'] for c in inspector.get_columns(table)}
|
|
|
|
|
|
def upgrade():
|
|
conn = op.get_bind()
|
|
inspector = sa.inspect(conn)
|
|
if 'status_components' in inspector.get_table_names() and \
|
|
not _has_col(inspector, 'status_components', 'wordpress_site_id'):
|
|
with op.batch_alter_table('status_components') as batch_op:
|
|
batch_op.add_column(sa.Column('wordpress_site_id', sa.Integer(), nullable=True))
|
|
if 'status_incidents' in inspector.get_table_names() and \
|
|
not _has_col(inspector, 'status_incidents', 'component_id'):
|
|
with op.batch_alter_table('status_incidents') as batch_op:
|
|
batch_op.add_column(sa.Column('component_id', sa.Integer(), nullable=True))
|
|
|
|
|
|
def downgrade():
|
|
conn = op.get_bind()
|
|
inspector = sa.inspect(conn)
|
|
if _has_col(inspector, 'status_incidents', 'component_id'):
|
|
with op.batch_alter_table('status_incidents') as batch_op:
|
|
batch_op.drop_column('component_id')
|
|
if _has_col(inspector, 'status_components', 'wordpress_site_id'):
|
|
with op.batch_alter_table('status_components') as batch_op:
|
|
batch_op.drop_column('wordpress_site_id')
|