-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
Implement archive cleanup #885
Changes from 2 commits
42ae0e4
5a85f6e
3a5a732
e274e78
0dfe6fd
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1837,6 +1837,59 @@ func DeleteRepositoryArchives() error { | |
}) | ||
} | ||
|
||
// DeleteOldRepositoryArchives deletes old repository archives. | ||
func DeleteOldRepositoryArchives() { | ||
if taskStatusTable.IsRunning(archiveCleanup) { | ||
return | ||
} | ||
taskStatusTable.Start(archiveCleanup) | ||
defer taskStatusTable.Stop(archiveCleanup) | ||
|
||
log.Trace("Doing: ArchiveCleanup") | ||
|
||
if err := x. | ||
Where("id > 0"). | ||
Iterate(new(Repository), | ||
func(idx int, bean interface{}) error { | ||
repo := bean.(*Repository) | ||
basePath := filepath.Join(repo.RepoPath(), "archives") | ||
|
||
for _, ty := range []string{"zip", "targz"} { | ||
path := filepath.Join(basePath, ty) | ||
file, err := os.Open(path) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. file is not close There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed. |
||
if err != nil { | ||
if !os.IsNotExist(err) { | ||
log.Warn("Unable to open directory %s: %v", path, err) | ||
return err | ||
} | ||
|
||
// If the directory doesn't exist, that's okay. | ||
continue | ||
} | ||
|
||
files, err := file.Readdir(0) | ||
file.Close() | ||
if err != nil { | ||
log.Warn("Unable to read directory %s: %v", path, err) | ||
return err | ||
} | ||
|
||
minimumOldestTime := time.Now().Add(-setting.Cron.ArchiveCleanup.OlderThan) | ||
for _, info := range files { | ||
if info.ModTime().Before(minimumOldestTime) && !info.IsDir() { | ||
toDelete := filepath.Join(path, info.Name()) | ||
// This is a best-effort purge, so we do not check error codes to confirm removal. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd still trace-print any error (could be something a sysadmin could fix...) |
||
os.Remove(toDelete) | ||
} | ||
} | ||
} | ||
|
||
return nil | ||
}); err != nil { | ||
log.Error(4, "ArchiveClean: %v", err) | ||
} | ||
} | ||
|
||
func gatherMissingRepoRecords() ([]*Repository, error) { | ||
repos := make([]*Repository, 0, 10) | ||
if err := x. | ||
|
@@ -1915,9 +1968,10 @@ func RewriteRepositoryUpdateHook() error { | |
var taskStatusTable = sync.NewStatusTable() | ||
|
||
const ( | ||
mirrorUpdate = "mirror_update" | ||
gitFsck = "git_fsck" | ||
checkRepos = "check_repos" | ||
mirrorUpdate = "mirror_update" | ||
gitFsck = "git_fsck" | ||
checkRepos = "check_repos" | ||
archiveCleanup = "archive_cleanup" | ||
) | ||
|
||
// GitFsck calls 'git fsck' to check repository health. | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could this be defined as a standalone function, for better readability ?