added explicit get and delete for expired account requests

This commit is contained in:
Cam Otts 2023-01-12 13:00:16 -06:00
parent deb6831fee
commit 0db3f0c9e6
No known key found for this signature in database
GPG Key ID: 367B7C7EBD84A8BD
2 changed files with 56 additions and 11 deletions

View File

@ -2,6 +2,8 @@ package controller
import ( import (
"context" "context"
"fmt"
"strings"
"time" "time"
"github.com/pkg/errors" "github.com/pkg/errors"
@ -28,7 +30,7 @@ func (ma *maintenanceAgent) run() {
select { select {
case <-ma.ctx.Done(): case <-ma.ctx.Done():
{ {
logrus.Info("Stopping maintenance loop...") logrus.Info("stopping maintenance loop...")
ticker.Stop() ticker.Stop()
return return
} }
@ -49,8 +51,23 @@ func (ma *maintenanceAgent) deleteExpiredAccountRequests() error {
} }
defer func() { _ = tx.Rollback() }() defer func() { _ = tx.Rollback() }()
if err := str.DeleteExpiredAccountRequests(time.Now().UTC().Add(-ma.expiration), tx); err != nil { expir := time.Now().UTC().Add(-ma.expiration)
return errors.Wrapf(err, "error deleting expired account requests") accountRequests, err := str.FindExpiredAccountRequests(expir, 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 { if err := tx.Commit(); err != nil {

View File

@ -1,11 +1,12 @@
package store package store
import ( import (
"fmt"
"strings"
"time" "time"
"github.com/jmoiron/sqlx" "github.com/jmoiron/sqlx"
"github.com/pkg/errors" "github.com/pkg/errors"
"github.com/sirupsen/logrus"
) )
type AccountRequest struct { type AccountRequest struct {
@ -43,6 +44,22 @@ func (self *Store) FindAccountRequestWithToken(token string, tx *sqlx.Tx) (*Acco
return ar, nil return ar, nil
} }
func (self *Store) FindExpiredAccountRequests(before time.Time, tx *sqlx.Tx) ([]*AccountRequest, error) {
rows, err := tx.Queryx("select * from account_requests where created_at < $1", 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) { func (self *Store) FindAccountRequestWithEmail(email string, tx *sqlx.Tx) (*AccountRequest, error) {
ar := &AccountRequest{} ar := &AccountRequest{}
if err := tx.QueryRowx("select * from account_requests where email = $1", email).StructScan(ar); err != nil { if err := tx.QueryRowx("select * from account_requests where email = $1", email).StructScan(ar); err != nil {
@ -63,15 +80,26 @@ func (self *Store) DeleteAccountRequest(id int, tx *sqlx.Tx) error {
return nil return nil
} }
func (self *Store) DeleteExpiredAccountRequests(before time.Time, tx *sqlx.Tx) error { func (self *Store) DeleteMultipleAccountRequests(ids []int, tx *sqlx.Tx) error {
stmt, err := tx.Prepare("delete from account_requests where created_at < $1") if len(ids) == 0 {
logrus.Infof("Trying to delete account requests older than %v", before) return nil
if err != nil {
return errors.Wrap(err, "error preparing account_requests delete expired statement")
} }
_, err = stmt.Exec(before)
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 { if err != nil {
return errors.Wrap(err, "error executing account_requests delete expired statement") 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 return nil
} }