feat: add ability to list global cron tasks

Closes #7732
This commit is contained in:
Jose Diaz-Gonzalez
2025-09-14 04:38:19 -04:00
parent 8b9b658951
commit 7adeee7086
5 changed files with 79 additions and 9 deletions

View File

@@ -2,6 +2,7 @@ package cron
import (
"fmt"
"strings"
appjson "github.com/dokku/dokku/plugins/app-json"
"github.com/dokku/dokku/plugins/common"
@@ -32,11 +33,14 @@ type TemplateCommand struct {
ID string `json:"id"`
// App is the app the cron command belongs to
App string `json:"app"`
App string `json:"app,omitempty"`
// Command is the command to run
Command string `json:"command"`
// Global is whether the cron command is global
Global bool `json:"global,omitempty"`
// Schedule is the cron schedule
Schedule string `json:"schedule"`
@@ -125,6 +129,43 @@ func FetchCronEntries(input FetchCronEntriesInput) ([]TemplateCommand, error) {
return commands, nil
}
// FetchGlobalCronEntries returns a list of global cron commands
// This function should only be used for the cron:list --global command
// and not internally by the cron plugin
func FetchGlobalCronEntries() ([]TemplateCommand, error) {
commands := []TemplateCommand{}
response, _ := common.CallPlugnTrigger(common.PlugnTriggerInput{
Trigger: "cron-entries",
Args: []string{"docker-local"},
})
for _, line := range strings.Split(response.StdoutContents(), "\n") {
if strings.TrimSpace(line) == "" {
continue
}
parts := strings.Split(line, ";")
if len(parts) != 2 && len(parts) != 3 {
common.LogWarn(fmt.Sprintf("Invalid injected cron task: %v", line))
continue
}
id := base36.EncodeToStringLc([]byte(strings.Join(parts, ";;;")))
command := TemplateCommand{
ID: id,
Schedule: parts[0],
Command: parts[1],
AltCommand: parts[1],
Maintenance: false,
Global: true,
}
if len(parts) == 3 {
command.LogFile = parts[2]
}
commands = append(commands, command)
}
return commands, nil
}
// GenerateCommandID creates a unique ID for a given app/command/schedule combination
func GenerateCommandID(appName string, c appjson.CronCommand) string {
return base36.EncodeToStringLc([]byte(appName + "===" + c.Command + "===" + c.Schedule))