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

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