这是indexloc提供的服务,不要输入任何密码
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions docs/processes/scheduled-cron-tasks.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,17 @@ dokku cron:list node-js-app --format json
[{"id":"cGhwPT09cGhwIHRlc3QucGhwPT09QGRhaWx5","app":"node-js-app","command":"node index.js","schedule":"@daily"}]
```

To fetch global tasks, use the `--global` flag:

```shell
dokku cron:list --global
```

```
ID Schedule Command
5cruaotm4yzzpnjlsdunblj8qyjp @daily /bin/true
```

#### Executing a cron task on the fly

Cron tasks can be invoked via the `cron:run` command. This command takes an `app` argument and a `cron id` (retrievable from `cron:list` output).
Expand Down
43 changes: 42 additions & 1 deletion plugins/cron/cron.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cron

import (
"fmt"
"strings"

appjson "github.com/dokku/dokku/plugins/app-json"
"github.com/dokku/dokku/plugins/common"
Expand Down Expand Up @@ -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"`

Expand Down Expand Up @@ -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))
Expand Down
4 changes: 4 additions & 0 deletions plugins/cron/src/subcommands/subcommands.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,13 @@ func main() {
switch subcommand {
case "list":
args := flag.NewFlagSet("cron:list", flag.ExitOnError)
global := args.Bool("global", false, "--global: set a global property")
format := args.String("format", "stdout", "format: [ stdout | json ]")
args.Parse(os.Args[2:])
appName := args.Arg(0)
if *global {
appName = "--global"
}
err = cron.CommandList(appName, *format)
case "report":
args := flag.NewFlagSet("cron:report", flag.ExitOnError)
Expand Down
23 changes: 16 additions & 7 deletions plugins/cron/subcommands.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,6 @@ import (

// CommandList lists all scheduled cron tasks for a given app
func CommandList(appName string, format string) error {
if err := common.VerifyAppName(appName); err != nil {
return err
}

if format == "" {
format = "stdout"
}
Expand All @@ -26,9 +22,22 @@ func CommandList(appName string, format string) error {
return fmt.Errorf("Invalid format specified, supported formats: json, stdout")
}

entries, err := FetchCronEntries(FetchCronEntriesInput{AppName: appName})
if err != nil {
return err
var entries []TemplateCommand
if appName == "--global" {
var err error
entries, err = FetchGlobalCronEntries()
if err != nil {
return err
}
} else {
var err error
if err := common.VerifyAppName(appName); err != nil {
return err
}
entries, err = FetchCronEntries(FetchCronEntriesInput{AppName: appName})
if err != nil {
return err
}
}

if format == "stdout" {
Expand Down
7 changes: 6 additions & 1 deletion tests/unit/cron.bats
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ version = "0.0.1"
[plugin.config]
EOF
cp /var/lib/dokku/plugins/available/cron-entries/plugin.toml /var/lib/dokku/plugins/enabled/cron-entries/plugin.toml

}

teardown() {
Expand Down Expand Up @@ -126,6 +125,12 @@ teardown() {
echo "status: $status"
assert_success

run /bin/bash -c "dokku cron:list --global --format json"
echo "output: $output"
echo "status: $status"
assert_success
assert_output '[{"id":"5cruaotm4yzzpnjlsdunblj8qyjp","command":"/bin/true","global":true,"schedule":"@daily","maintenance":false}]'

run /bin/bash -c "cat /var/spool/cron/crontabs/dokku"
echo "output: $output"
echo "status: $status"
Expand Down
Loading