feat: add episode service with background refresh worker
This commit is contained in:
44
internal/episodes/worker.go
Normal file
44
internal/episodes/worker.go
Normal 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
|
||||
},
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user