A refresh's saves waited out saveGap (1 min) after the last one written,
the scan's checkpoints and its end too: the first checkpoint was written,
the rest waited. A magpie ended in that minute (quit, updated, `web` or
`serve` stopped by SIGTERM, whose handler died without sessions.Saved())
kept a few hundred of the files it read, and the next start read every
rollout again.
- A refresh that reads scanFiles (32) files or more is a scan: its
checkpoints wait scanSaveGap (15 s) and its last save waits for nothing.
A poll's few changed files still wait out saveGap. Saves folded into one
take the shortest gap asked, and a shorter one wakes a waiting save.
- The signal handler writes the index before dying, bounded by
saveOnSignal (5 s).
Real history in a sandbox HOME (11,994 Codex rollouts, 52.5 GB, plus
Claude projects; 17,140 files), index removed, `magpie web` killed by
SIGTERM ~44 s in (right after the first /api/sessions answered):
- v0.1.2521 tree: 1,200 entries kept (1,415 on a second try). Restart:
/api/sessions 23.63 s, 1:38 of CPU.
- this change: 17,140 kept. Restart: /api/sessions 1.30 s, 3.4 s of CPU.
Tests, on mote: TestScanSavedWithoutTheGap and
TestSignalSavesTheSessionIndex are new. Broken by hand: with the scan's
gaps left at saveGap the first fails ("a scan's last save waited out
saveGap"); with the save call taken out of the handler the second fails
("died without the session index saved"). go test ./internal/sessions/
./internal/cli/ ok. gofmt -l clean; go vet (darwin), GOOS=windows go vet,
GOOS=linux go vet -tags nogui of both packages ok.
Not run: the full suite; the GUI app's own quit and update paths (they
return to main, which already called Saved); Windows and Linux runs.
Picked onto 0c3163804 (origin/main at push); not run again there.
137 lines
4.3 KiB
Go
137 lines
4.3 KiB
Go
package cli
|
|
|
|
import (
|
|
"context"
|
|
"os"
|
|
"os/signal"
|
|
"sync"
|
|
"sync/atomic"
|
|
"syscall"
|
|
"time"
|
|
|
|
"github.com/yetone/magpie/internal/gateway"
|
|
"github.com/yetone/magpie/internal/proc"
|
|
"github.com/yetone/magpie/internal/provider"
|
|
"github.com/yetone/magpie/internal/sessions"
|
|
)
|
|
|
|
// endProbesOnSignal: the probes magpie asks a CLI with lead a session of
|
|
// their own (proc.ProbeContext), so Ctrl+C in the terminal, the terminal
|
|
// closing or a service manager's SIGTERM reach magpie and not them, and
|
|
// magpie ending on the signal left them to init. They are ended first, and
|
|
// magpie then dies of the signal. A command that waits on Ctrl+C itself
|
|
// (interruptContext) is left to finish its own way, its probes ended all the
|
|
// same; the app and the TUI, which quit on SIGINT and SIGTERM their own way,
|
|
// take those back (ownSignals). A signal magpie was started ignoring (nohup,
|
|
// a background job) stays ignored: asking for it would undo that.
|
|
func endProbesOnSignal() {
|
|
caught = nil
|
|
for _, s := range []os.Signal{os.Interrupt, syscall.SIGTERM, syscall.SIGHUP} {
|
|
if !signal.Ignored(s) {
|
|
caught = append(caught, s)
|
|
}
|
|
}
|
|
if len(caught) == 0 {
|
|
return
|
|
}
|
|
onSignal = make(chan os.Signal, 1)
|
|
signal.Notify(onSignal, caught...)
|
|
go func(sigs chan os.Signal) {
|
|
for s := range sigs {
|
|
// asked before the probes are ended, which can take a moment
|
|
// the command may spend finishing, and stop asking
|
|
handled := s == os.Interrupt && interrupts.Load() > 0
|
|
proc.EndProbes()
|
|
if !handled {
|
|
provider.FlushServed() // a gateway's last answers, noted and not yet written
|
|
gateway.FlushSticks() // and who answered which conversation
|
|
// and the session index, or the next start reads every
|
|
// file a scan read since the last save again
|
|
savedWithin(sessionsSaved, saveOnSignal)
|
|
die(s)
|
|
}
|
|
}
|
|
}(onSignal)
|
|
}
|
|
|
|
// saveOnSignal bounds how long a signal waits for the session index to be
|
|
// written: a long history's takes a second or two, and a signal is asked to
|
|
// end magpie now.
|
|
var saveOnSignal = 5 * time.Second
|
|
|
|
// sessionsSaved is sessions.Saved; a var so tests can hold it.
|
|
var sessionsSaved = sessions.Saved
|
|
|
|
// savedWithin runs save and waits for it up to d.
|
|
func savedWithin(save func(), d time.Duration) {
|
|
done := make(chan struct{})
|
|
go func() { save(); close(done) }()
|
|
select {
|
|
case <-done:
|
|
case <-time.After(d):
|
|
}
|
|
}
|
|
|
|
// caught are the signals endProbesOnSignal asked for; onSignal is where
|
|
// they come.
|
|
var (
|
|
caught []os.Signal
|
|
onSignal chan os.Signal
|
|
)
|
|
|
|
// die ends magpie of s, as s would have had there been no handler: the
|
|
// shell that ran it sees a process a signal ended (a loop running magpie
|
|
// stops on Ctrl+C), not one that exited. Where a signal can't be sent to
|
|
// oneself (Windows), it exits as a shell reports such a process. A var so
|
|
// tests can see what a signal would do.
|
|
var die = func(s os.Signal) {
|
|
raise(s)
|
|
code := 1
|
|
if n, ok := s.(syscall.Signal); ok {
|
|
code = 128 + int(n)
|
|
}
|
|
os.Exit(code)
|
|
}
|
|
|
|
// ownSignals hands SIGINT and SIGTERM back, for a command that quits on
|
|
// them its own way: Wails ends the app through OnShutdown (which ends the
|
|
// probes, and installs a downloaded update), bubbletea leaves the TUI's
|
|
// screen as it found it, and each comes back to main, where the probes are
|
|
// ended too. Exiting first skipped all of that. SIGHUP stays: neither
|
|
// handles it, and the terminal closing would end magpie there and then,
|
|
// with its probes, out of the terminal's reach, left running.
|
|
func ownSignals() {
|
|
if onSignal == nil {
|
|
return
|
|
}
|
|
signal.Stop(onSignal)
|
|
for _, s := range caught {
|
|
if s == syscall.SIGHUP {
|
|
signal.Notify(onSignal, s)
|
|
}
|
|
}
|
|
}
|
|
|
|
// leaveSignals hands every signal endProbesOnSignal asked for back, for a
|
|
// command that starts no probe and ends on each its own way.
|
|
func leaveSignals() {
|
|
if onSignal != nil {
|
|
signal.Stop(onSignal)
|
|
}
|
|
}
|
|
|
|
// interrupts counts the commands ending themselves on Ctrl+C.
|
|
var interrupts atomic.Int32
|
|
|
|
// interruptContext is signal.NotifyContext(…, os.Interrupt), for a command
|
|
// that ends itself on Ctrl+C rather than being ended by it.
|
|
func interruptContext() (context.Context, context.CancelFunc) {
|
|
interrupts.Add(1)
|
|
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt)
|
|
var once sync.Once
|
|
return ctx, func() {
|
|
stop()
|
|
once.Do(func() { interrupts.Add(-1) })
|
|
}
|
|
}
|