diff options
author | Stan Hu <stanhu@gmail.com> | 2018-03-29 18:24:51 +0300 |
---|---|---|
committer | Jacob Vosmaer (GitLab) <jacob@gitlab.com> | 2018-03-29 18:24:51 +0300 |
commit | f256c356c558d4fc554764fded069d7e9d00b006 (patch) | |
tree | 52fcaea55bcd5a394c82345455eff6f7dbab76b9 | |
parent | 265a7ef3ff61bcb214810a3f77647972ce5cdc18 (diff) |
Break repository cleanup functions out into a separate file
-rw-r--r-- | internal/service/repository/cleanup.go | 101 | ||||
-rw-r--r-- | internal/service/repository/gc.go | 91 |
2 files changed, 103 insertions, 89 deletions
diff --git a/internal/service/repository/cleanup.go b/internal/service/repository/cleanup.go new file mode 100644 index 000000000..f0c8d1c96 --- /dev/null +++ b/internal/service/repository/cleanup.go @@ -0,0 +1,101 @@ +package repository + +import ( + "io/ioutil" + "os" + "path/filepath" + "strings" + "time" + + "google.golang.org/grpc/codes" + "google.golang.org/grpc/status" +) + +func cleanupRepo(repoPath string) error { + threshold := time.Now().Add(-1 * time.Hour) + if err := cleanRefsLocks(filepath.Join(repoPath, "refs"), threshold); err != nil { + return status.Errorf(codes.Internal, "Cleanup: cleanRefsLocks: %v", err) + } + if err := cleanPackedRefsLock(repoPath, threshold); err != nil { + return status.Errorf(codes.Internal, "Cleanup: cleanPackedRefsLock: %v", err) + } + + worktreeThreshold := time.Now().Add(-6 * time.Hour) + if err := cleanStaleWorktrees(repoPath, worktreeThreshold); err != nil { + return status.Errorf(codes.Internal, "Cleanup: cleanStaleWorktrees: %v", err) + } + + return nil +} + +func cleanRefsLocks(rootPath string, threshold time.Time) error { + return filepath.Walk(rootPath, func(path string, info os.FileInfo, err error) error { + if err != nil { + return err + } + + if info.IsDir() { + return nil + } + + if strings.HasSuffix(info.Name(), ".lock") && info.ModTime().Before(threshold) { + if err := os.Remove(path); err != nil && !os.IsNotExist(err) { + return err + } + } + + return nil + }) +} + +func cleanPackedRefsLock(repoPath string, threshold time.Time) error { + path := filepath.Join(repoPath, "packed-refs.lock") + fileInfo, err := os.Stat(path) + if err != nil { + if os.IsNotExist(err) { + return nil + } + return err + } + + if fileInfo.ModTime().Before(threshold) { + if err := os.Remove(path); err != nil && !os.IsNotExist(err) { + return err + } + } + + return nil +} + +func cleanStaleWorktrees(repoPath string, threshold time.Time) error { + worktreePath := filepath.Join(repoPath, "worktrees") + + dirInfo, err := os.Stat(worktreePath) + if err != nil { + if os.IsNotExist(err) || !dirInfo.IsDir() { + return nil + } + return err + } + + worktreeEntries, err := ioutil.ReadDir(worktreePath) + if err != nil { + return err + } + + for _, info := range worktreeEntries { + if !info.IsDir() || (info.Mode()&os.ModeSymlink != 0) { + continue + } + + path := filepath.Join(worktreePath, info.Name()) + + if info.ModTime().Before(threshold) { + if err := os.RemoveAll(path); err != nil && !os.IsNotExist(err) { + return err + } + } + } + + return nil +} diff --git a/internal/service/repository/gc.go b/internal/service/repository/gc.go index 83d4ad334..0c9ec9ec4 100644 --- a/internal/service/repository/gc.go +++ b/internal/service/repository/gc.go @@ -1,12 +1,6 @@ package repository import ( - "io/ioutil" - "os" - "path/filepath" - "strings" - "time" - "github.com/grpc-ecosystem/go-grpc-middleware/logging/logrus" log "github.com/sirupsen/logrus" "gitlab.com/gitlab-org/gitaly/internal/helper/housekeeping" @@ -30,17 +24,8 @@ func (server) GarbageCollect(ctx context.Context, in *pb.GarbageCollectRequest) return nil, err } - threshold := time.Now().Add(-1 * time.Hour) - if err := cleanRefsLocks(filepath.Join(repoPath, "refs"), threshold); err != nil { - return nil, status.Errorf(codes.Internal, "GarbageCollect: cleanRefsLocks: %v", err) - } - if err := cleanPackedRefsLock(repoPath, threshold); err != nil { - return nil, status.Errorf(codes.Internal, "GarbageCollect: cleanPackedRefsLock: %v", err) - } - - worktreeThreshold := time.Now().Add(-6 * time.Hour) - if err := cleanStaleWorktrees(repoPath, worktreeThreshold); err != nil { - return nil, status.Errorf(codes.Internal, "GarbageCollect: cleanStaleWorktrees: %v", err) + if err := cleanupRepo(repoPath); err != nil { + return nil, err } args := []string{"-c"} @@ -70,75 +55,3 @@ func (server) GarbageCollect(ctx context.Context, in *pb.GarbageCollectRequest) return &pb.GarbageCollectResponse{}, nil } - -func cleanRefsLocks(rootPath string, threshold time.Time) error { - return filepath.Walk(rootPath, func(path string, info os.FileInfo, err error) error { - if err != nil { - return err - } - - if info.IsDir() { - return nil - } - - if strings.HasSuffix(info.Name(), ".lock") && info.ModTime().Before(threshold) { - if err := os.Remove(path); err != nil && !os.IsNotExist(err) { - return err - } - } - - return nil - }) -} - -func cleanPackedRefsLock(repoPath string, threshold time.Time) error { - path := filepath.Join(repoPath, "packed-refs.lock") - fileInfo, err := os.Stat(path) - if err != nil { - if os.IsNotExist(err) { - return nil - } - return err - } - - if fileInfo.ModTime().Before(threshold) { - if err := os.Remove(path); err != nil && !os.IsNotExist(err) { - return err - } - } - - return nil -} - -func cleanStaleWorktrees(repoPath string, threshold time.Time) error { - worktreePath := filepath.Join(repoPath, "worktrees") - - dirInfo, err := os.Stat(worktreePath) - if err != nil { - if os.IsNotExist(err) || !dirInfo.IsDir() { - return nil - } - return err - } - - worktreeEntries, err := ioutil.ReadDir(worktreePath) - if err != nil { - return err - } - - for _, info := range worktreeEntries { - if !info.IsDir() || (info.Mode()&os.ModeSymlink != 0) { - continue - } - - path := filepath.Join(worktreePath, info.Name()) - - if info.ModTime().Before(threshold) { - if err := os.RemoveAll(path); err != nil && !os.IsNotExist(err) { - return err - } - } - } - - return nil -} |