/
cron.go
89 lines (74 loc) · 1.93 KB
/
cron.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package dokku
import (
"fmt"
"github.com/texm/dokku-go/internal/reports"
"strings"
)
type cronManager interface {
ListAppCronTasks(appName string) ([]CronTask, error)
GetAppCronReport(appName string) (*AppCronReport, error)
GetAllAppCronReport() (CronReport, error)
}
type CronTask struct {
ID string
Schedule string
Command string
}
type AppCronReport struct {
TaskCount int `dokku:"Cron task count"`
}
type CronReport map[string]*AppCronReport
const (
cronAppListCmd = "cron:list %s"
cronReportCmd = "cron:report"
cronAppReportCmd = "cron:report %s"
)
func parseCronOutput(output string) ([]CronTask, error) {
lines := strings.Split(output, "\n")
columnLine := lines[0]
scheduleIndex := strings.Index(columnLine, "Schedule")
commandIndex := strings.Index(columnLine, "Command")
crons := make([]CronTask, len(lines)-1)
for i, line := range lines[1:] {
id := strings.TrimSpace(line[:scheduleIndex])
schedule := strings.TrimSpace(line[scheduleIndex:commandIndex])
command := strings.TrimSpace(line[commandIndex:])
crons[i] = CronTask{
ID: id,
Schedule: schedule,
Command: command,
}
}
return crons, nil
}
func (c *BaseClient) ListAppCronTasks(appName string) ([]CronTask, error) {
cmd := fmt.Sprintf(cronAppListCmd, appName)
out, err := c.Exec(cmd)
if err != nil {
return nil, err
}
return parseCronOutput(out)
}
func (c *BaseClient) GetAppCronReport(appName string) (*AppCronReport, error) {
cmd := fmt.Sprintf(cronAppReportCmd, appName)
out, err := c.Exec(cmd)
if err != nil {
return nil, err
}
var report AppCronReport
if err := reports.ParseInto(out, &report); err != nil {
return nil, err
}
return &report, nil
}
func (c *BaseClient) GetAllAppCronReport() (CronReport, error) {
out, err := c.Exec(cronReportCmd)
if err != nil {
return nil, err
}
var report CronReport
if err := reports.ParseIntoMap(out, &report); err != nil {
return nil, err
}
return report, nil
}