Files
spsays-bff/internal/worker/reminder_worker.go

42 lines
1.0 KiB
Go

package worker
import (
"context"
"time"
"github.com/prabhupadasays/spsays-bff/internal/service"
"go.uber.org/zap"
)
// ReminderWorker ticks every reminderInterval and asks the ReminderService to
// fire any sādhana reminders due at that moment (in each user's local time).
// The window inside the service is wider than this interval, so no target
// time slips between ticks.
type ReminderWorker struct {
svc *service.ReminderService
logger *zap.Logger
}
const reminderInterval = 15 * time.Minute
func NewReminderWorker(svc *service.ReminderService, logger *zap.Logger) *ReminderWorker {
return &ReminderWorker{svc: svc, logger: logger}
}
// Run starts the ticker loop in a goroutine; it exits when ctx is cancelled.
func (w *ReminderWorker) Run(ctx context.Context) {
go func() {
w.logger.Info("sadhna reminder worker started", zap.Duration("interval", reminderInterval))
t := time.NewTicker(reminderInterval)
defer t.Stop()
for {
select {
case <-ctx.Done():
return
case <-t.C:
w.svc.RunOnce(ctx, time.Now())
}
}
}()
}