wip
This commit is contained in:
parent
00a12ffa3f
commit
c3e00455c9
|
@ -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 {
|
func (r *Recur) Valid() bool {
|
||||||
return r.Start.IsZero() || !slices.Contains(ValidPeriods, r.Period)
|
return r.Start.IsZero() || !slices.Contains(ValidPeriods, r.Period)
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,14 +8,16 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
type Recur struct {
|
type Recur struct {
|
||||||
repo Recurrer
|
repoSync Syncer
|
||||||
logger *slog.Logger
|
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{
|
r := &Recur{
|
||||||
repo: repo,
|
repoRecur: repoRecur,
|
||||||
logger: logger,
|
repoSync: repoSync,
|
||||||
|
logger: logger,
|
||||||
}
|
}
|
||||||
go r.run(interval)
|
go r.run(interval)
|
||||||
|
|
||||||
|
@ -33,26 +35,30 @@ func (r *Recur) run(interval time.Duration) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Recur) recur() error {
|
func (r *Recur) recur() error {
|
||||||
items, err := r.repo.RecursBefore(time.Now())
|
items, err := r.repoRecur.RecursBefore(time.Now())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
for _, i := range items {
|
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 err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
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)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
Loading…
Reference in New Issue