2024-12-01 10:22:47 +01:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2024-12-22 08:37:12 +01:00
|
|
|
"fmt"
|
2024-12-01 10:22:47 +01:00
|
|
|
"log/slog"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/google/uuid"
|
|
|
|
"go-mod.ewintr.nl/planner/item"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Recur struct {
|
|
|
|
repoSync Syncer
|
|
|
|
repoRecur Recurrer
|
|
|
|
logger *slog.Logger
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewRecur(repoRecur Recurrer, repoSync Syncer, logger *slog.Logger) *Recur {
|
|
|
|
r := &Recur{
|
|
|
|
repoRecur: repoRecur,
|
|
|
|
repoSync: repoSync,
|
|
|
|
logger: logger,
|
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
2024-12-22 08:37:12 +01:00
|
|
|
r.logger.Info("start looking for recurring items")
|
2024-12-01 10:22:47 +01:00
|
|
|
items, err := r.repoRecur.RecursBefore(time.Now())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-12-22 08:37:12 +01:00
|
|
|
r.logger.Info("found recurring items", "count", len(items))
|
2024-12-01 10:22:47 +01:00
|
|
|
for _, i := range items {
|
2024-12-22 08:37:12 +01:00
|
|
|
r.logger.Info("processing recurring item", "item", fmt.Sprintf("%+v", i))
|
2024-12-01 10:22:47 +01:00
|
|
|
// spawn instance
|
|
|
|
ne, err := item.NewEvent(i)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-12-22 08:37:12 +01:00
|
|
|
r.logger.Info("processing recurring event", "event", fmt.Sprintf("%+v", ne))
|
2024-12-01 10:22:47 +01:00
|
|
|
y, m, d := i.RecurNext.Date()
|
|
|
|
ne.ID = uuid.New().String()
|
|
|
|
ne.Recurrer = nil
|
|
|
|
ne.RecurNext = time.Time{}
|
|
|
|
ne.Start = time.Date(y, m, d, ne.Start.Hour(), ne.Start.Minute(), 0, 0, time.UTC)
|
2024-12-22 08:37:12 +01:00
|
|
|
r.logger.Info("created instance of recurring event", "event", fmt.Sprintf("%+v", ne))
|
2024-12-01 10:22:47 +01:00
|
|
|
|
|
|
|
ni, err := ne.Item()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := r.repoSync.Update(ni, time.Now()); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-12-22 08:37:12 +01:00
|
|
|
r.logger.Info("storen instance of recurring event", "recEventID", ne.ID, "instanceID", ni.ID)
|
2024-12-01 10:22:47 +01:00
|
|
|
|
|
|
|
// set next
|
2024-12-22 08:37:12 +01:00
|
|
|
next := i.Recurrer.NextAfter(i.RecurNext)
|
|
|
|
if err := r.repoRecur.RecursNext(i.ID, next, time.Now()); err != nil {
|
2024-12-01 10:22:47 +01:00
|
|
|
return err
|
|
|
|
}
|
2024-12-22 08:37:12 +01:00
|
|
|
r.logger.Info("updated recur date", "recEventID", ne.ID, "next", next)
|
2024-12-01 10:22:47 +01:00
|
|
|
}
|
|
|
|
r.logger.Info("processed recurring items", "count", len(items))
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|