Files
magpie/internal/cli/serve_follow.go
T
yetone 1315ac7d99 update: the first update finds a magpie without gateway.json, and a restart waits a bounded time (ljlwlcz on X)
Two edges of v0.1.3687's gateway.json:

1. The first update from a magpie older than the record found no gateway.json,
   asked 3425, said "isn't running" and left a service on MAGPIE_ADDR=0.0.0.0:10100
   on the old version. With no record, no MAGPIE_ADDR and no answer at the usual
   address, `magpie update` now tries the installed service's MAGPIE_ADDR (read
   from its unit or launch agent, daemon.Addr), the port in Settings and 3425,
   and takes the first that answers as magpie. --addr and MAGPIE_ADDR stay the
   user's word.
2. A restart asked by `magpie update` waited on "N requests" with no end (Windows
   hands nothing over, long streams never finish). The app and magpie serve now
   wait at most 10 minutes for that ask, then restart; the output says so
   ("or in 10 min at the latest, which cuts what is still running"). `magpie
   update --force` restarts at once (gateway.SetForceRestarter), and says if the
   running magpie is too old to know --force. The app's own look at the disk
   (followReplaced) and autonomous serve follow still never cut a request.

Run (mote refused SSH; win-run on xiaomi-gpu): go vet cli/gui/gateway/daemon
(windows), linux nogui vet; go test -run TestUpdate|TestServeFollow|
TestTerminalUpdate|TestRestart over cli, gui, gateway, daemon: ok except
TestUpdateThatWontStart, red on origin/main on the same box too (#6059).
Broken by hand: drain bound off, record fallback off, serve bound off: the new
tests TestTerminalUpdateWaitIsBoundedAndCanBeForced, TestUpdateFindsAMagpieThatWroteNoRecord,
TestServeFollowDrainIsBounded go red.
Not tried: a real systemd service upgrade; darwin cross vet (cgo) and the full
suite (started after the tag if mote is back).
2026-11-11 13:01:11 +08:00

180 lines
5.7 KiB
Go

package cli
import (
"context"
"log"
"os"
"sync"
"time"
"github.com/yetone/magpie/internal/gateway"
"github.com/yetone/magpie/internal/update"
)
// `magpie serve` follows an update put in where it runs from. An update
// is put in by another magpie: `magpie web` or the app updating itself
// from the same program, or `magpie update` in a terminal. None of them
// restarted the gateway `magpie serve` keeps, as the service `magpie
// daemon install` writes, a systemd user unit or a launch agent (acezan on
// Discord: `magpie web` as a system unit updated itself, and the user
// unit's serve ran on for good from "magpie.real (deleted)", the page
// saying an older magpie served the gateway; #4335 on a Mac, where the
// old bundle is deleted and TCC turns the process away from Desktop,
// Documents and Downloads). The updater can't reliably restart it: a
// system unit doesn't reach the user's systemctl, and the app may not be
// running at all. So serve looks itself, as the app and magpie web do
// (gui's followReplaced): once the program it started from is another
// file, it waits until the gateway is idle, then runs the new program in
// its own place, the same process under the same service manager or in
// the same terminal. A build of one's own (not a release) never does,
// since a rebuild in place would restart it.
// serveFollowTick is how often serve looks at its program on disk;
// serveIdleSettle is how long nothing has ended before it counts as idle
// (the gap of a tool chain, as gui's idleSettle); serveWaitMost is how
// long it waits for that before it restarts once nothing is in flight,
// however recent. Vars for the tests.
var (
serveFollowTick = 5 * time.Second
serveIdleSettle = 10 * time.Second
serveWaitMost = time.Hour
// serveDrainMost is how long a restart `magpie update` asked for
// waits for what is in flight; then serve restarts anyway
serveDrainMost = 10 * time.Minute
)
// serveReexec runs the new program in serve's place; a var for the tests.
var serveReexec = update.Reexec
// serveFollow watches exe, the program serve started from (started is its
// os.Stat then), and calls stop once it has been replaced and busy says
// the gateway is idle. Restart reports the program to run in this one's
// place once stop has been called for it, "" otherwise.
type serveFollow struct {
exe string
started os.FileInfo
busy func() gateway.Busy
released bool
stop func()
mu sync.Mutex
due time.Time // when the replaced program was first seen
asked time.Time // when `magpie update` asked for the restart while busy
stopped bool
}
// newServeFollow follows the program this process runs, for s; nil when
// it can't be told (no path, or no file there).
func newServeFollow(s *gateway.Server, stop func()) *serveFollow {
exe, err := update.Executable()
if err != nil {
return nil
}
st, err := os.Stat(exe)
if err != nil {
return nil
}
return &serveFollow{exe: exe, started: st, busy: s.Busy, released: update.Released(version), stop: stop}
}
// run looks every serveFollowTick until ctx ends or serve is stopped.
func (f *serveFollow) run(ctx context.Context) {
if f == nil || !f.released {
return
}
t := time.NewTicker(serveFollowTick)
defer t.Stop()
for {
select {
case <-ctx.Done():
return
case <-t.C:
}
if f.check(time.Now()) {
return
}
}
}
// check stops serve when the program was replaced and the gateway is idle
// at now; it reports whether it did.
func (f *serveFollow) check(now time.Time) bool {
if !update.Replaced(f.exe, f.started) {
return false
}
f.mu.Lock()
if f.due.IsZero() {
f.due = now
log.Printf("magpie serve: %s was updated; restarting into it once the gateway is idle", f.exe)
}
waited := now.Sub(f.due)
f.mu.Unlock()
f.mu.Lock()
drained := !f.asked.IsZero() && now.Sub(f.asked) >= serveDrainMost
f.mu.Unlock()
b := f.busy()
if !drained && (b.Any() || now.Sub(b.Last) < serveIdleSettle && waited < serveWaitMost) {
return false
}
f.mu.Lock()
f.stopped = true
f.mu.Unlock()
f.stop()
return true
}
// restartTo is the program to run in serve's place, "" when serve wasn't
// stopped for an update.
func (f *serveFollow) restartTo() string {
if f == nil {
return ""
}
f.mu.Lock()
defer f.mu.Unlock()
if !f.stopped {
return ""
}
return f.exe
}
// answer is what `magpie update`'s ask (gateway.RestartPath) is told: serve
// restarts into what it put in, at once when the gateway is idle (after
// the answer is out), else once it is, as on its own next look; "current"
// when nothing was put in where serve runs from, "unsupported" for a build
// of one's own, restarted the way it was started.
func (f *serveFollow) answer(string) gateway.RestartReply {
switch {
case f == nil || !f.released:
return gateway.RestartReply{State: "unsupported"}
case !update.Replaced(f.exe, f.started):
return gateway.RestartReply{State: "current"}
}
b := f.busy()
if !b.Any() && time.Since(b.Last) >= serveIdleSettle {
go f.check(time.Now())
return gateway.RestartReply{State: "restarting"}
}
f.mu.Lock()
if f.asked.IsZero() {
f.asked = time.Now()
}
f.mu.Unlock()
return gateway.RestartReply{State: "waiting", Busy: &b, Drain: int(serveDrainMost / time.Second)}
}
// answerNow is answer for `magpie update --force`: serve restarts at once,
// whatever is in flight.
func (f *serveFollow) answerNow(string) gateway.RestartReply {
switch {
case f == nil || !f.released:
return gateway.RestartReply{State: "unsupported"}
case !update.Replaced(f.exe, f.started):
return gateway.RestartReply{State: "current"}
}
f.mu.Lock()
f.asked = time.Now().Add(-serveDrainMost)
f.mu.Unlock()
go f.check(time.Now())
return gateway.RestartReply{State: "restarting", Forced: true}
}