From f6e05a6fd8fa0fd7b556717d00893ba691db0495 Mon Sep 17 00:00:00 2001 From: Andre Medeiros Date: Sat, 14 Aug 2021 11:54:48 -0400 Subject: [PATCH] clean orphaned accounts --- internal/cmd/scheduler.go | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/internal/cmd/scheduler.go b/internal/cmd/scheduler.go index 5439ddc..df1d499 100644 --- a/internal/cmd/scheduler.go +++ b/internal/cmd/scheduler.go @@ -10,6 +10,7 @@ import ( "github.com/DataDog/datadog-go/statsd" "github.com/adjust/rmq/v4" "github.com/christianselig/apollo-backend/internal/cmdutil" + "github.com/christianselig/apollo-backend/internal/repository" "github.com/go-co-op/gocron" "github.com/go-redis/redis/v8" "github.com/jackc/pgx/v4" @@ -70,7 +71,7 @@ func SchedulerCmd(ctx context.Context) *cobra.Command { s.Every(200).Milliseconds().SingletonMode().Do(func() { enqueueAccounts(ctx, logger, statsd, db, redis, luaSha, notifQueue) }) s.Every(1).Second().Do(func() { cleanQueues(ctx, logger, queue) }) s.Every(1).Minute().Do(func() { reportStats(ctx, logger, statsd, db, redis) }) - s.Every(1).Minute().Do(func() { cleanAccounts(ctx, logger, db) }) + s.Every(1).Minute().Do(func() { pruneStale(ctx, logger, db) }) s.StartAsync() <-ctx.Done() @@ -103,12 +104,21 @@ func evalScript(ctx context.Context, redis *redis.Client) (string, error) { return redis.ScriptLoad(ctx, lua).Result() } -func cleanAccounts(ctx context.Context, logger *logrus.Logger, pool *pgxpool.Pool) { +func pruneStale(ctx context.Context, logger *logrus.Logger, pool *pgxpool.Pool) { + ar := repository.NewPostgresAccount(pool) + count, err := ar.PruneStale(ctx) + + if err != nil { + logger.WithFields(logrus.Fields{ + "err": err, + }).Error("failed cleaning stale accounts") + return + } + now := time.Now().Unix() - 7200 - count := 0 ids := []int64{} - err := pool.BeginFunc(ctx, func(tx pgx.Tx) error { + err = pool.BeginFunc(ctx, func(tx pgx.Tx) error { stmt := ` WITH account AS ( SELECT id