Files
magpie/internal/agent/rescan.go
T
yetoneandggbdpq 83fdd09c07 agent: the Agents page's re-detect asks where updates come from again (#4980)
latestVersion kept its answer for 6 hours under a constant key, and the
page's re-detect (Rescan) forgot everything but it. A version published
after magpie started showed no update until the memo ran out or magpie
restarted, though a fresh process asked npm at once and saw it.

Rescan now expires the kept latest versions, the way it already forgot
where commands were found and what the slow readers had said. Unlike the
patch's forget, the entries are only expired, not dropped: the value stays
until a new ask answers, so a re-detect while npm is down still shows the
last version known instead of none (a failed ask keeps the old value in
memo.get). An ask already running is left to finish.

Run on mote (macOS arm64, go1.26.8), sandbox HOME:
go test -tags nogui -race -count=1 -run "Rescan|Latest|Update"
./internal/agent/ ok. TestRescanAsksLatestAgain: 1.4.0, then 1.5.0 after
a Rescan, then still 1.5.0 when the next ask fails with a 502 (asked 1, 2,
3 times).
Broken by hand: without the expire in Rescan, red ("1.4.0, asked 1
times"); with an expire that deletes the entry, as the patch did, red ("a
failed ask after the refresh: latest \"\", asked 3 times").
Not tried: the Agents page against the real npm registry; Windows and
Linux.

Co-authored-by: ggbdpq <ggbdpq@gmail.com>
2026-11-06 05:53:50 +08:00

39 lines
1.2 KiB
Go

package agent
import (
"time"
"github.com/yetone/magpie/internal/proc"
)
// Rescan forgets what was found of the agents on this machine a while ago,
// so the next read of them looks again at once: the reader asked for it with
// the Agents page's refresh (#844), having just installed an agent, or
// removed one, or changed its settings outside magpie. What is read from an
// agent's files every time needs nothing forgotten; these are the answers
// kept for a while because asking was slow (the apps folder, wsl.exe, Alma's
// server), and where each command was found on PATH (proc.LookPath).
func Rescan() {
proc.Forget()
// where updates come from is asked again, too: a version published
// after magpie started showed no update until the memo ran out (#4980)
latests.expireAll()
cursorLocalSeen.Lock()
cursorLocalSeen.at = time.Time{}
cursorLocalSeen.Unlock()
wsl.Lock()
wsl.at, wsl.runAt = time.Time{}, time.Time{}
// a running distro is asked again, and one that failed is retried now
// rather than in ten minutes
wsl.probed, wsl.failed = map[string]bool{}, map[string]time.Time{}
wsl.Unlock()
almaRead.Lock()
almaRead.at, almaRead.body = nil, nil
almaRead.gen++
almaRead.Unlock()
}