mirror of
https://github.com/openziti/zrok.git
synced 2024-11-07 08:44:14 +01:00
Merge pull request #155 from openziti-test-kitchen/stale-account-request-cleanup
Maintenance job to remove stale account requests (#135)
This commit is contained in:
commit
9491e13307
@ -1,6 +1,7 @@
|
||||
package controller
|
||||
|
||||
import (
|
||||
"time"
|
||||
"github.com/michaelquigley/cf"
|
||||
"github.com/openziti-test-kitchen/zrok/controller/store"
|
||||
"github.com/pkg/errors"
|
||||
@ -18,6 +19,7 @@ type Config struct {
|
||||
Ziti *ZitiConfig
|
||||
Metrics *MetricsConfig
|
||||
Influx *InfluxConfig
|
||||
Maintenance *MaintenanceConfig
|
||||
}
|
||||
|
||||
type AdminConfig struct {
|
||||
@ -59,9 +61,26 @@ type InfluxConfig struct {
|
||||
Token string `cf:"+secret"`
|
||||
}
|
||||
|
||||
type MaintenanceConfig struct {
|
||||
Registration *RegistrationMaintenanceConfig
|
||||
}
|
||||
|
||||
type RegistrationMaintenanceConfig struct {
|
||||
ExpirationTimeout time.Duration
|
||||
CheckFrequency time.Duration
|
||||
BatchLimit int
|
||||
}
|
||||
|
||||
func DefaultConfig() *Config {
|
||||
return &Config{
|
||||
Metrics: &MetricsConfig{ServiceName: "metrics"},
|
||||
Maintenance: &MaintenanceConfig{
|
||||
Registration: &RegistrationMaintenanceConfig{
|
||||
ExpirationTimeout: time.Hour * 24 * 30, //30 days
|
||||
CheckFrequency: time.Hour,
|
||||
BatchLimit: 500,
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1,6 +1,8 @@
|
||||
package controller
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/go-openapi/loads"
|
||||
influxdb2 "github.com/influxdata/influxdb-client-go/v2"
|
||||
"github.com/openziti-test-kitchen/zrok/controller/store"
|
||||
@ -71,6 +73,15 @@ func Run(inCfg *Config) error {
|
||||
}()
|
||||
}
|
||||
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
defer func() {
|
||||
cancel()
|
||||
}()
|
||||
|
||||
if cfg.Maintenance != nil && cfg.Maintenance.Registration != nil {
|
||||
go newMaintenanceAgent(ctx, cfg.Maintenance).run()
|
||||
}
|
||||
|
||||
server := rest_server_zrok.NewServer(api)
|
||||
defer func() { _ = server.Shutdown() }()
|
||||
server.Host = cfg.Endpoint.Host
|
||||
|
75
controller/maintenance.go
Normal file
75
controller/maintenance.go
Normal file
@ -0,0 +1,75 @@
|
||||
package controller
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
type maintenanceAgent struct {
|
||||
*MaintenanceConfig
|
||||
ctx context.Context
|
||||
}
|
||||
|
||||
func newMaintenanceAgent(ctx context.Context, cfg *MaintenanceConfig) *maintenanceAgent {
|
||||
return &maintenanceAgent{
|
||||
MaintenanceConfig: cfg,
|
||||
ctx: ctx,
|
||||
}
|
||||
}
|
||||
|
||||
func (ma *maintenanceAgent) run() {
|
||||
ticker := time.NewTicker(ma.Registration.CheckFrequency)
|
||||
for {
|
||||
select {
|
||||
case <-ma.ctx.Done():
|
||||
{
|
||||
logrus.Info("stopping maintenance loop...")
|
||||
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() }()
|
||||
|
||||
expir := time.Now().UTC().Add(-ma.Registration.ExpirationTimeout)
|
||||
accountRequests, err := str.FindExpiredAccountRequests(expir, ma.Registration.BatchLimit, tx)
|
||||
if err != nil {
|
||||
return errors.Wrapf(err, "error finding expire account requests before %v", expir)
|
||||
}
|
||||
if len(accountRequests) > 0 {
|
||||
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, ","))
|
||||
|
||||
if err := str.DeleteMultipleAccountRequests(ids, tx); err != nil {
|
||||
return errors.Wrapf(err, "error deleting expired account requests before %v", expir)
|
||||
}
|
||||
if err := tx.Commit(); err != nil {
|
||||
return errors.Wrapf(err, "error committing expired acount requests deletion")
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
@ -1,6 +1,10 @@
|
||||
package store
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/jmoiron/sqlx"
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
@ -40,6 +44,22 @@ func (self *Store) FindAccountRequestWithToken(token string, tx *sqlx.Tx) (*Acco
|
||||
return ar, nil
|
||||
}
|
||||
|
||||
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)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "error selecting expired account_requests")
|
||||
}
|
||||
var ars []*AccountRequest
|
||||
for rows.Next() {
|
||||
ar := &AccountRequest{}
|
||||
if err := rows.StructScan(ar); err != nil {
|
||||
return nil, errors.Wrap(err, "error scanning account_request")
|
||||
}
|
||||
ars = append(ars, ar)
|
||||
}
|
||||
return ars, nil
|
||||
}
|
||||
|
||||
func (self *Store) FindAccountRequestWithEmail(email string, tx *sqlx.Tx) (*AccountRequest, error) {
|
||||
ar := &AccountRequest{}
|
||||
if err := tx.QueryRowx("select * from account_requests where email = $1", email).StructScan(ar); err != nil {
|
||||
@ -59,3 +79,27 @@ func (self *Store) DeleteAccountRequest(id int, tx *sqlx.Tx) error {
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (self *Store) DeleteMultipleAccountRequests(ids []int, tx *sqlx.Tx) error {
|
||||
if len(ids) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
anyIds := make([]any, len(ids))
|
||||
indexes := make([]string, len(ids))
|
||||
|
||||
for i, id := range ids {
|
||||
anyIds[i] = id
|
||||
indexes[i] = fmt.Sprintf("$%d", i+1)
|
||||
}
|
||||
|
||||
stmt, err := tx.Prepare(fmt.Sprintf("delete from account_requests where id in (%s)", strings.Join(indexes, ",")))
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "error preparing account_requests delete multiple statement")
|
||||
}
|
||||
_, err = stmt.Exec(anyIds...)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "error executing account_requests delete multiple statement")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user