package main import ( "log/slog" "time" "go-mod.ewintr.nl/planner/item" ) type Recur struct { repoSync Syncer repoRecur Recurrer logger *slog.Logger } func NewRecur(repoRecur Recurrer, repoSync Syncer, interval time.Duration, logger *slog.Logger) *Recur { r := &Recur{ repoRecur: repoRecur, repoSync: repoSync, logger: logger, } go r.run(interval) return r } func (r *Recur) run(interval time.Duration) { ticker := time.NewTicker(interval) for range ticker.C { if err := r.recur(); err != nil { r.logger.Error("could not recur", "error", err) } } } func (r *Recur) recur() error { items, err := r.repoRecur.RecursBefore(time.Now()) if err != nil { return err } for _, i := range items { // spawn instance ne, err := item.NewEvent(i) if err != nil { return err } ne.Start = i.RecurNext // TODO something with time ni, err := ne.Item() if err != nil { return err } if err := r.repoSync.Update(ni); err != nil { return err } // set next if err := r.repoRecur.RecursNext(i.ID, i.Recurrer.NextAfter(i.RecurNext)); err != nil { return err } } return nil }