-
Notifications
You must be signed in to change notification settings - Fork 416
fix(api): add cron job to clean up UserSession #4105
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -21,43 +21,46 @@ type RetentionController interface { | |
| } | ||
|
|
||
| type RetentionControllerImpl struct { | ||
| l *zerolog.Logger | ||
| repo v1.Repository | ||
| dv datautils.DataDecoderValidator | ||
| s gocron.Scheduler | ||
| tenantAlerter *alerting.TenantAlertManager | ||
| a *hatcheterrors.Wrapped | ||
| p *partition.Partition | ||
| dataRetention bool | ||
| workerRetention bool | ||
| queueRetention bool | ||
| l *zerolog.Logger | ||
| repo v1.Repository | ||
| dv datautils.DataDecoderValidator | ||
| s gocron.Scheduler | ||
| tenantAlerter *alerting.TenantAlertManager | ||
| a *hatcheterrors.Wrapped | ||
| p *partition.Partition | ||
| dataRetention bool | ||
| workerRetention bool | ||
| queueRetention bool | ||
| userSessionRetention bool | ||
| } | ||
|
|
||
| type RetentionControllerOpt func(*RetentionControllerOpts) | ||
|
|
||
| type RetentionControllerOpts struct { | ||
| l *zerolog.Logger | ||
| repo v1.Repository | ||
| dv datautils.DataDecoderValidator | ||
| ta *alerting.TenantAlertManager | ||
| alerter hatcheterrors.Alerter | ||
| p *partition.Partition | ||
| dataRetention bool | ||
| workerRetention bool | ||
| queueRetention bool | ||
| l *zerolog.Logger | ||
| repo v1.Repository | ||
| dv datautils.DataDecoderValidator | ||
| ta *alerting.TenantAlertManager | ||
| alerter hatcheterrors.Alerter | ||
| p *partition.Partition | ||
| dataRetention bool | ||
| workerRetention bool | ||
| queueRetention bool | ||
| userSessionRetention bool | ||
| } | ||
|
|
||
| func defaultRetentionControllerOpts() *RetentionControllerOpts { | ||
| logger := logger.NewDefaultLogger("retention-controller") | ||
| alerter := hatcheterrors.NoOpAlerter{} | ||
|
|
||
| return &RetentionControllerOpts{ | ||
| l: &logger, | ||
| dv: datautils.NewDataDecoderValidator(), | ||
| alerter: alerter, | ||
| dataRetention: true, | ||
| queueRetention: true, | ||
| workerRetention: false, | ||
| l: &logger, | ||
| dv: datautils.NewDataDecoderValidator(), | ||
| alerter: alerter, | ||
| dataRetention: true, | ||
| queueRetention: true, | ||
| workerRetention: false, | ||
| userSessionRetention: true, | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -141,44 +144,50 @@ func New(fs ...RetentionControllerOpt) (*RetentionControllerImpl, error) { | |
| a.WithData(map[string]interface{}{"service": "retention-controller"}) | ||
|
|
||
| return &RetentionControllerImpl{ | ||
| l: opts.l, | ||
| repo: opts.repo, | ||
| dv: opts.dv, | ||
| s: s, | ||
| tenantAlerter: opts.ta, | ||
| a: a, | ||
| p: opts.p, | ||
| dataRetention: opts.dataRetention, | ||
| workerRetention: opts.workerRetention, | ||
| queueRetention: opts.queueRetention, | ||
| l: opts.l, | ||
| repo: opts.repo, | ||
| dv: opts.dv, | ||
| s: s, | ||
| tenantAlerter: opts.ta, | ||
| a: a, | ||
| p: opts.p, | ||
| dataRetention: opts.dataRetention, | ||
| workerRetention: opts.workerRetention, | ||
| queueRetention: opts.queueRetention, | ||
| userSessionRetention: opts.userSessionRetention, | ||
| }, nil | ||
| } | ||
|
|
||
| func (rc *RetentionControllerImpl) Start() (func() error, error) { | ||
| rc.l.Debug().Msg("starting retention controller") | ||
|
|
||
| ctx, cancel := context.WithCancel(context.Background()) | ||
| var err error | ||
| ctx, cancel := context.WithCancelCause(context.Background()) | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. praise: Beautiful 😎 |
||
| defer func() { | ||
| if err != nil { | ||
| cancel(err) | ||
| } | ||
| }() | ||
|
|
||
| if rc.queueRetention { | ||
| queueInterval := time.Second * 60 | ||
|
|
||
| _, err := rc.s.NewJob( | ||
| _, err = rc.s.NewJob( | ||
| gocron.DurationJob(queueInterval), | ||
| gocron.NewTask( | ||
| rc.runDeleteMessageQueueItems(ctx), | ||
| ), | ||
| ) | ||
|
|
||
| if err != nil { | ||
| cancel() | ||
| return nil, fmt.Errorf("could not set up runDeleteMessageQueueItems: %w", err) | ||
| } | ||
| } | ||
|
|
||
| if rc.workerRetention { | ||
| workerInterval := 24 * time.Hour | ||
|
|
||
| _, err := rc.s.NewJob( | ||
| _, err = rc.s.NewJob( | ||
| gocron.DurationJob(workerInterval), | ||
| gocron.NewTask( | ||
| rc.runCleanupOldWorkers(ctx), | ||
|
|
@@ -187,15 +196,29 @@ func (rc *RetentionControllerImpl) Start() (func() error, error) { | |
| ) | ||
|
|
||
| if err != nil { | ||
| cancel() | ||
| return nil, fmt.Errorf("could not set up runCleanupOldWorkers: %w", err) | ||
| } | ||
| } | ||
|
|
||
| if rc.userSessionRetention { | ||
| userSessionInterval := 1 * time.Hour | ||
|
|
||
| _, err = rc.s.NewJob( | ||
| gocron.DurationJob(userSessionInterval), | ||
| gocron.NewTask( | ||
| rc.runCleanupUserSessions(ctx), | ||
| ), | ||
| ) | ||
|
|
||
| if err != nil { | ||
| return nil, fmt.Errorf("could not set up runCleanupUserSessions: %w", err) | ||
| } | ||
| } | ||
|
|
||
| rc.s.Start() | ||
|
|
||
| cleanup := func() error { | ||
| cancel() | ||
| cancel(nil) | ||
|
|
||
| if err := rc.s.Shutdown(); err != nil { | ||
| return fmt.Errorf("could not shutdown scheduler: %w", err) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| package retention | ||
|
|
||
| import ( | ||
| "context" | ||
| "time" | ||
| ) | ||
|
|
||
| func (rc *RetentionControllerImpl) runCleanupUserSessions(ctx context.Context) func() { | ||
| return func() { | ||
| rc.l.Debug().Ctx(ctx).Msg("retention controller: cleaning up user sessions") | ||
|
|
||
| ctx, cancel := context.WithTimeout(ctx, time.Second*20) | ||
| defer cancel() | ||
|
|
||
| err := rc.repo.UserSession().CleanupUserSessions(ctx) | ||
| if err != nil { | ||
| rc.l.Err(err).Ctx(ctx).Msg("user sessions cleanup failed") | ||
| } | ||
| } | ||
| } |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,9 +2,11 @@ package repository | |
|
|
||
| import ( | ||
| "context" | ||
| "errors" | ||
| "time" | ||
|
|
||
| "github.com/google/uuid" | ||
| "github.com/jackc/pgx/v5" | ||
|
|
||
| "github.com/hatchet-dev/hatchet/pkg/repository/sqlchelpers" | ||
| "github.com/hatchet-dev/hatchet/pkg/repository/sqlcv1" | ||
|
|
@@ -37,6 +39,8 @@ type UserSessionRepository interface { | |
| Update(ctx context.Context, sessionId uuid.UUID, opts *UpdateSessionOpts) (*sqlcv1.UserSession, error) | ||
| Delete(ctx context.Context, sessionId uuid.UUID) (*sqlcv1.UserSession, error) | ||
| GetById(ctx context.Context, sessionId uuid.UUID) (*sqlcv1.UserSession, error) | ||
|
|
||
| CleanupUserSessions(ctx context.Context) error | ||
| } | ||
|
|
||
| type userSessionRepository struct { | ||
|
|
@@ -165,3 +169,23 @@ func (r *userSessionRepository) GetById(ctx context.Context, sessionId uuid.UUID | |
| sessionId, | ||
| ) | ||
| } | ||
|
|
||
| func (r *userSessionRepository) CleanupUserSessions(ctx context.Context) error { | ||
| const batchSize int32 = 1000 | ||
|
|
||
| for { | ||
| result, err := r.queries.CleanupUserSessions(ctx, r.pool, batchSize) | ||
| if err != nil { | ||
| if errors.Is(err, pgx.ErrNoRows) { | ||
| return nil | ||
| } | ||
| return err | ||
| } | ||
|
Comment on lines
+178
to
+183
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If no sessions have expired (and no data is found), we're going to be returning a false positive Think we should be guarding against this with a |
||
|
|
||
| if result.RowsAffected() < int64(batchSize) { | ||
| break | ||
| } | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@mrkaye97 Can you give this a look-over? Seems fine to me but would like to make sure!