summaryrefslogtreecommitdiff
path: root/internal/scheduler/recurrence.go
blob: 3d48d8f214fbd6d8ba269196bf017d7f1600becc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
package scheduler

import (
	"context"
	"log"
	"time"

	"task-dashboard/internal/config"
	"task-dashboard/internal/store"
)

// RunRecurrenceCheck ticks every interval, calling AdvanceDueRecurringTasks
// until ctx is cancelled. Errors are logged, not fatal -- one bad tick
// shouldn't kill the loop; the next tick tries again.
func RunRecurrenceCheck(ctx context.Context, s *store.Store, interval time.Duration) {
	ticker := time.NewTicker(interval)
	defer ticker.Stop()
	for {
		select {
		case <-ctx.Done():
			return
		case <-ticker.C:
			n, err := s.AdvanceDueRecurringTasks(config.Now())
			if err != nil {
				log.Printf("ERROR [RecurrenceCheck]: %v", err)
				continue
			}
			if n > 0 {
				log.Printf("RecurrenceCheck: created %d next iteration(s)", n)
			}
		}
	}
}