feat: add ability to suspend and resume a cron task by ID

This takes advantage of the properties system and allows users to suspend/resume the task at will.
This commit is contained in:
Jose Diaz-Gonzalez
2025-09-21 20:52:57 -04:00
parent 3c7985cd82
commit f99e3a6c9c
6 changed files with 99 additions and 13 deletions

View File

@@ -12,27 +12,27 @@ import (
)
// CommandPropertySet is a generic function that will set a property for a given plugin/app combination
func CommandPropertySet(pluginName, appName, property, value string, properties map[string]string, globalProperties map[string]bool) {
func CommandPropertySet(pluginName, appName, property, value string, validProperties map[string]string, validGlobalProperties map[string]bool) {
if appName != "--global" {
if err := VerifyAppName(appName); err != nil {
LogFailWithError(err)
}
}
if appName == "--global" && !globalProperties[property] {
if appName == "--global" && !validGlobalProperties[property] {
LogFail("Property cannot be specified globally")
}
if property == "" {
LogFail("No property specified")
}
for k := range globalProperties {
if _, ok := properties[k]; !ok {
properties[k] = ""
for k := range validGlobalProperties {
if _, ok := validProperties[k]; !ok {
validProperties[k] = ""
}
}
if _, ok := properties[property]; !ok {
properties := reflect.ValueOf(properties).MapKeys()
if _, ok := validProperties[property]; !ok {
properties := reflect.ValueOf(validProperties).MapKeys()
validPropertyList := make([]string, len(properties))
for i := 0; i < len(properties); i++ {
validPropertyList[i] = properties[i].String()

View File

@@ -1,4 +1,4 @@
SUBCOMMANDS = subcommands/list subcommands/report subcommands/run subcommands/set
SUBCOMMANDS = subcommands/list subcommands/report cron/resume subcommands/run subcommands/set subcommands/suspend
TRIGGERS = triggers/app-json-is-valid triggers/cron-get-property triggers/install triggers/post-app-clone-setup triggers/post-app-rename-setup triggers/post-delete triggers/scheduler-stop
BUILD = commands subcommands triggers
PLUGIN_NAME = cron

View File

@@ -2,6 +2,7 @@ package cron
import (
"fmt"
"strconv"
"strings"
appjson "github.com/dokku/dokku/plugins/app-json"
@@ -27,6 +28,8 @@ var (
}
)
const MaintenancePropertyPrefix = "maintenance."
// CronTask is a struct that represents a cron task
type CronTask struct {
// ID is a unique identifier for the cron task
@@ -102,6 +105,11 @@ func FetchCronTasks(input FetchCronTasksInput) ([]CronTask, error) {
return tasks, nil
}
properties, err := common.PropertyGetAllByPrefix("cron", appName, MaintenancePropertyPrefix)
if err != nil {
return tasks, fmt.Errorf("Error getting maintenance properties: %w", err)
}
for i, c := range input.AppJSON.Cron {
if c.Command == "" {
if input.WarnToFailure {
@@ -127,7 +135,19 @@ func FetchCronTasks(input FetchCronTasksInput) ([]CronTask, error) {
return tasks, fmt.Errorf("Invalid cron schedule for app %s (schedule %s): %s", appName, c.Schedule, err.Error())
}
maintenance := isAppCronInMaintenance || c.Maintenance
maintenance := c.Maintenance
if value, ok := properties[MaintenancePropertyPrefix+c.Schedule]; ok {
boolValue, err := strconv.ParseBool(value)
if err != nil {
return tasks, fmt.Errorf("Invalid maintenance property for app %s (schedule %s): %s", appName, c.Schedule, err.Error())
}
// only override the maintenance value if the property is set to true
if boolValue {
maintenance = boolValue
}
}
tasks = append(tasks, CronTask{
App: appName,
Command: c.Command,

View File

@@ -37,6 +37,12 @@ func main() {
appName := args.Arg(0)
err = cron.CommandReport(appName, *format, infoFlag)
}
case "resume":
args := flag.NewFlagSet("cron:resume", flag.ExitOnError)
args.Parse(os.Args[2:])
appName := args.Arg(0)
cronID := args.Arg(1)
err = cron.CommandResume(appName, cronID)
case "run":
args := flag.NewFlagSet("cron:run", flag.ExitOnError)
detached := args.Bool("detach", false, "--detach: run the container in a detached mode")
@@ -57,6 +63,12 @@ func main() {
value = args.Arg(1)
}
err = cron.CommandSet(appName, property, value)
case "suspend":
args := flag.NewFlagSet("cron:suspend", flag.ExitOnError)
args.Parse(os.Args[2:])
appName := args.Arg(0)
cronID := args.Arg(1)
err = cron.CommandSuspend(appName, cronID)
default:
err = fmt.Errorf("Invalid plugin subcommand call: %s", subcommand)
}

View File

@@ -5,6 +5,7 @@ import (
"errors"
"fmt"
"os"
"strings"
"github.com/dokku/dokku/plugins/common"
@@ -43,7 +44,15 @@ func CommandList(appName string, format string) error {
if format == "stdout" {
output := []string{"ID | Schedule | Maintenance | Command"}
for _, task := range tasks {
output = append(output, fmt.Sprintf("%s | %s | %t | %s", task.ID, task.Schedule, task.Maintenance, task.Command))
maintenance := "false"
if task.Maintenance {
if task.TaskInMaintenance {
maintenance = "true (task)"
} else if task.AppInMaintenance {
maintenance = "true (app)"
}
}
output = append(output, fmt.Sprintf("%s | %s | %s | %s", task.ID, task.Schedule, maintenance, task.Command))
}
result := columnize.SimpleFormat(output)
@@ -82,6 +91,11 @@ func CommandReport(appName string, format string, infoFlag string) error {
return ReportSingleApp(appName, format, infoFlag)
}
// CommandResume resumes a cron task
func CommandResume(appName string, cronID string) error {
return CommandSet(appName, fmt.Sprintf("%s%s", MaintenancePropertyPrefix, cronID), "")
}
// CommandRun executes a cron task on the fly
func CommandRun(appName string, cronID string, detached bool) error {
if err := common.VerifyAppName(appName); err != nil {
@@ -138,7 +152,42 @@ func CommandSet(appName string, property string, value string) error {
return err
}
common.CommandPropertySet("cron", appName, property, value, DefaultProperties, GlobalProperties)
validProperties := DefaultProperties
globalProperties := GlobalProperties
if strings.HasPrefix(property, MaintenancePropertyPrefix) {
if appName == "--global" {
return fmt.Errorf("Task maintenance properties cannot be set globally")
}
maintenanceParts := strings.SplitN(property, ".", 2)
if len(maintenanceParts) != 3 {
return fmt.Errorf("Invalid task maintenance property, expected format: %s$ID: %s", MaintenancePropertyPrefix, property)
}
if maintenanceParts[1] == "" {
return fmt.Errorf("Invalid task maintenance property, missing ID")
}
tasks, err := FetchCronTasks(FetchCronTasksInput{AppName: appName})
if err != nil {
return err
}
maintenanceID := maintenanceParts[1]
for _, task := range tasks {
if task.ID == maintenanceID {
validProperties[property] = ""
globalProperties[property] = false
break
}
}
if _, ok := validProperties[property]; !ok {
return fmt.Errorf("Invalid task maintenance property, no matching task ID found: %s", property)
}
}
common.CommandPropertySet("cron", appName, property, value, validProperties, globalProperties)
scheduler := common.GetAppScheduler(appName)
_, err := common.CallPlugnTrigger(common.PlugnTriggerInput{
Trigger: "scheduler-cron-write",
@@ -147,3 +196,8 @@ func CommandSet(appName string, property string, value string) error {
})
return err
}
// CommandSuspend suspends a cron task
func CommandSuspend(appName string, cronID string) error {
return CommandSet(appName, fmt.Sprintf("%s%s", MaintenancePropertyPrefix, cronID), "true")
}

View File

@@ -163,7 +163,7 @@ func TriggerSchedulerAppStatus(scheduler string, appName string) error {
return nil
}
// TriggerSchedulerCronWrite writes out cron entries for a given application
// TriggerSchedulerCronWrite writes out cron tasks for a given application
func TriggerSchedulerCronWrite(scheduler string, appName string) error {
if scheduler != "k3s" {
return nil
@@ -171,7 +171,7 @@ func TriggerSchedulerCronWrite(scheduler string, appName string) error {
cronTasks, err := cron.FetchCronTasks(cron.FetchCronTasksInput{AppName: appName})
if err != nil {
return fmt.Errorf("Error fetching cron entries: %w", err)
return fmt.Errorf("Error fetching cron tasks: %w", err)
}
ctx, cancel := context.WithCancel(context.Background())