mirror of
https://github.com/yusing/godoxy.git
synced 2026-03-25 18:41:10 +01:00
This major overhaul of the idlewatcher system introduces a modern, real-time loading experience with Server-Sent Events (SSE) streaming and improved error handling. - **Real-time Event Streaming**: New SSE endpoint (`/$godoxy/wake-events`) provides live updates during container wake process - **Enhanced Loading Page**: Modern console-style interface with timestamped events and color-coded status messages - **Improved Static Asset Management**: Dedicated paths for CSS, JS, and favicon to avoid conflicting with upstream assets - **Event History Buffer**: Stores wake events for reconnecting clients and debugging - Refactored HTTP request handling with cleaner static asset routing - Added `WakeEvent` system with structured event types (starting, waking_dep, dep_ready, container_woke, waiting_ready, ready, error) - Implemented thread-safe event broadcasting using xsync.Map for concurrent SSE connections - Enhanced error handling with detailed logging and user-friendly error messages - Simplified loading page template system with better asset path management - Fixed race conditions in dependency waking and state management - Removed `common.go` functions (canceled, waitStarted) - moved inline for better context - Updated Waker interface to accept context parameter in Wake() method - New static asset paths use `/$godoxy/` prefix to avoid conflicts - Console-style output with Fira Code font for better readability - Color-coded event types (yellow for starting, blue for dependencies, green for success, red for errors) - Automatic page refresh when container becomes ready - Improved visual design with better glassmorphism effects and responsive layout - Real-time progress feedback during dependency wake and container startup This change transforms the static loading page into a dynamic, informative experience that keeps users informed during the wake process while maintaining backward compatibility with existing routing behavior.
168 lines
3.8 KiB
Go
168 lines
3.8 KiB
Go
package idlewatcher
|
|
|
|
import (
|
|
"time"
|
|
|
|
idlewatcher "github.com/yusing/godoxy/internal/idlewatcher/types"
|
|
"github.com/yusing/godoxy/internal/types"
|
|
gperr "github.com/yusing/goutils/errs"
|
|
"github.com/yusing/goutils/task"
|
|
)
|
|
|
|
// Start implements health.HealthMonitor.
|
|
func (w *Watcher) Start(parent task.Parent) gperr.Error {
|
|
w.task.OnCancel("route_cleanup", func() {
|
|
parent.Finish(w.task.FinishCause())
|
|
})
|
|
return nil
|
|
}
|
|
|
|
// Task implements health.HealthMonitor.
|
|
func (w *Watcher) Task() *task.Task {
|
|
return w.task
|
|
}
|
|
|
|
// Finish implements health.HealthMonitor.
|
|
func (w *Watcher) Finish(reason any) {
|
|
if w.stream != nil {
|
|
w.stream.Close()
|
|
}
|
|
}
|
|
|
|
// Name implements health.HealthMonitor.
|
|
func (w *Watcher) Name() string {
|
|
return w.cfg.ContainerName()
|
|
}
|
|
|
|
// String implements health.HealthMonitor.
|
|
func (w *Watcher) String() string {
|
|
return w.Name()
|
|
}
|
|
|
|
// Uptime implements health.HealthMonitor.
|
|
func (w *Watcher) Uptime() time.Duration {
|
|
return 0
|
|
}
|
|
|
|
// Latency implements health.HealthMonitor.
|
|
func (w *Watcher) Latency() time.Duration {
|
|
return 0
|
|
}
|
|
|
|
// Status implements health.HealthMonitor.
|
|
func (w *Watcher) Status() types.HealthStatus {
|
|
state := w.state.Load()
|
|
if state.err != nil {
|
|
return types.StatusError
|
|
}
|
|
if state.ready {
|
|
return types.StatusHealthy
|
|
}
|
|
if state.status == idlewatcher.ContainerStatusRunning {
|
|
return types.StatusStarting
|
|
}
|
|
return types.StatusNapping
|
|
}
|
|
|
|
// Detail implements health.HealthMonitor.
|
|
func (w *Watcher) Detail() string {
|
|
state := w.state.Load()
|
|
if state.err != nil {
|
|
return state.err.Error()
|
|
}
|
|
if !state.ready {
|
|
return "not ready"
|
|
}
|
|
if state.status == idlewatcher.ContainerStatusRunning {
|
|
return "starting"
|
|
}
|
|
return "napping"
|
|
}
|
|
|
|
// MarshalJSON implements health.HealthMonitor.
|
|
func (w *Watcher) MarshalJSON() ([]byte, error) {
|
|
url := w.hc.URL()
|
|
if url.Port() == "0" {
|
|
url = nil
|
|
}
|
|
var detail string
|
|
if err := w.error(); err != nil {
|
|
detail = err.Error()
|
|
}
|
|
return (&types.HealthJSONRepr{
|
|
Name: w.Name(),
|
|
Status: w.Status(),
|
|
Config: &types.HealthCheckConfig{
|
|
Interval: idleWakerCheckInterval,
|
|
Timeout: idleWakerCheckTimeout,
|
|
},
|
|
URL: url,
|
|
Detail: detail,
|
|
}).MarshalJSON()
|
|
}
|
|
|
|
func (w *Watcher) checkUpdateState() (ready bool, err error) {
|
|
// the new container info not yet updated
|
|
if w.hc.URL().Host == "" {
|
|
return false, nil
|
|
}
|
|
|
|
state := w.state.Load()
|
|
|
|
// Check if container has been starting for too long (timeout after WakeTimeout)
|
|
if !state.startedAt.IsZero() {
|
|
elapsed := time.Since(state.startedAt)
|
|
if elapsed > w.cfg.WakeTimeout {
|
|
err := gperr.Errorf("container failed to become ready within %v (started at %v, %d health check attempts)",
|
|
w.cfg.WakeTimeout, state.startedAt, state.healthTries)
|
|
w.l.Error().
|
|
Dur("elapsed", elapsed).
|
|
Time("started_at", state.startedAt).
|
|
Int("health_tries", state.healthTries).
|
|
Msg("container startup timeout")
|
|
w.setError(err)
|
|
return false, err
|
|
}
|
|
}
|
|
|
|
res, err := w.hc.CheckHealth()
|
|
if err != nil {
|
|
w.l.Debug().Err(err).Msg("health check error")
|
|
w.setError(err)
|
|
return false, err
|
|
}
|
|
|
|
if res.Healthy {
|
|
w.l.Debug().
|
|
Dur("startup_time", time.Since(state.startedAt)).
|
|
Int("health_tries", state.healthTries+1).
|
|
Msg("container ready")
|
|
w.setReady()
|
|
return true, nil
|
|
}
|
|
|
|
// Health check failed, increment counter and log
|
|
newHealthTries := state.healthTries + 1
|
|
w.state.Store(&containerState{
|
|
status: state.status,
|
|
ready: false,
|
|
err: state.err,
|
|
startedAt: state.startedAt,
|
|
healthTries: newHealthTries,
|
|
})
|
|
|
|
// log every 3 seconds
|
|
const everyN = int(3 * time.Second / idleWakerCheckInterval)
|
|
if newHealthTries%everyN == 0 {
|
|
url := w.hc.URL()
|
|
w.l.Debug().
|
|
Int("health_tries", newHealthTries).
|
|
Dur("elapsed", time.Since(state.startedAt)).
|
|
Str("url", url.String()).
|
|
Str("detail", res.Detail).
|
|
Msg("health check failed, still starting")
|
|
}
|
|
|
|
return false, nil
|
|
}
|