mirror of
https://github.com/Luzifer/twitch-bot.git
synced 2024-11-08 16:20:02 +00:00
[core] Add cleanup for expired timers
Signed-off-by: Knut Ahlers <knut@ahlers.me>
This commit is contained in:
parent
fdf3d95322
commit
c311370d1c
4 changed files with 25 additions and 10 deletions
|
@ -7,6 +7,8 @@ import (
|
|||
"time"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
"github.com/robfig/cron/v3"
|
||||
"github.com/sirupsen/logrus"
|
||||
"gorm.io/gorm"
|
||||
"gorm.io/gorm/clause"
|
||||
|
||||
|
@ -28,11 +30,17 @@ type (
|
|||
|
||||
var _ plugins.TimerStore = (*Service)(nil)
|
||||
|
||||
func New(db database.Connector) (*Service, error) {
|
||||
func New(db database.Connector, cronService *cron.Cron) (*Service, error) {
|
||||
s := &Service{
|
||||
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")
|
||||
}
|
||||
|
||||
|
@ -101,3 +109,9 @@ func (s Service) SetTimer(id string, expiry time.Time) error {
|
|||
"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")
|
||||
}
|
||||
}
|
||||
|
|
|
@ -12,7 +12,7 @@ import (
|
|||
|
||||
func TestTimerRoundtrip(t *testing.T) {
|
||||
dbc := database.GetTestDatabase(t)
|
||||
ts, err := New(dbc)
|
||||
ts, err := New(dbc, nil)
|
||||
require.NoError(t, err, "creating timer service")
|
||||
|
||||
id := "78c0176a-938e-497b-bed4-83d5bdec6caf"
|
||||
|
|
|
@ -59,7 +59,7 @@ func (s storageFile) migratePermissions(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 {
|
||||
return errors.Wrap(err, "creating timer service")
|
||||
}
|
||||
|
|
15
main.go
15
main.go
|
@ -124,18 +124,19 @@ func main() {
|
|||
}
|
||||
|
||||
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 {
|
||||
log.WithError(err).Fatal("Unable to apply access migration")
|
||||
}
|
||||
|
||||
if timerService, err = timer.New(db); err != nil {
|
||||
log.WithError(err).Fatal("Unable to apply timer migration")
|
||||
log.WithError(err).Fatal("applying access migration")
|
||||
}
|
||||
|
||||
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{
|
||||
TwitchClient: cfg.TwitchClient,
|
||||
TwitchClientSecret: cfg.TwitchClientSecret,
|
||||
|
@ -146,7 +147,7 @@ func main() {
|
|||
},
|
||||
}); err != nil {
|
||||
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, "")
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue