diff --git a/cmd/gitaly/main.go b/cmd/gitaly/main.go index f3f97413b907ba494b6f6e30bfb2298c1356e465..8cebe20929381d76b8242966344f07010f088df1 100644 --- a/cmd/gitaly/main.go +++ b/cmd/gitaly/main.go @@ -10,6 +10,7 @@ import ( "github.com/prometheus/client_golang/prometheus/promhttp" log "github.com/sirupsen/logrus" "gitlab.com/gitlab-org/gitaly/internal/bootstrap" + "gitlab.com/gitlab-org/gitaly/internal/cache" "gitlab.com/gitlab-org/gitaly/internal/config" "gitlab.com/gitlab-org/gitaly/internal/git" "gitlab.com/gitlab-org/gitaly/internal/server" @@ -102,6 +103,7 @@ func main() { tracing.Initialize(tracing.WithServiceName("gitaly")) tempdir.StartCleaning() + cache.StartCleaning() log.WithError(run(b)).Error("shutting down") } diff --git a/internal/cache/walker.go b/internal/cache/walker.go index 6e0ba685a7f14019c0e26f7551736d7609bed768..c9f3f13c005563f601c09318fb2af481f0c4656f 100644 --- a/internal/cache/walker.go +++ b/internal/cache/walker.go @@ -136,14 +136,21 @@ func moveAndClear(storage config.Storage) error { return nil } +// StartCleaning starts goroutines that will clean up cache directories. +// These goroutines will query config.Config, so you probably don't want +// to spawn them when testing. +func StartCleaning() { + for _, storage := range config.Config.Storages { + startCleanWalker(storage) + } +} + func init() { config.RegisterHook(func() error { for _, storage := range config.Config.Storages { if err := moveAndClear(storage); err != nil { return err } - - startCleanWalker(storage) } return nil }) diff --git a/internal/cache/walker_test.go b/internal/cache/walker_test.go index b3d3044d602b9c09c1e695f6472b0814b6e73533..fa11c4a360efe02cc6ff1d32d0843cf97a6c0a9e 100644 --- a/internal/cache/walker_test.go +++ b/internal/cache/walker_test.go @@ -58,7 +58,7 @@ func TestDiskCacheObjectWalker(t *testing.T) { *cache.ExportDisableMoveAndClear = true defer func() { *cache.ExportDisableMoveAndClear = false }() - require.NoError(t, config.Validate()) // triggers walker + cache.StartCleaning() pollCountersUntil(t, expectChecks, expectRemovals) diff --git a/internal/config/config.go b/internal/config/config.go index fb711adda2c5c0b4570841a1edc146e0c7780313..336a2510d9333f74dbd944cb88cb8f506bb15812 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -122,12 +122,15 @@ func Load(file io.Reader) error { return nil } -// RegisterHook adds a post-validation callback. +// RegisterHook adds a post-validation callback. Be careful with config +// data race conditions if you spawn goroutines in a hook: tests may +// modify configuration data _after_ your hook has run. func RegisterHook(f func() error) { hooks = append(hooks, f) } -// Validate checks the current Config for sanity. +// Validate checks the current Config for sanity. It will also run all +// hooks that have been registered with RegisterHook. func Validate() error { for _, err := range []error{ validateListeners(),