Skip to content

feat: basic self cleanup addition #30

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions build/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func environ() (env []string) {
}

func (bin *Binary) WriteBuild(writer io.Writer) error {
dir, err := tempDirectory()
dir, err := GetTempDirectory()
if err != nil && !os.IsNotExist(err) {
return fmt.Errorf("creating temporary directory: %w", err)
}
Expand Down Expand Up @@ -274,7 +274,7 @@ func tempFilename() (string, error) {
return f, nil
}

func tempDirectory() (string, error) {
func GetTempDirectory() (string, error) {
dir, err := os.MkdirTemp(os.TempDir(), "goblin")
if err != nil {
return "", err
Expand Down
49 changes: 49 additions & 0 deletions cmd/goblin-api/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"log"
"net/http"
"os"
"os/exec"
"path/filepath"
"strings"
"time"
Expand Down Expand Up @@ -135,6 +136,7 @@ func main() {
}
}

clearTempCaches()
clearStorageBackgroundJob()
StartServer(portFlag)
}
Expand Down Expand Up @@ -400,3 +402,50 @@ func constructArtifactName(bin *build.Binary) string {
artifactName.Write([]byte(bin.Arch))
return artifactName.String()
}

func CleanupGoCache() {
log.Println("Cleaning up go caches")
cmd := exec.Command("go", "clean", "-cache")
err := cmd.Run()
if err != nil {
log.Printf("Failed to run go clean -cache with error: %v", err)
}

cmd = exec.Command("go", "clean", "-modcache")
err = cmd.Run()
if err != nil {
log.Printf("Failed to run go clean -modcache with error: %v", err)
}
}

func CleanupFailedBuildCaches() {
log.Println("Cleaning up fail build directories")
base := os.TempDir()
entries, err := os.ReadDir(base)
if err != nil {
log.Println("failed to read temp directory")
return
}
for _, ent := range entries {
if ent.IsDir() {
if strings.HasPrefix(ent.Name(), "goblin") {
err := os.RemoveAll(ent.Name())
if err != nil {
log.Printf("failed to remove dir: %v", err)
}
}
}
}
}

func clearTempCaches() {
tickerDur, _ := time.ParseDuration("30s")
ticker := time.NewTicker(tickerDur)

go func() {
for range ticker.C {
CleanupFailedBuildCaches()
CleanupGoCache()
}
}()
}