package worker import ( "context" "log" "time" "git.techease.ru/Smart-search/smart-search-back/internal/repository" ) type InviteCleaner struct { inviteRepo repository.InviteRepository ctx context.Context ticker *time.Ticker done chan bool } func NewInviteCleaner(ctx context.Context, inviteRepo repository.InviteRepository) *InviteCleaner { return &InviteCleaner{ inviteRepo: inviteRepo, ctx: ctx, done: make(chan bool), } } func (w *InviteCleaner) Start() { w.ticker = time.NewTicker(6 * time.Hour) w.deactivateExpiredInvites() go func() { for { select { case <-w.ticker.C: w.deactivateExpiredInvites() case <-w.done: return case <-w.ctx.Done(): log.Println("Invite cleaner context cancelled, stopping worker") return } } }() log.Println("Invite cleaner worker started (runs every 6 hours)") } func (w *InviteCleaner) Stop() { if w.ticker != nil { w.ticker.Stop() } select { case w.done <- true: default: } log.Println("Invite cleaner worker stopped") } func (w *InviteCleaner) deactivateExpiredInvites() { count, err := w.inviteRepo.DeactivateExpired(w.ctx) if err != nil { log.Printf("Error deactivating expired invites: %v", err) return } if count > 0 { log.Printf("Deactivated %d expired invite codes", count) } }