1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
11 "git.arvados.org/arvados.git/lib/controller/dblock"
12 "git.arvados.org/arvados.git/sdk/go/auth"
13 "git.arvados.org/arvados.git/sdk/go/ctxlog"
16 func (h *Handler) periodicWorker(workerName string, interval time.Duration, locker *dblock.DBLocker, run func(context.Context) error) {
17 logger := ctxlog.FromContext(h.BackgroundContext).WithField("worker", workerName)
18 ctx := ctxlog.Context(h.BackgroundContext, logger)
20 logger.Debugf("interval is %v, not running worker", interval)
23 if !locker.Lock(ctx, h.dbConnector.GetDB) {
28 for time.Sleep(interval); ctx.Err() == nil; time.Sleep(interval) {
35 logger.WithError(err).Infof("%s failed", workerName)
40 func (h *Handler) trashSweepWorker() {
41 h.periodicWorker("trash sweep", h.Cluster.Collections.TrashSweepInterval.Duration(), dblock.TrashSweep, func(ctx context.Context) error {
42 ctx = auth.NewContext(ctx, &auth.Credentials{Tokens: []string{h.Cluster.SystemRootToken}})
43 _, err := h.federation.SysTrashSweep(ctx, struct{}{})
48 func (h *Handler) containerLogSweepWorker() {
49 h.periodicWorker("container log sweep", h.Cluster.Containers.Logging.SweepInterval.Duration(), dblock.ContainerLogSweep, func(ctx context.Context) error {
50 db, err := h.dbConnector.GetDB(ctx)
54 res, err := db.ExecContext(ctx, `
57 WHERE logs.object_uuid=containers.uuid
58 AND logs.event_type in ('stdout', 'stderr', 'arv-mount', 'crunch-run', 'crunchstat', 'hoststat', 'node', 'container', 'keepstore')
59 AND containers.log IS NOT NULL
60 AND now() - containers.finished_at > $1::interval`,
61 h.Cluster.Containers.Logging.MaxAge.String())
65 logger := ctxlog.FromContext(ctx)
66 rows, err := res.RowsAffected()
68 logger.WithError(err).Warn("unexpected error from RowsAffected()")
70 logger.WithField("rows", rows).Info("deleted rows from logs table")