Skip to content

Commit

Permalink
feat(cleanupmanager): add periodic job for API key cleanup and remove…
Browse files Browse the repository at this point in the history
… unused update logic
  • Loading branch information
mastercactapus committed Dec 18, 2024
1 parent acc14da commit 77a229c
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 72 deletions.
17 changes: 1 addition & 16 deletions engine/cleanupmanager/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,13 @@ import (

"github.com/target/goalert/alert"
"github.com/target/goalert/engine/processinglock"
"github.com/target/goalert/util"
)

// DB handles updating escalation policies.
type DB struct {
db *sql.DB
lock *processinglock.Lock

cleanupAPIKeys *sql.Stmt
setTimeout *sql.Stmt

cleanupSessions *sql.Stmt

alertStore *alert.Store

logger *slog.Logger
Expand All @@ -38,20 +32,11 @@ func NewDB(ctx context.Context, db *sql.DB, alertstore *alert.Store, log *slog.L
return nil, err
}

p := &util.Prepare{Ctx: ctx, DB: db}

return &DB{
db: db,
lock: lock,
logger: log,

// Abort any cleanup operation that takes longer than 3 seconds
// error will be logged.
setTimeout: p.P(`SET LOCAL statement_timeout = 3000`),
cleanupAPIKeys: p.P(`update user_calendar_subscriptions set disabled = true where id = any(select id from user_calendar_subscriptions where greatest(last_access, last_update) < (now() - $1::interval) order by id limit 100 for update skip locked)`),

cleanupSessions: p.P(`DELETE FROM auth_user_sessions WHERE id = any(select id from auth_user_sessions where last_access_at < (now() - '30 days'::interval) LIMIT 100 for update skip locked)`),

alertStore: alertstore,
}, p.Err
}, nil
}
15 changes: 15 additions & 0 deletions engine/cleanupmanager/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const QueueName = "cleanup-manager"
const (
PriorityAlertCleanup = 1
PrioritySchedHistory = 1
PriorityAPICleanup = 1
PriorityTempSchedLFW = 2
PriorityAlertLogsLFW = 2
PriorityTempSched = 3
Expand Down Expand Up @@ -57,6 +58,7 @@ func (db *DB) Setup(ctx context.Context, args processinglock.SetupArgs) error {
river.AddWorker(args.Workers, river.WorkFunc(db.LookForWorkScheduleData))
river.AddWorker(args.Workers, river.WorkFunc(db.CleanupAlertLogs))
river.AddWorker(args.Workers, river.WorkFunc(db.LookForWorkAlertLogs))
river.AddWorker(args.Workers, river.WorkFunc(db.CleanupAPIKeys))

err := args.River.Queues().Add(QueueName, river.QueueConfig{MaxWorkers: 5})
if err != nil {
Expand Down Expand Up @@ -117,5 +119,18 @@ func (db *DB) Setup(ctx context.Context, args processinglock.SetupArgs) error {
),
})

args.River.PeriodicJobs().AddMany([]*river.PeriodicJob{
river.NewPeriodicJob(
river.PeriodicInterval(24*time.Hour),
func() (river.JobArgs, *river.InsertOpts) {
return APIKeysArgs{}, &river.InsertOpts{
Queue: QueueName,
Priority: PriorityAPICleanup,
}
},
&river.PeriodicJobOpts{RunOnStart: true},
),
})

return nil
}
56 changes: 0 additions & 56 deletions engine/cleanupmanager/update.go

This file was deleted.

0 comments on commit 77a229c

Please sign in to comment.