This commit is contained in:
Erik Winter 2024-12-09 08:44:19 +01:00
parent 00a12ffa3f
commit c3e00455c9
2 changed files with 35 additions and 18 deletions

View File

@ -69,6 +69,17 @@ func (r *Recur) onMonths(date time.Time) bool {
}
}
func (r *Recur) NextAfter(old time.Time) time.Time {
day, _ := time.ParseDuration("1d")
test := old.Add(day)
for {
if r.On(test) || test.After(time.Date(2500, 1, 1, 0, 0, 0, 0, time.UTC)) {
return test
}
test.Add(day)
}
}
func (r *Recur) Valid() bool {
return r.Start.IsZero() || !slices.Contains(ValidPeriods, r.Period)
}

View File

@ -8,14 +8,16 @@ import (
)
type Recur struct {
repo Recurrer
logger *slog.Logger
repoSync Syncer
repoRecur Recurrer
logger *slog.Logger
}
func NewRecur(repo Recurrer, interval time.Duration, logger *slog.Logger) *Recur {
func NewRecur(repoRecur Recurrer, repoSync Syncer, interval time.Duration, logger *slog.Logger) *Recur {
r := &Recur{
repo: repo,
logger: logger,
repoRecur: repoRecur,
repoSync: repoSync,
logger: logger,
}
go r.run(interval)
@ -33,26 +35,30 @@ func (r *Recur) run(interval time.Duration) {
}
func (r *Recur) recur() error {
items, err := r.repo.RecursBefore(time.Now())
items, err := r.repoRecur.RecursBefore(time.Now())
if err != nil {
return err
}
for _, i := range items {
if err := r.repo.RecursNext(i.ID, NewNext(i.Recurrer, i.RecurNext)); err != nil {
// 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
}
func NewNext(r *item.Recur, old time.Time) time.Time {
day, _ := time.ParseDuration("1d")
test := old.Add(day)
for {
if r.On(test) || test.After(time.Date(2500, 1, 1, 0, 0, 0, 0, time.UTC)) {
return test
}
test.Add(day)
}
}