feat: add episode service with background refresh worker

This commit is contained in:
2026-05-17 21:16:34 +02:00
parent b918e12e9f
commit 501dcb7d38
3 changed files with 538 additions and 0 deletions

View File

@@ -0,0 +1,44 @@
package episodes
import (
"context"
"log"
"mal/internal/domain"
"time"
"go.uber.org/fx"
)
const workerInterval = time.Minute
func RegisterWorker(lc fx.Lifecycle, svc domain.EpisodeService) {
ctx, cancel := context.WithCancel(context.Background())
lc.Append(fx.Hook{
OnStart: func(context.Context) error {
go func() {
log.Println("episodes: availability worker started")
ticker := time.NewTicker(workerInterval)
defer ticker.Stop()
for {
if err := svc.RefreshTrackedDue(ctx, 25); err != nil {
log.Printf("episodes: availability worker tick failed error=%v", err)
}
select {
case <-ticker.C:
case <-ctx.Done():
log.Println("episodes: availability worker stopped")
return
}
}
}()
return nil
},
OnStop: func(context.Context) error {
cancel()
return nil
},
})
}