Add the TUI.
A table with one row per URL, a level-colored log pane and a status bar. New alerts flash their row and ring the bell. Keys pause, force checks, open the product page or cart link, and reload the config, which is validated first and applied in place, keeping each URL's timing. Headless mode and the TUI now share one watcher. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
8f0eda5d4d
commit
a19e6fa711
14 files changed
+1359
-105
No files matched your search
+70
-11
@@ -20,6 +20,10 @@ import (
|
||||
// tick is how often the scheduler looks for due URLs.
|
||||
const tick = time.Second
|
||||
|
||||
// queue is how many checks a host's worker can have waiting. With the
|
||||
// host floor of 10s, a full queue is over an hour of checks.
|
||||
const queue = 512
|
||||
|
||||
// Fetcher fetches pages. *fetch.Client is the real one; it waits for the
|
||||
// limiter's WaitCheck itself.
|
||||
type Fetcher interface {
|
||||
@@ -45,6 +49,7 @@ type Status struct {
|
||||
Item string
|
||||
Shop string
|
||||
URL string
|
||||
FetchURL string
|
||||
Host string
|
||||
Last time.Time
|
||||
Next time.Time
|
||||
@@ -52,6 +57,8 @@ type Status struct {
|
||||
Checking bool
|
||||
}
|
||||
|
||||
// target is one URL to check. Reload may change any field, so everything
|
||||
// is guarded by Scheduler.mu.
|
||||
type target struct {
|
||||
item config.Item
|
||||
url string // as configured
|
||||
@@ -59,11 +66,12 @@ type target struct {
|
||||
host string
|
||||
plugin *plugin.Plugin
|
||||
|
||||
// Guarded by Scheduler.mu.
|
||||
last time.Time
|
||||
next time.Time
|
||||
paused bool
|
||||
inFlight bool
|
||||
// removed is set when a reload drops the URL during its check.
|
||||
removed bool
|
||||
}
|
||||
|
||||
// Scheduler checks every URL at its interval. Each round, the due URLs are
|
||||
@@ -91,6 +99,50 @@ func New(ctx context.Context, cfg *config.Config, host *plugin.Host, fetcher Fet
|
||||
wake: make(chan struct{}, 1),
|
||||
}
|
||||
|
||||
targets, warnings, err := s.plan(ctx, cfg, host)
|
||||
if err != nil { return nil, nil, err }
|
||||
|
||||
s.targets = targets
|
||||
return s, warnings, nil
|
||||
}
|
||||
|
||||
// Reload switches to a new config. URLs in both configs keep their timing
|
||||
// and pause state; a changed interval applies from the next check. A
|
||||
// failed reload changes nothing.
|
||||
func (s *Scheduler) Reload(ctx context.Context, cfg *config.Config, host *plugin.Host) ([]string, error) {
|
||||
targets, warnings, err := s.plan(ctx, cfg, host)
|
||||
if err != nil { return nil, err }
|
||||
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
|
||||
old := map[string]*target{}
|
||||
for _, t := range s.targets { old[t.fetchURL] = t }
|
||||
|
||||
for i, t := range targets {
|
||||
prev, ok := old[t.fetchURL]
|
||||
if !ok { continue }
|
||||
|
||||
delete(old, t.fetchURL)
|
||||
|
||||
interval := prev.item.Interval.Duration
|
||||
prev.item, prev.url, prev.plugin = t.item, t.url, t.plugin
|
||||
if !prev.inFlight && t.item.Interval.Duration != interval { prev.next = t.next }
|
||||
|
||||
targets[i] = prev
|
||||
}
|
||||
|
||||
for _, t := range old { t.removed = true }
|
||||
|
||||
s.targets = targets
|
||||
s.poke()
|
||||
|
||||
return warnings, nil
|
||||
}
|
||||
|
||||
// plan builds the targets of cfg.
|
||||
func (s *Scheduler) plan(ctx context.Context, cfg *config.Config, host *plugin.Host) ([]*target, []string, error) {
|
||||
var targets []*target
|
||||
var warnings []string
|
||||
|
||||
seen := map[string]string{}
|
||||
@@ -116,18 +168,18 @@ func New(ctx context.Context, cfg *config.Config, host *plugin.Host, fetcher Fet
|
||||
u, err := url.Parse(fetchURL)
|
||||
if err != nil { return nil, nil, err }
|
||||
|
||||
s.targets = append(s.targets, &target{
|
||||
targets = append(targets, &target{
|
||||
item: item,
|
||||
url: raw,
|
||||
fetchURL: fetchURL,
|
||||
host: strings.ToLower(u.Hostname()),
|
||||
plugin: p,
|
||||
next: limiter.NextCheck(fetchURL, item.Interval.Duration),
|
||||
next: s.limiter.NextCheck(fetchURL, item.Interval.Duration),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
return s, warnings, nil
|
||||
return targets, warnings, nil
|
||||
}
|
||||
|
||||
// Results delivers every finished check. It is closed when Run returns.
|
||||
@@ -152,8 +204,7 @@ func (s *Scheduler) Run(ctx context.Context) {
|
||||
for _, t := range due {
|
||||
ch, ok := workers[t.host]
|
||||
if !ok {
|
||||
// Room for every target, so handing work over never blocks.
|
||||
ch = make(chan *target, len(s.targets))
|
||||
ch = make(chan *target, queue)
|
||||
workers[t.host] = ch
|
||||
|
||||
wg.Add(1)
|
||||
@@ -203,23 +254,30 @@ func (s *Scheduler) due() []*target {
|
||||
func (s *Scheduler) check(ctx context.Context, t *target) {
|
||||
if ctx.Err() != nil { return }
|
||||
|
||||
s.mu.Lock()
|
||||
item, rawURL, fetchURL, p := t.item, t.url, t.fetchURL, t.plugin
|
||||
s.mu.Unlock()
|
||||
|
||||
// The fetcher reserves the check with the limiter right before the request.
|
||||
resp, err := s.fetcher.Get(ctx, t.fetchURL)
|
||||
resp, err := s.fetcher.Get(ctx, fetchURL)
|
||||
if ctx.Err() != nil { return }
|
||||
|
||||
backoff := RecordOutcome(s.limiter, t.host, resp, err)
|
||||
|
||||
var report *plugin.Report
|
||||
if err == nil { report, err = plugin.Evaluate(ctx, t.plugin, t.url, resp) }
|
||||
if err == nil { report, err = plugin.Evaluate(ctx, p, rawURL, resp) }
|
||||
|
||||
s.mu.Lock()
|
||||
t.last = s.clock.Now()
|
||||
t.next = s.limiter.NextCheck(t.fetchURL, t.item.Interval.Duration)
|
||||
t.next = s.limiter.NextCheck(fetchURL, t.item.Interval.Duration)
|
||||
t.inFlight = false
|
||||
next := t.next
|
||||
last, next := t.last, t.next
|
||||
removed := t.removed
|
||||
s.mu.Unlock()
|
||||
|
||||
r := Result{Item: t.item, URL: t.url, Time: t.last, Report: report, Err: err, Backoff: backoff, Next: next}
|
||||
if removed { return }
|
||||
|
||||
r := Result{Item: item, URL: rawURL, Time: last, Report: report, Err: err, Backoff: backoff, Next: next}
|
||||
|
||||
select {
|
||||
case s.results <- r:
|
||||
@@ -282,6 +340,7 @@ func (s *Scheduler) Statuses() []Status {
|
||||
Item: t.item.Name,
|
||||
Shop: t.item.Shop,
|
||||
URL: t.url,
|
||||
FetchURL: t.fetchURL,
|
||||
Host: t.host,
|
||||
Last: t.last,
|
||||
Next: t.next,
|
||||
|
||||
@@ -179,3 +179,45 @@ func TestDuplicateURLs(t *testing.T) {
|
||||
|
||||
if _, _, err := New(context.Background(), cfg, host, nil, ratelimit.New(nil)); err == nil { t.Error("two spellings of one product were accepted") }
|
||||
}
|
||||
|
||||
func TestReload(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
clk := &clock{now: time.Date(2026, 10, 14, 12, 0, 0, 0, time.UTC)}
|
||||
|
||||
limiter := ratelimit.New(nil, ratelimit.WithClock(clk))
|
||||
|
||||
host, err := plugin.NewHost(nil, plugin.DefaultLimits(), slog.New(slog.NewTextHandler(io.Discard, nil)))
|
||||
if err != nil { t.Fatal(err) }
|
||||
defer host.Close()
|
||||
|
||||
f := &fetcher{limiter: limiter, clock: clk, requests: map[string][]time.Time{}, hosts: map[string][]time.Time{}}
|
||||
|
||||
s, _, err := New(ctx, &config.Config{Items: []config.Item{
|
||||
item("metro", ratelimit.URLFloor, "https://www.amazon.de/dp/B0HMX2FSTF", "https://www.amazon.co.uk/dp/B0HM497GJJ"),
|
||||
item("cable", ratelimit.URLFloor, "https://www.amazon.de/dp/B0DK51S84Z"),
|
||||
}}, host, f, limiter)
|
||||
if err != nil { t.Fatal(err) }
|
||||
|
||||
for _, tg := range s.due() { s.check(ctx, tg) }
|
||||
s.Pause("metro", true)
|
||||
|
||||
before := map[string]Status{}
|
||||
for _, st := range s.Statuses() { before[st.URL] = st }
|
||||
|
||||
// The DE URL stays under a new spelling, the UK one goes, cable is
|
||||
// renamed and a new item comes in.
|
||||
_, err = s.Reload(ctx, &config.Config{Items: []config.Item{
|
||||
item("metro", ratelimit.URLFloor, "https://www.amazon.de/dp/B0HMX2FSTF?th=1"),
|
||||
item("cable2", ratelimit.URLFloor, "https://www.amazon.de/dp/B0DK51S84Z"),
|
||||
item("disk", ratelimit.URLFloor, "https://www.amazon.de/dp/B076WS2WJ6"),
|
||||
}}, host)
|
||||
if err != nil { t.Fatal(err) }
|
||||
|
||||
got := s.Statuses()
|
||||
if len(got) != 3 { t.Fatalf("%d URLs after reload, want 3", len(got)) }
|
||||
|
||||
metro, cable, disk := got[0], got[1], got[2]
|
||||
if !metro.Paused || !metro.Last.Equal(before["https://www.amazon.de/dp/B0HMX2FSTF"].Last) { t.Errorf("metro lost its state: %+v", metro) }
|
||||
if cable.Item != "cable2" || !cable.Next.Equal(before["https://www.amazon.de/dp/B0DK51S84Z"].Next) { t.Errorf("cable lost its state: %+v", cable) }
|
||||
if disk.Paused || !disk.Last.IsZero() { t.Errorf("new item has old state: %+v", disk) }
|
||||
}
|
||||
Reference in new issue
Block a user