feat(idlesleep): support container dependencies, including custom and docker depends_on, code refactor

This commit is contained in:
yusing
2025-06-04 23:26:38 +08:00
parent 22ab043e06
commit a39d527fc1
15 changed files with 507 additions and 152 deletions

View File

@@ -1,8 +1,6 @@
package idlewatcher
import (
"context"
"errors"
"net/http"
"strconv"
"time"
@@ -38,10 +36,6 @@ func (w *Watcher) ServeHTTP(rw http.ResponseWriter, r *http.Request) {
return
default:
f := &ForceCacheControl{expires: w.expires().Format(http.TimeFormat), ResponseWriter: rw}
w, ok := watcherMap[w.Key()] // could've been reloaded
if !ok {
return
}
w.rp.ServeHTTP(f, r)
}
}
@@ -50,6 +44,14 @@ func isFaviconPath(path string) bool {
return path == "/favicon.ico"
}
func (w *Watcher) redirectToStartEndpoint(rw http.ResponseWriter, r *http.Request) {
uri := "/"
if w.cfg.StartEndpoint != "" {
uri = w.cfg.StartEndpoint
}
http.Redirect(rw, r, uri, http.StatusTemporaryRedirect)
}
func (w *Watcher) wakeFromHTTP(rw http.ResponseWriter, r *http.Request) (shouldNext bool) {
w.resetIdleTimer()
@@ -92,7 +94,7 @@ func (w *Watcher) wakeFromHTTP(rw http.ResponseWriter, r *http.Request) (shouldN
ctx := r.Context()
if w.cancelled(ctx) {
gphttp.ServerError(rw, r, context.Cause(ctx), http.StatusServiceUnavailable)
w.redirectToStartEndpoint(rw, r)
return false
}
@@ -103,17 +105,19 @@ func (w *Watcher) wakeFromHTTP(rw http.ResponseWriter, r *http.Request) (shouldN
return false
}
var ready bool
for {
w.resetIdleTimer()
if w.cancelled(ctx) {
gphttp.ServerError(rw, r, context.Cause(ctx), http.StatusServiceUnavailable)
w.redirectToStartEndpoint(rw, r)
return false
}
w, ready, err = checkUpdateState(w.Key())
if !w.waitStarted(ctx) {
return false
}
ready, err := w.checkUpdateState()
if err != nil {
gphttp.ServerError(rw, r, err)
return false