Skip to content

Commit

Permalink
feat: add cleanup for devenv files
Browse files Browse the repository at this point in the history
  • Loading branch information
bchmnn committed Jun 25, 2024
1 parent b684965 commit 66bc2f0
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 6 deletions.
2 changes: 1 addition & 1 deletion service/backend/server/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func NewRouter(docker *service.DockerService, devenvFilesPath string, devenvFile
devenvController := controller.NewDevenvController(docker, devenvFilesPath, devenvFilesTmpPath)
replController := controller.NewReplController(docker, &replState)

cleanup := service.Cleanup(docker, &replState)
cleanup := service.Cleanup(docker, &replState, devenvFilesPath, devenvFilesTmpPath)
cleanup.DoCleanup()
cleanup.StartTask()

Expand Down
21 changes: 16 additions & 5 deletions service/backend/service/cleanup.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,18 @@ import (
)

type CleanupService struct {
Docker *DockerService
ReplState *ReplStateService
Docker *DockerService
ReplState *ReplStateService
DevenvFilesPath string
DevenvFilesTmpPath string
}

func Cleanup(docker *DockerService, replState *ReplStateService) CleanupService {
func Cleanup(docker *DockerService, replState *ReplStateService, devenvFilesPath string, devenvFilesTmpPath string) CleanupService {
return CleanupService{
Docker: docker,
ReplState: replState,
Docker: docker,
ReplState: replState,
DevenvFilesPath: devenvFilesPath,
DevenvFilesTmpPath: devenvFilesTmpPath,
}
}

Expand Down Expand Up @@ -48,6 +52,13 @@ func (cleanup *CleanupService) DoCleanup() {
database.DB.Unscoped().Where("created_at < ?", cutoffTime).Delete(&model.Devenv{})
database.DB.Unscoped().Where("created_at < ?", cutoffTime).Delete(&model.User{})
util.SLogger.Debugf("Cleaning database [%v]", time.Since(start))

util.SLogger.Debug("Cleaning devenvs starting ..")
start = time.Now()
util.DeleteDirsOlderThan(cleanup.DevenvFilesPath, cutoffTime)
util.DeleteDirsOlderThan(cleanup.DevenvFilesTmpPath, cutoffTime)
util.SLogger.Debugf("Cleaning devenvs [%v]", time.Since(start))

}

func (cleanup *CleanupService) StartTask() *chan struct{} {
Expand Down
39 changes: 39 additions & 0 deletions service/backend/util/files.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"io/fs"
"os"
"path/filepath"
"time"

cp "github.com/otiai10/copy"
)
Expand Down Expand Up @@ -84,3 +85,41 @@ func CopyRecurse(src string, target string, perm fs.FileMode) error {
})
}

func GetFileModTime(path string) (time.Time, error) {
info, err := os.Stat(path)
if err != nil {
return time.Time{}, err
}

return info.ModTime(), nil
}

func DeleteDirsOlderThan(path string, t time.Time) {
dir, err := os.Open(path)
if err != nil {
return
}
defer dir.Close()

files, err := dir.Readdir(0)
if err != nil {
return
}

for _, file := range files {
if file.IsDir() {
child := filepath.Join(path, file.Name())
modTime, err := GetFileModTime(child)
if err != nil {
continue
}
if modTime.Before(t) {
SLogger.Debugf("Deleting directory %s", child)
err = DeleteDir(child)
if err != nil {
continue
}
}
}
}
}

0 comments on commit 66bc2f0

Please sign in to comment.