Merge branch '18339-sweep-trash-lock'
[arvados.git] / lib / controller / trash.go
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 package controller
6
7 import (
8         "time"
9
10         "git.arvados.org/arvados.git/lib/controller/dblock"
11         "git.arvados.org/arvados.git/sdk/go/auth"
12         "git.arvados.org/arvados.git/sdk/go/ctxlog"
13 )
14
15 func (h *Handler) trashSweepWorker() {
16         sleep := h.Cluster.Collections.TrashSweepInterval.Duration()
17         logger := ctxlog.FromContext(h.BackgroundContext).WithField("worker", "trash sweep")
18         ctx := ctxlog.Context(h.BackgroundContext, logger)
19         if sleep <= 0 {
20                 logger.Debugf("Collections.TrashSweepInterval is %v, not running worker", sleep)
21                 return
22         }
23         dblock.TrashSweep.Lock(ctx, h.db)
24         defer dblock.TrashSweep.Unlock()
25         for time.Sleep(sleep); ctx.Err() == nil; time.Sleep(sleep) {
26                 dblock.TrashSweep.Check()
27                 ctx := auth.NewContext(ctx, &auth.Credentials{Tokens: []string{h.Cluster.SystemRootToken}})
28                 _, err := h.federation.SysTrashSweep(ctx, struct{}{})
29                 if err != nil {
30                         logger.WithError(err).Info("trash sweep failed")
31                 }
32         }
33 }