From 2da67d4a29b0fc206c5a4d462c9eb18641a21064 Mon Sep 17 00:00:00 2001 From: Michael Quigley Date: Thu, 12 Jan 2023 16:00:09 -0500 Subject: [PATCH] account expiration tweaks (#135) --- controller/config.go | 4 ++-- controller/maintenance.go | 15 +++++++++------ controller/store/account_request.go | 14 +++++++++++++- 3 files changed, 24 insertions(+), 9 deletions(-) diff --git a/controller/config.go b/controller/config.go index 3fc3d62f..a879c28f 100644 --- a/controller/config.go +++ b/controller/config.go @@ -1,10 +1,10 @@ package controller import ( - "time" "github.com/michaelquigley/cf" "github.com/openziti-test-kitchen/zrok/controller/store" "github.com/pkg/errors" + "time" ) const ConfigVersion = 1 @@ -76,7 +76,7 @@ func DefaultConfig() *Config { Metrics: &MetricsConfig{ServiceName: "metrics"}, Maintenance: &MaintenanceConfig{ Registration: &RegistrationMaintenanceConfig{ - ExpirationTimeout: time.Hour * 24 * 30, //30 days + ExpirationTimeout: time.Hour * 24, CheckFrequency: time.Hour, BatchLimit: 500, }, diff --git a/controller/maintenance.go b/controller/maintenance.go index c9046560..d81233fb 100644 --- a/controller/maintenance.go +++ b/controller/maintenance.go @@ -23,12 +23,14 @@ func newMaintenanceAgent(ctx context.Context, cfg *MaintenanceConfig) *maintenan } func (ma *maintenanceAgent) run() { + logrus.Info("starting") + defer logrus.Info("stopping") + ticker := time.NewTicker(ma.Registration.CheckFrequency) for { select { case <-ma.ctx.Done(): { - logrus.Info("stopping maintenance loop...") ticker.Stop() return } @@ -49,22 +51,23 @@ func (ma *maintenanceAgent) deleteExpiredAccountRequests() error { } defer func() { _ = tx.Rollback() }() - expir := time.Now().UTC().Add(-ma.Registration.ExpirationTimeout) - accountRequests, err := str.FindExpiredAccountRequests(expir, ma.Registration.BatchLimit, tx) + timeout := time.Now().UTC().Add(-ma.Registration.ExpirationTimeout) + accountRequests, err := str.FindExpiredAccountRequests(timeout, ma.Registration.BatchLimit, tx) if err != nil { - return errors.Wrapf(err, "error finding expire account requests before %v", expir) + return errors.Wrapf(err, "error finding expire account requests before %v", timeout) } if len(accountRequests) > 0 { + logrus.Infof("found %d expired account requests to remove", len(accountRequests)) acctStrings := make([]string, len(accountRequests)) ids := make([]int, len(accountRequests)) for i, acct := range accountRequests { ids[i] = acct.Id acctStrings[i] = fmt.Sprintf("{%d:%s}", acct.Id, acct.Email) } - logrus.Infof("starting deleting for expired account requests: %v", strings.Join(acctStrings, ",")) + logrus.Infof("deleting expired account requests: %v", strings.Join(acctStrings, ",")) if err := str.DeleteMultipleAccountRequests(ids, tx); err != nil { - return errors.Wrapf(err, "error deleting expired account requests before %v", expir) + return errors.Wrapf(err, "error deleting expired account requests before %v", timeout) } if err := tx.Commit(); err != nil { return errors.Wrapf(err, "error committing expired acount requests deletion") diff --git a/controller/store/account_request.go b/controller/store/account_request.go index a455e9ad..7811569d 100644 --- a/controller/store/account_request.go +++ b/controller/store/account_request.go @@ -45,7 +45,19 @@ func (self *Store) FindAccountRequestWithToken(token string, tx *sqlx.Tx) (*Acco } func (self *Store) FindExpiredAccountRequests(before time.Time, limit int, tx *sqlx.Tx) ([]*AccountRequest, error) { - rows, err := tx.Queryx(fmt.Sprintf("select * from account_requests where created_at < $1 limit %d for update", limit), before) + var sql string + switch self.cfg.Type { + case "postgres": + sql = "select * from account_requests where created_at < $1 limit %d for update" + + case "sqlite3": + sql = "select * from account_requests where created_at < $1 limit %d" + + default: + return nil, errors.Errorf("unknown database type '%v'", self.cfg.Type) + } + + rows, err := tx.Queryx(fmt.Sprintf(sql, limit), before) if err != nil { return nil, errors.Wrap(err, "error selecting expired account_requests") }