package web import ( "context" "errors" "net/http" "sync" "time" "supermarket/internal/ai" "supermarket/internal/inventory" "supermarket/internal/jobs" "supermarket/internal/profile" ) // A list imported into the Market and filled in from the websites runs in // the background; where it is is kept as a setting, so every copy of the // app shows it. const ( marketFillKey = "market:fill" marketFillLock = "market-fill" // marketFillMax is how long a whole list may take. marketFillMax = 2 * time.Hour ) // marketFillState is the last list filled in. type marketFillState struct { Running bool `json:"running"` Stopped bool `json:"stopped,omitempty"` // the app stopped before it was done Started time.Time `json:"started"` Finished time.Time `json:"finished,omitzero"` Total int `json:"total"` Lines []inventory.FillLine `json:"lines"` } func (f *marketFillState) Done() int { return len(f.Lines) } // Count is how many entries had the outcome. func (f *marketFillState) Count(outcome string) int { n := 0 for _, l := range f.Lines { if l.Outcome == outcome { n++ } } return n } // marketFill is the last list filled in, or nil. One that says it's // running when nothing holds its lock was stopped with the app. func (s *Server) marketFill() *marketFillState { var f marketFillState ok, err := s.DB.GetJSON(marketFillKey, &f) if err != nil { s.Log.Warn("market: reading the fill", "err", err) } if !ok || err != nil { return nil } if f.Running { _, release, err := s.jobs().TryLock(context.Background(), s.lockName(marketFillLock)) if err == nil { release() f.Running, f.Stopped = false, true _ = s.DB.PutJSON(marketFillKey, f) } } return &f } // startMarketFill adds the entries in the background, each filled in from // its website. func (s *Server) startMarketFill(entries []inventory.Entry, overwrite bool) error { if len(entries) == 0 { return errors.New("the list has no entries") } cfg, err := ai.LoadConfig(s.DB) if err != nil { return err } client, err := ai.New(cfg, s.Secrets) if err != nil { return errors.New(err.Error() + ": set up the AI to fill entries in") } ours, err := profile.Load(s.DB) if err != nil { return err } lctx, release, err := s.jobs().TryLock(context.Background(), s.lockName(marketFillLock)) if errors.Is(err, jobs.ErrBusy) { return errors.New("a list is already being filled in: wait for it to finish") } if err != nil { return err } st := marketFillState{Running: true, Started: time.Now(), Total: len(entries)} if err := s.DB.PutJSON(marketFillKey, st); err != nil { release() return err } go func() { defer release() ctx, cancel := context.WithTimeout(lctx, marketFillMax) defer cancel() var mu sync.Mutex save := func() { if err := s.DB.PutJSON(marketFillKey, st); err != nil { s.Log.Warn("market: saving the fill's progress", "err", err) } } inventory.FillImport(ctx, s.DB, entries, inventory.FillOptions{ Client: client, Ours: ours, Overwrite: overwrite, Room: s.limitSources, Done: func(l inventory.FillLine) { mu.Lock() defer mu.Unlock() st.Lines = append(st.Lines, l) save() }, }) mu.Lock() defer mu.Unlock() st.Running, st.Finished = false, time.Now() save() }() return nil } // inventoryFillStatus is polled while a list is filled in: the progress // card, which stops polling once it's done. func (s *Server) inventoryFillStatus(w http.ResponseWriter, r *http.Request) { s.fragment(w, r, "import", "fill-status", s.marketFill()) }