2023-01-12 17:04:56 +01:00
|
|
|
package controller
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2023-01-12 20:00:16 +01:00
|
|
|
"fmt"
|
|
|
|
"strings"
|
2023-01-12 17:04:56 +01:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
|
|
|
type maintenanceAgent struct {
|
2023-01-12 21:09:04 +01:00
|
|
|
*MaintenanceConfig
|
|
|
|
ctx context.Context
|
2023-01-12 17:04:56 +01:00
|
|
|
}
|
|
|
|
|
2023-01-12 21:09:04 +01:00
|
|
|
func newMaintenanceAgent(ctx context.Context, cfg *MaintenanceConfig) *maintenanceAgent {
|
2023-01-12 17:04:56 +01:00
|
|
|
return &maintenanceAgent{
|
2023-01-12 21:09:04 +01:00
|
|
|
MaintenanceConfig: cfg,
|
|
|
|
ctx: ctx,
|
2023-01-12 17:04:56 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ma *maintenanceAgent) run() {
|
2023-01-12 22:00:09 +01:00
|
|
|
logrus.Info("starting")
|
|
|
|
defer logrus.Info("stopping")
|
|
|
|
|
2023-01-12 21:09:04 +01:00
|
|
|
ticker := time.NewTicker(ma.Registration.CheckFrequency)
|
2023-01-12 17:04:56 +01:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ma.ctx.Done():
|
|
|
|
{
|
|
|
|
ticker.Stop()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
case <-ticker.C:
|
|
|
|
{
|
|
|
|
if err := ma.deleteExpiredAccountRequests(); err != nil {
|
|
|
|
logrus.Error(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ma *maintenanceAgent) deleteExpiredAccountRequests() error {
|
|
|
|
tx, err := str.Begin()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer func() { _ = tx.Rollback() }()
|
|
|
|
|
2023-01-12 22:00:09 +01:00
|
|
|
timeout := time.Now().UTC().Add(-ma.Registration.ExpirationTimeout)
|
|
|
|
accountRequests, err := str.FindExpiredAccountRequests(timeout, ma.Registration.BatchLimit, tx)
|
2023-01-12 20:00:16 +01:00
|
|
|
if err != nil {
|
2023-01-12 22:00:09 +01:00
|
|
|
return errors.Wrapf(err, "error finding expire account requests before %v", timeout)
|
2023-01-12 20:00:16 +01:00
|
|
|
}
|
|
|
|
if len(accountRequests) > 0 {
|
2023-01-12 22:00:09 +01:00
|
|
|
logrus.Infof("found %d expired account requests to remove", len(accountRequests))
|
2023-01-12 20:00:16 +01:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2023-01-12 22:00:09 +01:00
|
|
|
logrus.Infof("deleting expired account requests: %v", strings.Join(acctStrings, ","))
|
2023-01-12 20:00:16 +01:00
|
|
|
if err := str.DeleteMultipleAccountRequests(ids, tx); err != nil {
|
2023-01-12 22:00:09 +01:00
|
|
|
return errors.Wrapf(err, "error deleting expired account requests before %v", timeout)
|
2023-01-12 20:00:16 +01:00
|
|
|
}
|
2023-01-12 21:09:04 +01:00
|
|
|
if err := tx.Commit(); err != nil {
|
|
|
|
return errors.Wrapf(err, "error committing expired acount requests deletion")
|
|
|
|
}
|
2023-01-12 17:04:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|