feat: recurring expenses and reports

This commit is contained in:
juancwu 2026-02-14 17:00:15 +00:00
commit 9e6ff67a87
23 changed files with 2943 additions and 56 deletions

View file

@ -0,0 +1,47 @@
package scheduler
import (
"context"
"log/slog"
"time"
"git.juancwu.dev/juancwu/budgit/internal/service"
)
type Scheduler struct {
recurringService *service.RecurringExpenseService
interval time.Duration
}
func New(recurringService *service.RecurringExpenseService) *Scheduler {
return &Scheduler{
recurringService: recurringService,
interval: 1 * time.Hour,
}
}
func (s *Scheduler) Start(ctx context.Context) {
// Run immediately on startup to catch up missed recurrences
s.run()
ticker := time.NewTicker(s.interval)
defer ticker.Stop()
for {
select {
case <-ctx.Done():
slog.Info("scheduler stopping")
return
case <-ticker.C:
s.run()
}
}
}
func (s *Scheduler) run() {
slog.Info("scheduler: processing due recurring expenses")
now := time.Now()
if err := s.recurringService.ProcessDueRecurrences(now); err != nil {
slog.Error("scheduler: failed to process recurring expenses", "error", err)
}
}