diff --git a/README.md b/README.md index ca575b6..f8ad082 100644 --- a/README.md +++ b/README.md @@ -85,6 +85,14 @@ You can try to run in dry-run mode first to see what is going to be purged: docker exec -t registry-ui /opt/docker-registry-ui -purge-tags -dry-run +Alternatively, you can schedule the purging task with built-in cron service. + +```config.yaml +purge_tags_keep_days: 90 +purge_tags_keep_count: 2 +purge_tags_schedule: '10 3 * * *' +``` + ### Debug mode To increase http request verbosity, run container with `-e GOREQUEST_DEBUG=1`. diff --git a/config.yml b/config.yml index 39e51a1..c29b5be 100644 --- a/config.yml +++ b/config.yml @@ -40,7 +40,9 @@ admins: [] # Debug mode. Affects only templates. debug: true -# CLI options. # How many days to keep tags but also keep the minimal count provided no matter how old. purge_tags_keep_days: 90 purge_tags_keep_count: 2 +# Schedules to purge tags with cron format. (Only for server mode) +# Empty string disables this feature. +purge_tags_schedule: '' diff --git a/glide.lock b/glide.lock index ee110fa..4687dc7 100644 --- a/glide.lock +++ b/glide.lock @@ -1,5 +1,5 @@ -hash: d156899e94e2d0d92ed200d5729bec5d0d205b5b98bbf2bd89f7f91c9ed7a518 -updated: 2018-05-28T13:28:11.313447+03:00 +hash: fea96c473a02b07acc1d600ee0f71c6a5143f34e5eb04a4c5e3e14378fca46f0 +updated: 2018-06-09T09:03:51.972089+09:00 imports: - name: github.com/CloudyKit/fastprinter version: 74b38d55f37af5d6c05ca11147d616b613a3420e @@ -36,6 +36,8 @@ imports: version: a578a48e8d6ca8b01a3b18314c43c6716bb5f5a3 - name: github.com/pkg/errors version: 816c9085562cd7ee03e7f8188a1cfd942858cded +- name: github.com/robfig/cron + version: b41be1df696709bb6395fe435af20370037c0b4c - name: github.com/tidwall/gjson version: 01f00f129617a6fe98941fb920d6c760241b54d2 - name: github.com/tidwall/match diff --git a/glide.yaml b/glide.yaml index cff1ead..51d0ec8 100644 --- a/glide.yaml +++ b/glide.yaml @@ -14,6 +14,8 @@ import: - package: github.com/mattn/go-sqlite3 version: 1.7.0 - package: github.com/go-sql-driver/mysql +- package: github.com/robfig/cron + version: ~1.1.0 testImport: - package: github.com/smartystreets/goconvey version: 1.6.2 diff --git a/main.go b/main.go index f0e6969..47c0c22 100644 --- a/main.go +++ b/main.go @@ -14,6 +14,7 @@ import ( "github.com/labstack/echo/middleware" "github.com/quiq/docker-registry-ui/events" "github.com/quiq/docker-registry-ui/registry" + "github.com/robfig/cron" "github.com/tidwall/gjson" "gopkg.in/yaml.v2" ) @@ -35,6 +36,7 @@ type configData struct { Debug bool `yaml:"debug"` PurgeTagsKeepDays int `yaml:"purge_tags_keep_days"` PurgeTagsKeepCount int `yaml:"purge_tags_keep_count"` + PurgeTagsSchedule string `yaml:"purge_tags_schedule"` } type template struct { @@ -93,9 +95,20 @@ func main() { // Execute CLI task and exit. if purgeTags { - registry.PurgeOldTags(a.client, purgeDryRun, a.config.PurgeTagsKeepDays, a.config.PurgeTagsKeepCount) + a.purgeOldTags(purgeDryRun) return } + // Schedules to purge tags. + if a.config.PurgeTagsSchedule != "" { + c := cron.New() + task := func() { + a.purgeOldTags(purgeDryRun) + } + if err := c.AddFunc(a.config.PurgeTagsSchedule, task); err != nil { + panic(fmt.Errorf("Invalid schedule format: %s", a.config.PurgeTagsSchedule)) + } + c.Start() + } // Count tags in background. go a.client.CountTags(a.config.CacheRefreshInterval) @@ -269,3 +282,8 @@ func (a *apiClient) receiveEvents(c echo.Context) error { a.eventListener.ProcessEvents(c.Request()) return c.String(http.StatusOK, "OK") } + +// purgeOldTags purges old tags. +func (a *apiClient) purgeOldTags(dryRun bool) { + registry.PurgeOldTags(a.client, dryRun, a.config.PurgeTagsKeepDays, a.config.PurgeTagsKeepCount) +}