Skip to content

Commit

Permalink
enclose ticker in function for goroutine defer
Browse files Browse the repository at this point in the history
fix cleantemp interface
  • Loading branch information
Mike Vanbuskirk committed Oct 24, 2023
1 parent 845320b commit e7b6856
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 13 deletions.
37 changes: 25 additions & 12 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion pkg/cleantemp/cleantemp.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down

0 comments on commit e7b6856

Please sign in to comment.