diff --git a/main.go b/main.go index a8942b371073..99ccabd09a21 100644 --- a/main.go +++ b/main.go @@ -178,6 +178,23 @@ func init() { } } +// Encloses tempdir cleanup in a function so it can be pushed +// to a goroutine +func runCleanup(ctx context.Context, execName string) { + // Every 15 minutes, attempt to remove dirs + pid := os.Getpid() + ticker := time.NewTicker(900 * time.Second) + defer ticker.Stop() + + for range ticker.C { + err := cleantemp.CleanTempDir(ctx, execName, pid) + if err != nil { + ctx.Logger().Error(err, "Error cleaning up orphaned directories ") + } + } + +} + func main() { // setup logger logFormat := log.WithConsoleSink @@ -220,19 +237,15 @@ func main() { var execName = "trufflehog" - ticker := time.NewTicker(900 * time.Second) - defer ticker.Stop() - - for { - select { - case <-ticker.C: - pid := os.Getpid() - err = cleantemp.CleanTempDir(ctx, execName, pid) - if err != nil { - ctx.Logger().Error(err, "Error cleaning up orphaned directories ") - } - } + // Inital orphaned dir cleanup when the scanner is invoked + pid := os.Getpid() + err = cleantemp.CleanTempDir(ctx, execName, pid) + if err != nil { + ctx.Logger().Error(err, "Error cleaning up orphaned directories ") } + + go runCleanup(ctx, execName) + } func run(state overseer.State) { diff --git a/pkg/cleantemp/cleantemp.go b/pkg/cleantemp/cleantemp.go index ba6b7d7d3681..e32fb7bc2565 100644 --- a/pkg/cleantemp/cleantemp.go +++ b/pkg/cleantemp/cleantemp.go @@ -26,7 +26,7 @@ func MkdirTemp() (string, error) { // Defines the interface for removing orphaned artifacts from aborted scans type CleanTemp interface { //Removes orphaned directories from sources like Git - CleanTempDir(ctx context.Context, dirName string, pid int) error + CleanTempDir(ctx logContext.Context, dirName string, pid int) error //Removes orphaned files/artifacts from sources like Artifactory CleanTempFiles(ctx context.Context, fileName string, pid int) error }