/
subcommands.go
117 lines (110 loc) · 3.04 KB
/
subcommands.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package cmd
import (
"fmt"
"os"
"time"
"github.com/Tarick/tscheduler/pkg/job"
"github.com/spf13/cobra"
)
var (
// go build -ldflags "-X github.com/Tarick/tscheduler/cmd.buildVer=$(git describe --tags --abbrev=0 HEAD) -X github.com/Tarick/tscheduler/cmd.buildTime=$(date +'%Y-%m-%d_%T')"
buildVer string
buildTime string
versionCmd = &cobra.Command{
Use: "version",
Short: "Print the version number and build date",
Long: `Version information and build date`,
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("Tscheduler ", buildVer, "-", buildTime)
},
}
runCmd = &cobra.Command{
Use: "run",
Short: "Runs scheduling service",
Long: `Runs scheduler, use this to initially start it`,
Run: func(cmd *cobra.Command, args []string) {
runScheduler()
},
}
parseCmd = &cobra.Command{
Use: "parse",
Short: "Parse config and print configured jobs",
Long: `Parses config to validate it, prints jobs and estimated runs`,
Run: func(cmd *cobra.Command, args []string) {
printParsedJobs()
},
}
resumeCmd = &cobra.Command{
Use: "resume",
Short: "Resumes scheduling",
Long: `Resumes previously stopped scheduling.`,
Run: func(cmd *cobra.Command, args []string) {
startScheduler()
},
}
pauseCmd = &cobra.Command{
Use: "stop",
Short: "Pauses (suspends) scheduling",
Long: `Pause suspends scheduling, which can be resumed with "resume".`,
Run: func(cmd *cobra.Command, args []string) {
stopScheduler()
},
}
statusCmd = &cobra.Command{
Use: "status",
Short: "Status returns current scheduler and jobs state",
Long: `Status returns state of scheduler and prints registered jobs.`,
Run: func(cmd *cobra.Command, args []string) {
getStatus()
},
}
shutdownCmd = &cobra.Command{
Use: "shutdown",
Short: "Gracefully stops scheduler and causes program to exit",
Long: `Stops scheduler by calling /stop on management interface and make program exit.`,
Run: func(cmd *cobra.Command, args []string) {
shutdown()
},
}
)
func init() {
rootCmd.AddCommand(runCmd)
rootCmd.AddCommand(versionCmd)
rootCmd.AddCommand(parseCmd)
rootCmd.AddCommand(pauseCmd)
rootCmd.AddCommand(resumeCmd)
rootCmd.AddCommand(statusCmd)
rootCmd.AddCommand(shutdownCmd)
}
func printParsedJobs() {
runs := 5
fmt.Printf("Jobs next %d scheduled runs:\n", runs)
for _, jobConfig := range jobConfigs {
command := createCommand(jobConfig)
schedule := []job.Schedule{}
for _, scheduleSpec := range jobConfig.ScheduleSpec {
if s, err := job.NewSchedule(scheduleSpec); err != nil {
fmt.Printf("Failure creating schedule for job %v: %v \n", jobConfig.ID, err)
os.Exit(1)
} else {
schedule = append(schedule, s)
}
}
j, err := job.New(jobConfig.ID, command, schedule)
if err != nil {
fmt.Println("Failure creating job: ", err)
continue
}
t := time.Now()
fmt.Printf("\n %v\n", j)
for i := 1; i <= runs; i++ {
t, err = j.Next(t)
if err != nil {
fmt.Println(err)
continue
}
fmt.Printf("%d run for the Job: %v\n", i, t)
t = t.Add(1 * time.Second)
}
}
}