Merge pull request #10 from uphy/cron

Add built-in cron feature.
This commit is contained in:
Roman Vynar
2018-07-04 17:32:35 +03:00
committed by GitHub
5 changed files with 36 additions and 4 deletions

20
main.go
View File

@@ -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)
}