2021-01-18 18:15:42 -05:00
|
|
|
package cron
|
|
|
|
|
|
|
|
|
|
import (
|
2023-07-02 04:36:08 -04:00
|
|
|
"encoding/json"
|
2021-01-18 18:15:42 -05:00
|
|
|
"fmt"
|
2023-07-02 04:53:16 -04:00
|
|
|
"os"
|
2021-01-18 18:15:42 -05:00
|
|
|
|
|
|
|
|
"github.com/dokku/dokku/plugins/common"
|
2023-07-02 04:53:16 -04:00
|
|
|
|
2021-01-18 18:15:42 -05:00
|
|
|
"github.com/ryanuber/columnize"
|
2023-07-02 04:53:16 -04:00
|
|
|
"mvdan.cc/sh/v3/shell"
|
2021-01-18 18:15:42 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// CommandList lists all scheduled cron tasks for a given app
|
2023-07-02 04:36:08 -04:00
|
|
|
func CommandList(appName string, format string) error {
|
2021-01-18 18:15:42 -05:00
|
|
|
if err := common.VerifyAppName(appName); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-02 04:36:08 -04:00
|
|
|
if format == "" {
|
|
|
|
|
format = "stdout"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if format != "stdout" && format != "json" {
|
|
|
|
|
return fmt.Errorf("Invalid format specified, supported formats: json, stdout")
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-02 02:43:55 -04:00
|
|
|
entries, err := FetchCronEntries(appName)
|
2021-01-18 18:15:42 -05:00
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-02 04:36:08 -04:00
|
|
|
if format == "stdout" {
|
|
|
|
|
output := []string{"ID | Schedule | Command"}
|
|
|
|
|
for _, entry := range entries {
|
|
|
|
|
output = append(output, fmt.Sprintf("%s | %s | %s", entry.ID, entry.Schedule, entry.Command))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
result := columnize.SimpleFormat(output)
|
|
|
|
|
fmt.Println(result)
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
out, err := json.Marshal(entries)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
2021-01-18 18:15:42 -05:00
|
|
|
}
|
2023-07-02 04:36:08 -04:00
|
|
|
common.Log(string(out))
|
2021-01-18 18:15:42 -05:00
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// CommandReport displays a cron report for one or more apps
|
2021-02-01 22:23:05 -05:00
|
|
|
func CommandReport(appName string, format string, infoFlag string) error {
|
2021-01-18 18:15:42 -05:00
|
|
|
if len(appName) == 0 {
|
|
|
|
|
apps, err := common.DokkuApps()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
for _, appName := range apps {
|
2021-02-01 22:23:05 -05:00
|
|
|
if err := ReportSingleApp(appName, format, infoFlag); err != nil {
|
2021-01-18 18:15:42 -05:00
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-01 22:23:05 -05:00
|
|
|
return ReportSingleApp(appName, format, infoFlag)
|
2021-01-18 18:15:42 -05:00
|
|
|
}
|
2023-07-02 04:53:16 -04:00
|
|
|
|
|
|
|
|
// CommandRun executes a cron command on the fly
|
|
|
|
|
func CommandRun(appName string, cronID string, detached bool) error {
|
|
|
|
|
if err := common.VerifyAppName(appName); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
entries, err := FetchCronEntries(appName)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if cronID == "" {
|
|
|
|
|
return fmt.Errorf("Please specify a Cron ID from the output of 'dokku cron:list %s'", appName)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
command := ""
|
|
|
|
|
for _, entry := range entries {
|
|
|
|
|
if entry.ID == cronID {
|
|
|
|
|
command = entry.Command
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if command == "" {
|
|
|
|
|
return fmt.Errorf("No matching Cron ID found. Please specify a Cron ID from the output of 'dokku cron:list %s'", appName)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fields, err := shell.Fields(command, func(name string) string {
|
|
|
|
|
return ""
|
|
|
|
|
})
|
|
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("Could not parse command: %s", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if detached {
|
|
|
|
|
os.Setenv("DOKKU_DETACH_CONTAINER", "1")
|
|
|
|
|
os.Setenv("DOKKU_DISABLE_TTY", "true")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
os.Setenv("DOKKU_CRON_ID", cronID)
|
|
|
|
|
os.Setenv("DOKKU_RM_CONTAINER", "1")
|
|
|
|
|
scheduler := common.GetAppScheduler(appName)
|
2023-08-26 15:19:12 -04:00
|
|
|
args := append([]string{scheduler, appName, "0"}, fields...)
|
2023-07-02 04:53:16 -04:00
|
|
|
return common.PlugnTrigger("scheduler-run", args...)
|
|
|
|
|
}
|