mirror of
https://github.com/dokku/dokku.git
synced 2026-02-23 19:50:34 +01:00
refactor: rename command to task in cron code
This commit is contained in:
@@ -27,8 +27,8 @@ var (
|
||||
}
|
||||
)
|
||||
|
||||
// TemplateCommand is a struct that represents a cron task
|
||||
type TemplateCommand struct {
|
||||
// CronTask is a struct that represents a cron task
|
||||
type CronTask struct {
|
||||
// ID is a unique identifier for the cron task
|
||||
ID string `json:"id"`
|
||||
|
||||
@@ -55,7 +55,7 @@ type TemplateCommand struct {
|
||||
}
|
||||
|
||||
// DokkuRunCommand returns the dokku run command to execute for a given cron task
|
||||
func (t TemplateCommand) DokkuRunCommand() string {
|
||||
func (t CronTask) DokkuRunCommand() string {
|
||||
if t.AltCommand != "" {
|
||||
if t.LogFile != "" {
|
||||
return fmt.Sprintf("%s &>> %s", t.AltCommand, t.LogFile)
|
||||
@@ -74,28 +74,28 @@ type FetchCronTasksInput struct {
|
||||
}
|
||||
|
||||
// FetchCronTasks returns a list of cron tasks for a given app
|
||||
func FetchCronTasks(input FetchCronTasksInput) ([]TemplateCommand, error) {
|
||||
func FetchCronTasks(input FetchCronTasksInput) ([]CronTask, error) {
|
||||
appName := input.AppName
|
||||
commands := []TemplateCommand{}
|
||||
tasks := []CronTask{}
|
||||
isMaintenance := reportComputedMaintenance(appName) == "true"
|
||||
|
||||
if input.AppJSON == nil {
|
||||
appJSON, err := appjson.GetAppJSON(appName)
|
||||
if err != nil {
|
||||
return commands, fmt.Errorf("Unable to fetch app.json for app %s: %s", appName, err.Error())
|
||||
return tasks, fmt.Errorf("Unable to fetch app.json for app %s: %s", appName, err.Error())
|
||||
}
|
||||
|
||||
input.AppJSON = &appJSON
|
||||
}
|
||||
|
||||
if input.AppJSON.Cron == nil {
|
||||
return commands, nil
|
||||
return tasks, nil
|
||||
}
|
||||
|
||||
for i, c := range input.AppJSON.Cron {
|
||||
if c.Command == "" {
|
||||
if input.WarnToFailure {
|
||||
return commands, fmt.Errorf("Missing cron task command for app %s (index %d)", appName, i)
|
||||
return tasks, fmt.Errorf("Missing cron task command for app %s (index %d)", appName, i)
|
||||
}
|
||||
|
||||
common.LogWarn(fmt.Sprintf("Missing cron task command for app %s (index %d)", appName, i))
|
||||
@@ -104,7 +104,7 @@ func FetchCronTasks(input FetchCronTasksInput) ([]TemplateCommand, error) {
|
||||
|
||||
if c.Schedule == "" {
|
||||
if input.WarnToFailure {
|
||||
return commands, fmt.Errorf("Missing cron schedule for app %s (index %d)", appName, i)
|
||||
return tasks, fmt.Errorf("Missing cron schedule for app %s (index %d)", appName, i)
|
||||
}
|
||||
|
||||
common.LogWarn(fmt.Sprintf("Missing cron schedule for app %s (index %d)", appName, i))
|
||||
@@ -114,10 +114,10 @@ func FetchCronTasks(input FetchCronTasksInput) ([]TemplateCommand, error) {
|
||||
parser := cronparser.NewParser(cronparser.Minute | cronparser.Hour | cronparser.Dom | cronparser.Month | cronparser.Dow | cronparser.Descriptor)
|
||||
_, err := parser.Parse(c.Schedule)
|
||||
if err != nil {
|
||||
return commands, fmt.Errorf("Invalid cron schedule for app %s (schedule %s): %s", appName, c.Schedule, err.Error())
|
||||
return tasks, fmt.Errorf("Invalid cron schedule for app %s (schedule %s): %s", appName, c.Schedule, err.Error())
|
||||
}
|
||||
|
||||
commands = append(commands, TemplateCommand{
|
||||
tasks = append(tasks, CronTask{
|
||||
App: appName,
|
||||
Command: c.Command,
|
||||
Schedule: c.Schedule,
|
||||
@@ -126,14 +126,14 @@ func FetchCronTasks(input FetchCronTasksInput) ([]TemplateCommand, error) {
|
||||
})
|
||||
}
|
||||
|
||||
return commands, nil
|
||||
return tasks, nil
|
||||
}
|
||||
|
||||
// FetchGlobalCronTasks returns a list of global cron tasks
|
||||
// This function should only be used for the cron:list --global command
|
||||
// and not internally by the cron plugin
|
||||
func FetchGlobalCronTasks() ([]TemplateCommand, error) {
|
||||
commands := []TemplateCommand{}
|
||||
func FetchGlobalCronTasks() ([]CronTask, error) {
|
||||
tasks := []CronTask{}
|
||||
response, _ := common.CallPlugnTrigger(common.PlugnTriggerInput{
|
||||
Trigger: "cron-entries",
|
||||
Args: []string{"docker-local"},
|
||||
@@ -150,7 +150,7 @@ func FetchGlobalCronTasks() ([]TemplateCommand, error) {
|
||||
}
|
||||
|
||||
id := base36.EncodeToStringLc([]byte(strings.Join(parts, ";;;")))
|
||||
command := TemplateCommand{
|
||||
task := CronTask{
|
||||
ID: id,
|
||||
Schedule: parts[0],
|
||||
Command: parts[1],
|
||||
@@ -159,11 +159,11 @@ func FetchGlobalCronTasks() ([]TemplateCommand, error) {
|
||||
Global: true,
|
||||
}
|
||||
if len(parts) == 3 {
|
||||
command.LogFile = parts[2]
|
||||
task.LogFile = parts[2]
|
||||
}
|
||||
commands = append(commands, command)
|
||||
tasks = append(tasks, task)
|
||||
}
|
||||
return commands, nil
|
||||
return tasks, nil
|
||||
}
|
||||
|
||||
// GenerateCommandID creates a unique ID for a given app/command/schedule combination
|
||||
|
||||
@@ -22,7 +22,7 @@ func CommandList(appName string, format string) error {
|
||||
return fmt.Errorf("Invalid format specified, supported formats: json, stdout")
|
||||
}
|
||||
|
||||
var tasks []TemplateCommand
|
||||
var tasks []CronTask
|
||||
if appName == "--global" {
|
||||
var err error
|
||||
tasks, err = FetchGlobalCronTasks()
|
||||
|
||||
@@ -7,6 +7,6 @@ MAILTO={{ .Mailto }}
|
||||
PATH=/usr/local/bin:/usr/bin:/bin
|
||||
SHELL=/bin/bash
|
||||
|
||||
{{ range $task := .Commands -}}
|
||||
{{ range $task := .Tasks -}}
|
||||
{{ $task.Schedule }} {{ $task.DokkuRunCommand }}
|
||||
{{ end -}}
|
||||
|
||||
@@ -38,23 +38,23 @@ func deleteCrontab() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func generateCronTasks() ([]cron.TemplateCommand, error) {
|
||||
func generateCronTasks() ([]cron.CronTask, error) {
|
||||
apps, _ := common.UnfilteredDokkuApps()
|
||||
|
||||
g := new(errgroup.Group)
|
||||
results := make(chan []cron.TemplateCommand, len(apps)+1)
|
||||
results := make(chan []cron.CronTask, len(apps)+1)
|
||||
for _, appName := range apps {
|
||||
appName := appName
|
||||
g.Go(func() error {
|
||||
scheduler := common.GetAppScheduler(appName)
|
||||
if scheduler != "docker-local" {
|
||||
results <- []cron.TemplateCommand{}
|
||||
results <- []cron.CronTask{}
|
||||
return nil
|
||||
}
|
||||
|
||||
c, err := cron.FetchCronTasks(cron.FetchCronTasksInput{AppName: appName})
|
||||
if err != nil {
|
||||
results <- []cron.TemplateCommand{}
|
||||
results <- []cron.CronTask{}
|
||||
common.LogWarn(err.Error())
|
||||
return nil
|
||||
}
|
||||
@@ -65,55 +65,55 @@ func generateCronTasks() ([]cron.TemplateCommand, error) {
|
||||
}
|
||||
|
||||
g.Go(func() error {
|
||||
commands := []cron.TemplateCommand{}
|
||||
tasks := []cron.CronTask{}
|
||||
response, _ := common.CallPlugnTrigger(common.PlugnTriggerInput{
|
||||
Trigger: "cron-entries",
|
||||
Args: []string{"docker-local"},
|
||||
})
|
||||
for _, line := range strings.Split(response.StdoutContents(), "\n") {
|
||||
if strings.TrimSpace(line) == "" {
|
||||
results <- []cron.TemplateCommand{}
|
||||
results <- []cron.CronTask{}
|
||||
return nil
|
||||
}
|
||||
|
||||
parts := strings.Split(line, ";")
|
||||
if len(parts) != 2 && len(parts) != 3 {
|
||||
results <- []cron.TemplateCommand{}
|
||||
results <- []cron.CronTask{}
|
||||
return fmt.Errorf("Invalid injected cron task: %v", line)
|
||||
}
|
||||
|
||||
id := base36.EncodeToStringLc([]byte(strings.Join(parts, ";;;")))
|
||||
command := cron.TemplateCommand{
|
||||
task := cron.CronTask{
|
||||
ID: id,
|
||||
Schedule: parts[0],
|
||||
AltCommand: parts[1],
|
||||
Maintenance: false,
|
||||
}
|
||||
if len(parts) == 3 {
|
||||
command.LogFile = parts[2]
|
||||
task.LogFile = parts[2]
|
||||
}
|
||||
commands = append(commands, command)
|
||||
tasks = append(tasks, task)
|
||||
}
|
||||
results <- commands
|
||||
results <- tasks
|
||||
return nil
|
||||
})
|
||||
|
||||
err := g.Wait()
|
||||
close(results)
|
||||
|
||||
commands := []cron.TemplateCommand{}
|
||||
tasks := []cron.CronTask{}
|
||||
if err != nil {
|
||||
return commands, err
|
||||
return tasks, err
|
||||
}
|
||||
|
||||
for result := range results {
|
||||
c := result
|
||||
if len(c) > 0 && !c[0].Maintenance {
|
||||
commands = append(commands, c...)
|
||||
tasks = append(tasks, c...)
|
||||
}
|
||||
}
|
||||
|
||||
return commands, nil
|
||||
return tasks, nil
|
||||
}
|
||||
|
||||
func writeCronTasks(scheduler string) error {
|
||||
@@ -122,12 +122,12 @@ func writeCronTasks(scheduler string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
commands, err := generateCronTasks()
|
||||
tasks, err := generateCronTasks()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if len(commands) == 0 {
|
||||
if len(tasks) == 0 {
|
||||
return deleteCrontab()
|
||||
}
|
||||
|
||||
@@ -144,7 +144,7 @@ func writeCronTasks(scheduler string) error {
|
||||
mailto := mailtoResults.StdoutContents()
|
||||
|
||||
data := map[string]interface{}{
|
||||
"Commands": commands,
|
||||
"Tasks": tasks,
|
||||
"Mailfrom": mailfrom,
|
||||
"Mailto": mailto,
|
||||
}
|
||||
|
||||
@@ -317,7 +317,7 @@ func TriggerSchedulerDeploy(scheduler string, appName string, imageTag string) e
|
||||
return fmt.Errorf("Error fetching cron tasks: %w", err)
|
||||
}
|
||||
// remove maintenance cron tasks
|
||||
cronTasks := []cron.TemplateCommand{}
|
||||
cronTasks := []cron.CronTask{}
|
||||
for _, cronTask := range allCronTasks {
|
||||
if !cronTask.Maintenance {
|
||||
cronTasks = append(cronTasks, cronTask)
|
||||
|
||||
Reference in New Issue
Block a user