[core] Add cleanup for expired timers

Signed-off-by: Knut Ahlers <knut@ahlers.me>
This commit is contained in:
Knut Ahlers 2023-06-24 14:50:45 +02:00
parent fdf3d95322
commit c311370d1c
Signed by: luzifer
GPG key ID: D91C3E91E4CAD6F5
4 changed files with 25 additions and 10 deletions

View file

@ -7,6 +7,8 @@ import (
"time" "time"
"github.com/pkg/errors" "github.com/pkg/errors"
"github.com/robfig/cron/v3"
"github.com/sirupsen/logrus"
"gorm.io/gorm" "gorm.io/gorm"
"gorm.io/gorm/clause" "gorm.io/gorm/clause"
@ -28,11 +30,17 @@ type (
var _ plugins.TimerStore = (*Service)(nil) var _ plugins.TimerStore = (*Service)(nil)
func New(db database.Connector) (*Service, error) { func New(db database.Connector, cronService *cron.Cron) (*Service, error) {
s := &Service{ s := &Service{
db: db, db: db,
} }
if cronService != nil {
if _, err := cronService.AddFunc("@every 5m", s.cleanupTimers); err != nil {
return nil, errors.Wrap(err, "registering timer cleanup cron")
}
}
return s, errors.Wrap(s.db.DB().AutoMigrate(&timer{}), "applying migrations") return s, errors.Wrap(s.db.DB().AutoMigrate(&timer{}), "applying migrations")
} }
@ -101,3 +109,9 @@ func (s Service) SetTimer(id string, expiry time.Time) error {
"storing counter in database", "storing counter in database",
) )
} }
func (s Service) cleanupTimers() {
if err := s.db.DB().Delete(&timer{}, "expires_at < ?", time.Now().UTC()).Error; err != nil {
logrus.WithError(err).Error("cleaning up expired timers")
}
}

View file

@ -12,7 +12,7 @@ import (
func TestTimerRoundtrip(t *testing.T) { func TestTimerRoundtrip(t *testing.T) {
dbc := database.GetTestDatabase(t) dbc := database.GetTestDatabase(t)
ts, err := New(dbc) ts, err := New(dbc, nil)
require.NoError(t, err, "creating timer service") require.NoError(t, err, "creating timer service")
id := "78c0176a-938e-497b-bed4-83d5bdec6caf" id := "78c0176a-938e-497b-bed4-83d5bdec6caf"

View file

@ -59,7 +59,7 @@ func (s storageFile) migratePermissions(db database.Connector) (err error) {
} }
func (s storageFile) migrateTimers(db database.Connector) (err error) { func (s storageFile) migrateTimers(db database.Connector) (err error) {
ts, err := timer.New(db) ts, err := timer.New(db, nil)
if err != nil { if err != nil {
return errors.Wrap(err, "creating timer service") return errors.Wrap(err, "creating timer service")
} }

15
main.go
View file

@ -124,18 +124,19 @@ func main() {
} }
if db, err = database.New(cfg.StorageConnType, cfg.StorageConnString, cfg.StorageEncryptionPass); err != nil { if db, err = database.New(cfg.StorageConnType, cfg.StorageConnString, cfg.StorageEncryptionPass); err != nil {
log.WithError(err).Fatal("Unable to open storage backend") log.WithError(err).Fatal("opening storage backend")
} }
if accessService, err = access.New(db); err != nil { if accessService, err = access.New(db); err != nil {
log.WithError(err).Fatal("Unable to apply access migration") log.WithError(err).Fatal("applying access migration")
}
if timerService, err = timer.New(db); err != nil {
log.WithError(err).Fatal("Unable to apply timer migration")
} }
cronService = cron.New(cron.WithSeconds()) cronService = cron.New(cron.WithSeconds())
if timerService, err = timer.New(db, cronService); err != nil {
log.WithError(err).Fatal("applying timer migration")
}
if twitchClient, err = accessService.GetBotTwitchClient(access.ClientConfig{ if twitchClient, err = accessService.GetBotTwitchClient(access.ClientConfig{
TwitchClient: cfg.TwitchClient, TwitchClient: cfg.TwitchClient,
TwitchClientSecret: cfg.TwitchClientSecret, TwitchClientSecret: cfg.TwitchClientSecret,
@ -146,7 +147,7 @@ func main() {
}, },
}); err != nil { }); err != nil {
if !errors.Is(err, access.ErrChannelNotAuthorized) { if !errors.Is(err, access.ErrChannelNotAuthorized) {
log.WithError(err).Fatal("Unable to initialize Twitch client") log.WithError(err).Fatal("initializing Twitch client")
} }
twitchClient = twitch.New(cfg.TwitchClient, cfg.TwitchClientSecret, cfg.TwitchToken, "") twitchClient = twitch.New(cfg.TwitchClient, cfg.TwitchClientSecret, cfg.TwitchToken, "")
} }