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.
Python astracloudflarecontrol-panelcyberpaneldnsfablehosting-control-panelhosting-panelkimi-k3server-managementserverkitubuntu-web-panelwebserver
feat(deploy): live deploy transport — socket client + useDeployJobStream hook
Juan Denisa5247573e5b0
diff --git a/frontend/src/components/DeploymentJobProgress.jsx b/frontend/src/components/DeploymentJobProgress.jsxindex 0ea72bad..3ff1e866 100644--- a/frontend/src/components/DeploymentJobProgress.jsx+++ b/frontend/src/components/DeploymentJobProgress.jsx@@ -1,92 +1,63 @@-import { useEffect, useRef, useState } from 'react';-import api from '../services/api';+import { useEffect, useRef } from 'react';+import { Link } from 'react-router-dom';+import useDeployJobStream from '../hooks/useDeployJobStream';-// Give up after this many consecutive failed polls — a transient network blip-// shouldn't kill the watcher, but a permanently unreachable job must not spin-// forever.-const MAX_CONSECUTIVE_POLL_ERRORS = 5;--// Shared "watch it come up live" panel: polls a DeploymentJob and renders its-// status, progress bar, current step and log output. Used by the Templates+// Shared "watch it come up live" compact panel: streams a DeploymentJob (socket+// with an after_id poll fallback, via useDeployJobStream) and renders its+// status, progress bar, current step and a log tail. Used by the Templates// install modal and the service Logs tab so every deploy flow behaves the same.+// The full-page Deploy Console (/deployments/:jobId) is the richer view; this+// widget links to it via "Open console →".//-// Terminal handling:+// Terminal handling (unchanged contract):// - status 'succeeded' -> onSuccess(job)// - status 'failed' -> onFailure(job.error_message)-// - too many poll errors-> onFailure(last poll error)// - timeoutMs elapsed -> onFailure('timed out' message)export default function DeploymentJobProgress({jobId,onSuccess,onFailure,- pollInterval = 1500,timeoutMs = 600000,+ showConsoleLink = true,}) {- const [job, setJob] = useState(null);- const [pollError, setPollError] = useState(null); // { message, attempts }- // Keep callbacks in refs so a re-rendering parent doesn't restart polling.+ const { job, lines, error } = useDeployJobStream(jobId);++ // Keep callbacks in refs so a re-rendering parent doesn't re-fire them.const onSuccessRef = useRef(onSuccess);const onFailureRef = useRef(onFailure);onSuccessRef.current = onSuccess;onFailureRef.current = onFailure;+ const firedRef = useRef(false);+ // Fire the terminal callback exactly once.useEffect(() => {- if (!jobId) return undefined;- let stopped = false;- let consecutiveErrors = 0;- let intervalId = null;- let timeoutId = null;-- const finish = (cb, arg) => {- if (stopped) return;- stopped = true;- if (intervalId) clearInterval(intervalId);- if (timeoutId) clearTimeout(timeoutId);- cb(arg);- };-- async function poll() {- try {- const data = await api.getDeploymentJob(jobId, true);- if (stopped) return;- consecutiveErrors = 0;- setPollError(null);- const latest = data.job;- setJob(latest);- if (latest.status === 'succeeded') {- finish(onSuccessRef.current, latest);- } else if (latest.status === 'failed') {- finish(onFailureRef.current, latest.error_message || 'Deployment failed');- }- } catch (err) {- if (stopped) return;- consecutiveErrors += 1;- const message = err?.data?.error || err?.message || 'Failed to poll deployment status';- if (consecutiveErrors >= MAX_CONSECUTIVE_POLL_ERRORS) {- finish(onFailureRef.current, `Lost contact with the deployment job: ${message}`);- } else {- setPollError({ message, attempts: consecutiveErrors });- }- }+ if (!job || firedRef.current) return;+ if (job.status === 'succeeded') {+ firedRef.current = true;+ onSuccessRef.current?.(job);+ } else if (job.status === 'failed') {+ firedRef.current = true;+ onFailureRef.current?.(job.error_message || 'Deployment failed');}+ }, [job]);- poll();- intervalId = setInterval(poll, pollInterval);- timeoutId = setTimeout(() => {+ // Hard timeout guard, mirroring the old widget so a job that never reaches a+ // terminal state doesn't spin forever.+ useEffect(() => {+ if (!jobId) return undefined;+ const t = setTimeout(() => {+ if (firedRef.current) return;+ firedRef.current = true;const minutes = Math.round(timeoutMs / 60000);- finish(onFailureRef.current, `Deployment timed out after ${minutes} minutes without reaching a final state`);+ onFailureRef.current?.(+ `Deployment timed out after ${minutes} minutes without reaching a final state`+ );}, timeoutMs);-- return () => {- stopped = true;- if (intervalId) clearInterval(intervalId);- if (timeoutId) clearTimeout(timeoutId);- };- }, [jobId, pollInterval, timeoutMs]);+ return () => clearTimeout(t);+ }, [jobId, timeoutMs]);const status = job?.status || 'pending';const progress = job?.progress_percent ?? 0;- const logs = job?.logs || [];return (<div className="deployment-job-progress">@@ -104,17 +75,22 @@ export default function DeploymentJobProgress({Current step: {job.current_step_name}</p>)}- {pollError && (+ {error && (<div className="alert alert-warning">- Trouble reaching the deployment job (attempt {pollError.attempts}/{MAX_CONSECUTIVE_POLL_ERRORS}): {pollError.message}. Retrying…+ Trouble reaching the deployment job: {error}. Retrying…</div>)}<pre className="log-viewer">- {logs.map(log => {+ {lines.map(log => {const prefix = log.step_index ? `[${log.step_index}] ` : '';return `${prefix}${log.message}`;}).join('\n') || 'Waiting for deployment logs...'}</pre>+ {showConsoleLink && jobId && (+ <Link to={`/deployments/${jobId}`} className="deployment-job-progress__console-link">+ Open console →+ </Link>+ )}</div>);}diff --git a/frontend/src/hooks/useDeployJobStream.js b/frontend/src/hooks/useDeployJobStream.jsnew file mode 100644index 00000000..40589d16--- /dev/null+++ b/frontend/src/hooks/useDeployJobStream.js@@ -0,0 +1,195 @@+import { useState, useEffect, useRef, useCallback } from 'react';+import socketService from '../services/socket';+import api from '../services/api';++// Terminal statuses freeze the stream (unsubscribe + stop polling).+const TERMINAL = new Set(['succeeded', 'failed', 'cancelled']);+// Mirror the backend's per-job row cap (plan 51 D4): keep the tail bounded so a+// very chatty build can't grow the DOM unboundedly.+const MAX_LINES = 5000;+// Fall back to polling if the socket hasn't connected shortly after mount.+const SOCKET_GRACE_MS = 2500;++/**+ * Single transport hook for the Deploy Console, the compact progress widget,+ * the LogsTab banner, and the topbar pill (plan 51 §5.5).+ *+ * Boot fetches a full snapshot (GET /deployment-jobs/<id>?logs=true), then+ * prefers the live `deploy_log`/`deploy_status` socket channel and falls back to+ * `after_id` polling when sockets are unavailable. Log rows are de-duplicated by+ * DB id; after any (re)connect the hook re-syncs with `after_id=<max seen id>`+ * so lines emitted while disconnected are never lost.+ *+ * @param {string} jobId+ * @param {object} [opts]+ * @param {number} [opts.pollInterval=2000]+ * @param {boolean} [opts.enabled=true]+ * @returns {{job, lines, isLive, transport, error, loading, refetch}}+ */+export default function useDeployJobStream(jobId, opts = {}) {+ const { pollInterval = 2000, enabled = true, includePlan = false } = opts;++ const [job, setJob] = useState(null);+ const [lines, setLines] = useState([]);+ const [transport, setTransport] = useState('poll');+ const [error, setError] = useState(null);+ const [loading, setLoading] = useState(true);++ const maxIdRef = useRef(0);+ const seenRef = useRef(new Set());+ const stoppedRef = useRef(false);+ const pollRef = useRef(null);++ // Merge a batch of log rows, de-duping by id and tracking the high-water id.+ const mergeLines = useCallback((incoming) => {+ if (!incoming || incoming.length === 0) return;+ const fresh = [];+ for (const ln of incoming) {+ if (ln.id != null) {+ if (seenRef.current.has(ln.id)) continue;+ seenRef.current.add(ln.id);+ if (ln.id > maxIdRef.current) maxIdRef.current = ln.id;+ }+ fresh.push(ln);+ }+ if (fresh.length === 0) return;+ setLines((prev) => {+ const next = prev.concat(fresh);+ return next.length > MAX_LINES ? next.slice(next.length - MAX_LINES) : next;+ });+ }, []);++ const applyJob = useCallback((next) => {+ if (!next) return;+ // The plan is fetched once on boot and never changes; socket status+ // updates don't carry it, so preserve it across replacements.+ setJob((prev) => (prev?.plan && !next.plan ? { ...next, plan: prev.plan } : next));+ if (TERMINAL.has(next.status)) {+ stoppedRef.current = true;+ }+ }, []);++ const stopPolling = useCallback(() => {+ if (pollRef.current) {+ clearInterval(pollRef.current);+ pollRef.current = null;+ }+ }, []);++ // after_id poll: pull new log rows + the current job status.+ const pollOnce = useCallback(async () => {+ if (stoppedRef.current || !jobId) return;+ try {+ const [logsRes, jobRes] = await Promise.all([+ api.getDeploymentJobLogs(jobId, maxIdRef.current || null),+ api.getDeploymentJob(jobId, false),+ ]);+ setError(null);+ mergeLines(logsRes?.logs || []);+ applyJob(jobRes?.job);+ if (jobRes?.job && TERMINAL.has(jobRes.job.status)) {+ stopPolling();+ }+ } catch (err) {+ setError(err?.data?.error || err?.message || 'Failed to reach the deployment');+ }+ }, [jobId, mergeLines, applyJob, stopPolling]);++ const startPolling = useCallback(() => {+ if (pollRef.current || stoppedRef.current || pollInterval <= 0) return;+ setTransport('poll');+ pollRef.current = setInterval(pollOnce, pollInterval);+ }, [pollOnce, pollInterval]);++ const refetch = useCallback(() => pollOnce(), [pollOnce]);++ useEffect(() => {+ if (!jobId || !enabled) return undefined;++ // Reset per-job state (the hook may be reused as jobId changes on retry).+ stoppedRef.current = false;+ maxIdRef.current = 0;+ seenRef.current = new Set();+ setJob(null);+ setLines([]);+ setError(null);+ setLoading(true);++ let cancelled = false;++ // 1) Full snapshot boot.+ (async () => {+ try {+ const data = await api.getDeploymentJob(jobId, true, includePlan);+ if (cancelled) return;+ const snapshot = data?.job;+ if (snapshot?.logs) mergeLines(snapshot.logs);+ applyJob(snapshot);+ } catch (err) {+ if (!cancelled) {+ setError(err?.data?.error || err?.message || 'Deployment job not found');+ }+ } finally {+ if (!cancelled) setLoading(false);+ }+ })();++ // 2) Live socket path, with a graceful fall back to polling.+ socketService.connect();++ const resyncAndSubscribe = () => {+ if (stoppedRef.current) return;+ setTransport('socket');+ stopPolling();+ // Re-sync any rows emitted while we were disconnected, THEN trust the+ // stream (dedupe makes overlapping ids harmless).+ api.getDeploymentJobLogs(jobId, maxIdRef.current || null)+ .then((res) => mergeLines(res?.logs || []))+ .catch(() => {});+ socketService.subscribeDeploy(jobId);+ };++ const onDeployLog = (payload) => {+ if (payload?.job_id && String(payload.job_id) !== String(jobId)) return;+ mergeLines(payload?.lines || []);+ };+ const onDeployStatus = (payload) => {+ if (payload?.job_id && String(payload.job_id) !== String(jobId)) return;+ if (payload?.status) {+ applyJob(payload.status);+ if (TERMINAL.has(payload.status.status)) {+ socketService.unsubscribeDeploy(jobId);+ stopPolling();+ }+ }+ };++ if (socketService.socket?.connected) resyncAndSubscribe();+ const unsubConnect = socketService.on('connected', resyncAndSubscribe);+ const unsubDisconnect = socketService.on('disconnected', () => {+ if (!stoppedRef.current) startPolling();+ });+ const unsubLog = socketService.on('deploy_log', onDeployLog);+ const unsubStatus = socketService.on('deploy_status', onDeployStatus);++ // If the socket never connects, poll.+ const grace = setTimeout(() => {+ if (!socketService.socket?.connected && !stoppedRef.current) startPolling();+ }, SOCKET_GRACE_MS);++ return () => {+ cancelled = true;+ clearTimeout(grace);+ unsubConnect();+ unsubDisconnect();+ unsubLog();+ unsubStatus();+ socketService.unsubscribeDeploy(jobId);+ stopPolling();+ };+ }, [jobId, enabled, includePlan, mergeLines, applyJob, startPolling, stopPolling]);++ const isLive = !!job && !TERMINAL.has(job.status);++ return { job, lines, isLive, transport, error, loading, refetch };+}diff --git a/frontend/src/services/api/deploymentJobs.js b/frontend/src/services/api/deploymentJobs.jsindex b81cc845..d7b0110c 100644--- a/frontend/src/services/api/deploymentJobs.js+++ b/frontend/src/services/api/deploymentJobs.js@@ -10,11 +10,19 @@ export async function getDeploymentJobs(params = {}) {return this.request(`/deployment-jobs${suffix}`);}-export async function getDeploymentJob(jobId, includeLogs = true) {- return this.request(`/deployment-jobs/${jobId}?logs=${includeLogs}`);+export async function getDeploymentJob(jobId, includeLogs = true, includePlan = false) {+ const query = new URLSearchParams({ logs: String(includeLogs) });+ if (includePlan) query.append('plan', 'true');+ return this.request(`/deployment-jobs/${jobId}?${query}`);}export async function getDeploymentJobLogs(jobId, afterId = null) {const suffix = afterId ? `?after_id=${afterId}` : '';return this.request(`/deployment-jobs/${jobId}/logs${suffix}`);}++// Retry a failed deployment job — clones it and enqueues a fresh run (plan 51+// D8). Returns { job_id, job } for the new job so the console can swap to it.+export async function retryDeploymentJob(jobId) {+ return this.request(`/deployment-jobs/${jobId}/retry`, { method: 'POST' });+}diff --git a/frontend/src/services/socket.js b/frontend/src/services/socket.jsindex 28b0c281..91b9595c 100644--- a/frontend/src/services/socket.js+++ b/frontend/src/services/socket.js@@ -63,6 +63,16 @@ class SocketService {this.socket.on('notification', (data) => {this.emit('notification', data);});++ // Deploy Console live push (plan 51): batched log lines + status+ // summaries for a deployment job. Accelerator over after_id polling.+ this.socket.on('deploy_log', (data) => {+ this.emit('deploy_log', data);+ });++ this.socket.on('deploy_status', (data) => {+ this.emit('deploy_status', data);+ });}disconnect() {@@ -96,6 +106,18 @@ class SocketService {}}+ subscribeDeploy(jobId) {+ if (this.socket?.connected) {+ this.socket.emit('subscribe_deploy', { job_id: jobId });+ }+ }++ unsubscribeDeploy(jobId) {+ if (this.socket?.connected) {+ this.socket.emit('unsubscribe_deploy', { job_id: jobId });+ }+ }+on(event, callback) {if (!this.listeners.has(event)) {this.listeners.set(event, new Set());