Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
1 change: 1 addition & 0 deletions options/locale/locale_en-US.ini
Original file line number Diff line number Diff line change
Expand Up @@ -2794,6 +2794,7 @@ repos.size = Size
packages.package_manage_panel = Package Management
packages.total_size = Total Size: %s
packages.unreferenced_size = Unreferenced Size: %s
packages.cleanup = Cleanup expired data
packages.owner = Owner
packages.creator = Creator
packages.name = Name
Expand Down
12 changes: 12 additions & 0 deletions routers/web/admin/packages.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package admin
import (
"net/http"
"net/url"
"time"

"code.gitea.io/gitea/models/db"
packages_model "code.gitea.io/gitea/models/packages"
Expand All @@ -14,6 +15,7 @@ import (
"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/util"
packages_service "code.gitea.io/gitea/services/packages"
packages_cleanup_service "code.gitea.io/gitea/services/packages/cleanup"
)

const (
Expand Down Expand Up @@ -101,3 +103,13 @@ func DeletePackageVersion(ctx *context.Context) {
"redirect": setting.AppSubURL + "/admin/packages?page=" + url.QueryEscape(ctx.FormString("page")) + "&q=" + url.QueryEscape(ctx.FormString("q")) + "&type=" + url.QueryEscape(ctx.FormString("type")),
})
}

func CleanupExpiredData(ctx *context.Context) {
if err := packages_cleanup_service.CleanupExpiredData(ctx, time.Duration(0)); err != nil {
ctx.ServerError("CleanupExpiredData", err)
return
}

ctx.Flash.Success(ctx.Tr("packages.cleanup.success"))
ctx.Redirect(setting.AppSubURL + "/admin/packages")
}
1 change: 1 addition & 0 deletions routers/web/web.go
Original file line number Diff line number Diff line change
Expand Up @@ -593,6 +593,7 @@ func registerRoutes(m *web.Route) {
m.Group("/packages", func() {
m.Get("", admin.Packages)
m.Post("/delete", admin.DeletePackageVersion)
m.Post("/cleanup", admin.CleanupExpiredData)
}, packagesEnabled)

m.Group("/hooks", func() {
Expand Down
2 changes: 1 addition & 1 deletion services/cron/tasks_basic.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ func registerCleanupPackages() {
OlderThan: 24 * time.Hour,
}, func(ctx context.Context, _ *user_model.User, config Config) error {
realConfig := config.(*OlderThanConfig)
return packages_cleanup_service.Cleanup(ctx, realConfig.OlderThan)
return packages_cleanup_service.CleanupTask(ctx, realConfig.OlderThan)
})
}

Expand Down
26 changes: 22 additions & 4 deletions services/packages/cleanup/cleanup.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,25 @@ import (
debian_service "code.gitea.io/gitea/services/packages/debian"
)

// Cleanup removes expired package data
func Cleanup(taskCtx context.Context, olderThan time.Duration) error {
ctx, committer, err := db.TxContext(taskCtx)
// Task method to execute cleanup rules and cleanup expired package data
func CleanupTask(ctx context.Context, olderThan time.Duration) error {
if err := ExecuteCleanupRules(ctx); err != nil {
return err
}

return CleanupExpiredData(ctx, olderThan)
}

func ExecuteCleanupRules(outerCtx context.Context) error {
ctx, committer, err := db.TxContext(outerCtx)
if err != nil {
return err
}
defer committer.Close()

err = packages_model.IterateEnabledCleanupRules(ctx, func(ctx context.Context, pcr *packages_model.PackageCleanupRule) error {
select {
case <-taskCtx.Done():
case <-outerCtx.Done():
return db.ErrCancelledf("While processing package cleanup rules")
default:
}
Expand Down Expand Up @@ -122,6 +130,16 @@ func Cleanup(taskCtx context.Context, olderThan time.Duration) error {
return err
}

return committer.Commit()
}

func CleanupExpiredData(outerCtx context.Context, olderThan time.Duration) error {
ctx, committer, err := db.TxContext(outerCtx)
if err != nil {
return err
}
defer committer.Close()

if err := container_service.Cleanup(ctx, olderThan); err != nil {
return err
}
Expand Down
6 changes: 6 additions & 0 deletions templates/admin/packages/list.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@
{{.locale.Tr "admin.packages.package_manage_panel"}} ({{.locale.Tr "admin.total" .TotalCount}},
{{.locale.Tr "admin.packages.total_size" (FileSize .TotalBlobSize)}},
{{.locale.Tr "admin.packages.unreferenced_size" (FileSize .TotalUnreferencedBlobSize)}})
<div class="ui right">
<form method="POST" action="/admin/packages/cleanup">
{{.CsrfTokenHtml}}
<button class="ui primary tiny button">{{.locale.Tr "admin.packages.cleanup"}})</button>
</form>
</div>
</h4>
<div class="ui attached segment">
<form class="ui form ignore-dirty">
Expand Down