/
create_job.go
148 lines (126 loc) · 4.28 KB
/
create_job.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package command
import (
"fmt"
"strings"
client "github.com/ds3lab/easeml/client/go/easemlclient"
"github.com/ds3lab/easeml/client/go/easemlclient/types"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
var jobDataset, jobObjective string
var jobModels, jobAltObjectives []string
var jobAcceptNewModels bool
var jobMaxTasks uint64
var createJobCmd = &cobra.Command{
Use: "job",
Short: "Creates a new job.",
Long: ``,
Run: func(cmd *cobra.Command, args []string) {
if apiKey == "" {
apiKey = viper.GetString("api-key")
}
serverAddress := viper.GetString("server-address")
context := client.Context{ServerAddress: serverAddress, UserCredentials: client.APIKeyCredentials{APIKey: apiKey}}
// Job dataset is required.
for jobDataset == "" {
err := readLine("Job Dataset: ", &jobDataset)
if err != nil {
fmt.Printf(err.Error() + "\n")
return
}
}
// Try to get the dataset.
dataset, err := context.GetDatasetByID(jobDataset)
if err != nil {
fmt.Println("Error: " + err.Error())
return
}
// Find all applicable objectives.
// The input schema of the objective must correspond to the output schema of the dataset.
objectives, err := context.GetModules(types.ModuleObjective, "", types.ModuleActive, "", dataset.SchemaOut, "")
if err != nil {
fmt.Println("Error: " + err.Error())
return
}
if len(objectives) == 0 {
fmt.Println("Error: No objectives applicable to the given dataset.")
return
}
if jobObjective == "" || len(jobAltObjectives) == 0 {
fmt.Println("APPLICABLE OBJECTIVES: ")
for i := range objectives {
fmt.Printf(" - %s\n", objectives[i].ID)
}
}
// Job objective is required.
for jobObjective == "" {
err := readLine("Job Objective: ", &jobObjective)
if err != nil {
fmt.Printf(err.Error() + "\n")
return
}
}
// Job Alternative Objectives is optional.
if len(jobAltObjectives) == 0 {
var altObjectivesString string
err := readLine("Job Alternative Objectives [optional, space-separated]: ", &altObjectivesString)
if err != nil {
fmt.Printf(err.Error() + "\n")
return
}
jobAltObjectives = strings.Fields(altObjectivesString)
}
// Find all applicable models.
models, err := context.GetModules(types.ModuleModel, "", types.ModuleActive, "", dataset.SchemaIn, dataset.SchemaOut)
if err != nil {
fmt.Println("Error: " + err.Error())
return
}
if len(models) == 0 {
fmt.Println("Error: No models applicable to the given dataset.")
return
}
if len(jobModels) == 0 {
fmt.Println("APPLICABLE MODELS: ")
for i := range models {
fmt.Printf(" - %s\n", models[i].ID)
}
}
// Job Models is required.
for len(jobModels) == 0 {
var modelsString string
err := readLine("Job Models [space-separated]: ", &modelsString)
if err != nil {
fmt.Printf(err.Error() + "\n")
return
}
jobModels = strings.Fields(modelsString)
}
//If an asterisk was specified, replace with all applicable models.
if len(jobModels) == 1 && jobModels[0] == "*" {
jobModels = []string{}
for i := range models {
jobModels = append(jobModels, models[i].ID)
}
}
// TODO: Refine this. Enable us to detect when a flag wasn't set.
result, err := context.CreateJob(jobDataset, jobObjective, jobModels, jobAltObjectives, jobAcceptNewModels, jobMaxTasks)
if err != nil {
fmt.Println(err.Error())
return
}
// TODO: Poll the job status until it becomes "ready", to enable the user to have feedback about the process
fmt.Printf("SUCCESS: Job \"%s\" created.\n", result)
},
}
func init() {
createCmd.AddCommand(createJobCmd)
createJobCmd.Flags().StringVar(&jobDataset, "dataset", "", "Job dataset.")
createJobCmd.Flags().StringVar(&jobObjective, "objective", "", "Job objective.")
createJobCmd.Flags().StringArrayVar(&jobModels, "models", []string{}, "Models to apply to the job. "+
"Asterisk (*) denotes all applicable models.")
createJobCmd.Flags().StringArrayVar(&jobAltObjectives, "alt-objectives", []string{}, "Job alternative objectives.")
createJobCmd.Flags().BoolVar(&jobAcceptNewModels, "accept-new-models", false, "Set to indicate that new models "+
"applicable to the job will also be added.")
createJobCmd.Flags().Uint64Var(&jobMaxTasks, "max-tasks", types.DefaultMaxTasks, "Maximum number of tasks to spawn from this job.")
}