Files
summercms/modules/lagoon/schedule.go
Jakub Zych 8818d7b023 feat(12.2-01): add deferred:purge, its daily framework schedule and relation child hooks
- deferred:purge [--days] in lagoon.RuntimeCommands (purge_days, default 5)
- lagoon.FrameworkSchedule entry at purge_at (default 03:00, empty disables)
- conga prepends framework entries as summercms.lagoon[i]:<command>
- pact.Relation{Before,After}{Create,Update,Delete} optional hooks
- lagoon, conga and pact READMEs, scheduling and setup docs
2026-10-02 17:45:41 +02:00

59 lines
1.8 KiB
Go

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
}