stillbox/pkg/share/service.go

50 lines
628 B
Go
Raw Normal View History

2025-01-12 19:40:36 -05:00
package share
import (
"context"
"time"
"github.com/rs/zerolog/log"
)
const (
PruneInterval = time.Hour * 4
)
type Service interface {
2025-01-17 20:53:51 -05:00
ShareStore() Store
2025-01-12 19:40:36 -05:00
Go(ctx context.Context)
}
type service struct {
2025-01-17 20:53:51 -05:00
Store
2025-01-12 19:40:36 -05:00
}
2025-01-17 20:53:51 -05:00
func (s *service) ShareStore() Store {
return s.Store
2025-01-12 19:40:36 -05:00
}
func (s *service) Go(ctx context.Context) {
tick := time.NewTicker(PruneInterval)
for {
select {
2025-01-15 23:11:42 -05:00
case <-tick.C:
2025-01-17 20:53:51 -05:00
err := s.Prune(ctx)
2025-01-12 19:40:36 -05:00
if err != nil {
log.Error().Err(err).Msg("share prune failed")
}
case <-ctx.Done():
tick.Stop()
return
}
}
}
func NewService() *service {
return &service{
2025-01-17 20:53:51 -05:00
Store: NewStore(),
2025-01-12 19:40:36 -05:00
}
}