package lagoon import ( "fmt" "regexp" "strconv" "strings" "git.golem15.com/golem15/summercms/modules/backpack" "git.golem15.com/golem15/summercms/modules/pact" ) // FrameworkScheduleID is the plugin id under which the scheduler lists the // framework's own schedule entries, so the daily purge has the entry id // summercms.lagoon[0]:deferred:purge. const FrameworkScheduleID = "summercms.lagoon" const ( purgeAtKey = "database.deferred_bindings.purge_at" purgeDaysKey = "database.deferred_bindings.purge_days" defaultPurgeAt = "03:00" // defaultPurgeDays is WinterCMS's DeferredBinding::cleanUp(5). defaultPurgeDays = 5 ) var purgeAtPattern = regexp.MustCompile(`^([01][0-9]|2[0-3]):([0-5][0-9])$`) // FrameworkSchedule returns the scheduled commands the framework itself // runs: deferred:purge daily at database.deferred_bindings.purge_at ("HH:MM" // in app.timezone, default "03:00"). An explicitly empty purge_at disables // the entry; a malformed one is an error naming the key. A nil app or an app // without config returns no entries and no error. func FrameworkSchedule(app *backpack.App) ([]pact.ScheduledCommand, error) { if app == nil || app.Config == nil { return nil, nil } at := defaultPurgeAt if raw, ok := app.Config.Lookup(purgeAtKey); ok { switch v := raw.(type) { case nil: at = "" case string: at = strings.TrimSpace(v) default: return nil, fmt.Errorf("lagoon: %s must be a quoted \"HH:MM\" string, got %v", purgeAtKey, raw) } } if at == "" { return nil, nil } m := purgeAtPattern.FindStringSubmatch(at) if m == nil { return nil, fmt.Errorf("lagoon: %s %q is not HH:MM (00:00 to 23:59)", purgeAtKey, at) } hour, _ := strconv.Atoi(m[1]) minute, _ := strconv.Atoi(m[2]) return []pact.ScheduledCommand{{Command: "deferred:purge", Cadence: pact.DailyAt(hour, minute)}}, nil }