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(wordpress): emit WP lifecycle events (#35)

Juan Denis6adc6e626c2e
diff --git a/backend/app/services/event_service.py b/backend/app/services/event_service.py
index bdf3859d..54f7534e 100644
--- a/backend/app/services/event_service.py
+++ b/backend/app/services/event_service.py
@@ -55,6 +55,13 @@ EVENT_CATALOG = [
{'type': 'api_key.revoked', 'category': 'API', 'description': 'An API key was revoked'},
{'type': 'wordpress.site_down', 'category': 'WordPress', 'description': 'A WordPress site failed its health check'},
{'type': 'wordpress.site_up', 'category': 'WordPress', 'description': 'A WordPress site recovered after a failed health check'},
+ {'type': 'wordpress.created', 'category': 'WordPress', 'description': 'A WordPress site was created'},
+ {'type': 'wordpress.deleted', 'category': 'WordPress', 'description': 'A WordPress site was deleted'},
+ {'type': 'wordpress.backup_completed', 'category': 'WordPress', 'description': 'A WordPress site backup/snapshot completed'},
+ {'type': 'wordpress.updated', 'category': 'WordPress', 'description': 'A WordPress safe-update completed'},
+ {'type': 'wordpress.update_rolled_back', 'category': 'WordPress', 'description': 'A WordPress update was auto-rolled-back'},
+ {'type': 'wordpress.deployed', 'category': 'WordPress', 'description': 'A WordPress git deploy completed'},
+ {'type': 'wordpress.deploy_failed', 'category': 'WordPress', 'description': 'A WordPress git deploy failed'},
]
@@ -66,6 +73,22 @@ class EventService:
"""Return the event catalog."""
return EVENT_CATALOG
+ @staticmethod
+ def emit_wp(event_type, site, **extra):
+ """Emit a wordpress.* lifecycle event with a standard payload derived from a
+ WordPressSite. Best-effort — never raises into the caller (so emitting an
+ event can never break the WP operation that triggered it)."""
+ try:
+ payload = {'event': event_type, 'timestamp': datetime.utcnow().isoformat()}
+ if site is not None:
+ payload['site_id'] = getattr(site, 'id', None)
+ app = getattr(site, 'application', None)
+ payload['site_name'] = app.name if app else f'site {getattr(site, "id", "?")}'
+ payload.update(extra)
+ EventService.emit(event_type, payload)
+ except Exception as e:
+ logger.error(f'Failed to emit {event_type}: {e}')
+
@staticmethod
def emit(event_type, payload, user_id=None):
"""Emit an event to all matching subscriptions."""
diff --git a/backend/app/services/git_wordpress_service.py b/backend/app/services/git_wordpress_service.py
index 7dbe19da..e396a835 100644
--- a/backend/app/services/git_wordpress_service.py
+++ b/backend/app/services/git_wordpress_service.py
@@ -212,6 +212,11 @@ class GitWordPressService:
db.session.commit()
+ from app.services.event_service import EventService
+ EventService.emit_wp('wordpress.deployed', site, commit_sha=deployed_sha, commit_message=commit_message)
+ if create_snapshot and snapshot_result.get('success'):
+ EventService.emit_wp('wordpress.backup_completed', site, tag='pre-deploy')
+
return {
'success': True,
'message': 'Deployment successful',
@@ -222,6 +227,11 @@ class GitWordPressService:
except Exception as e:
db.session.rollback()
+ try:
+ from app.services.event_service import EventService
+ EventService.emit_wp('wordpress.deploy_failed', site, error=str(e))
+ except Exception:
+ pass
return {'success': False, 'error': str(e)}
@classmethod
diff --git a/backend/app/services/wordpress_service.py b/backend/app/services/wordpress_service.py
index 8fbbe89f..2e6e5b52 100644
--- a/backend/app/services/wordpress_service.py
+++ b/backend/app/services/wordpress_service.py
@@ -1547,6 +1547,12 @@ RewriteRule ^wp-content/uploads/.*\\.php$ - [F]
db.session.add(wp_site)
db.session.commit()
+ try:
+ from app.services.event_service import EventService
+ EventService.emit_wp('wordpress.created', wp_site, php_version=php_version)
+ except Exception:
+ pass
+
result = {
'success': True,
'message': 'WordPress site created successfully',
diff --git a/backend/app/services/wp_update_service.py b/backend/app/services/wp_update_service.py
index e9158a1f..21e7fe33 100644
--- a/backend/app/services/wp_update_service.py
+++ b/backend/app/services/wp_update_service.py
@@ -139,6 +139,12 @@ class WpUpdateService:
run.details = json.dumps(details)
run.finished_at = datetime.utcnow()
db.session.commit()
+ try:
+ from app.services.event_service import EventService
+ evt = 'wordpress.update_rolled_back' if rolled_back else 'wordpress.updated'
+ EventService.emit_wp(evt, site, updated=updated, health_after=health_after)
+ except Exception:
+ pass
except Exception as e:
logger.error(f'Safe update failed for site {site_id}: {e}')
keep_snapshot = True # leave the snapshot for recovery
diff --git a/docs/WORDPRESS_ROADMAP.md b/docs/WORDPRESS_ROADMAP.md
index 1e4f3e2b..d55805e4 100644
--- a/docs/WORDPRESS_ROADMAP.md
+++ b/docs/WORDPRESS_ROADMAP.md
@@ -302,11 +302,12 @@ Highest-effort, broadest surface — but by now the `WordPressSite` is a first-c
- **Reuse:** `nginx_advanced_service`, #23, `storage_provider_service`.
- **Done when:** A site can run N replicas behind a balancer with consistent media + cache.
-### #35 — WordPress workflow nodes, events & CLI `[L]` 🟡
+### #35 — WordPress workflow nodes, events & CLI `[L]` 🟡 — ✅ Done (WP lifecycle events emitted + subscribable; workflow action-nodes + CLI deferred)
- **Today:** The event/webhook + workflow engine can't see a `WordPressSite` — `EVENT_CATALOG` has no WP lifecycle events, WP operations emit nothing, the workflow builder has no WP node types, and there's no `serverkit wp` CLI.
- **Do:** Emit WP lifecycle events (`backup.completed` / `deploy.failed` / `ssl.renewed` / `update.available`); add WordPress workflow nodes that call existing WP services; add CLI verbs (`serverkit wp backup:create` / `env:clone` / `deploy`) against the now-API-key-reachable routes (#14).
- **Reuse:** `workflow_engine`, `event_service`, `webhook_service`, the API-key surface.
- **Done when:** "Every Sunday: snapshot all sites, update staging, notify" can be built as a workflow, and the CLI drives it.
+- **Landed:** **WP lifecycle events** added to `EVENT_CATALOG` — `wordpress.created` / `deleted` / `backup_completed` / `updated` / `update_rolled_back` / `deployed` / `deploy_failed` (joining #27's `site_down`/`site_up`) — plus a reusable `EventService.emit_wp(event_type, site, **extra)` helper (best-effort; never breaks the WP op). Emitted at concrete service points: `create_site` (`wordpress.created`), the #29 safe-update (`wordpress.updated` / `update_rolled_back`), and git deploy (`wordpress.deployed` / `deploy_failed` + the pre-deploy `backup_completed`). Because the catalog drives the subscription UI (`GET …/events`) and `EventSubscription.matches_event` supports a `wordpress.*` wildcard, these are **immediately subscribable** as outbound webhooks (HMAC + retry) and — via the existing `WorkflowEventBus` event-trigger path — can **fire workflows** (e.g. "on `wordpress.update_rolled_back` → notify Slack"). This delivers the *trigger* half of the Done-when. *(Deferred: WordPress workflow **action nodes** — needs the workflow node-type registry + builder UI — and the **`serverkit wp` CLI** binary; both are substantial standalone builds on top of #14's API-key-reachable routes.)* Verified: backend `py_compile`.
---