-
Notifications
You must be signed in to change notification settings - Fork 175
/
cron_client.go
103 lines (87 loc) · 2.68 KB
/
cron_client.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
package arenaclient
import (
"fmt"
"github.com/kubeflow/arena/pkg/apis/config"
apiscron "github.com/kubeflow/arena/pkg/apis/cron"
"github.com/kubeflow/arena/pkg/apis/types"
"github.com/kubeflow/arena/pkg/apis/utils"
"github.com/kubeflow/arena/pkg/cron"
log "github.com/sirupsen/logrus"
)
type CronClient struct {
namespace string
configer *config.ArenaConfiger
}
// NewCronClient creates a CronClient
func NewCronClient(namespace string, configer *config.ArenaConfiger) *CronClient {
return &CronClient{
namespace: namespace,
configer: configer,
}
}
// Submit submits a training job
func (c *CronClient) SubmitCronTrainingJob(job *apiscron.Job) error {
switch job.Type() {
case types.CronTFTrainingJob:
args := job.Args().(*types.CronTFJobArgs)
return cron.SubmitCronTFJob(c.namespace, args)
}
return nil
}
// Namespace sets the namespace,this operation does not change the default namespace
func (c *CronClient) Namespace(namespace string) *CronClient {
copyCronClient := &CronClient{
namespace: namespace,
configer: c.configer,
}
return copyCronClient
}
// List return all cron task
func (c *CronClient) List(allNamespaces bool) ([]*types.CronInfo, error) {
return cron.ListCrons(c.namespace, allNamespaces)
}
// ListAndPrint lists and prints the job informations
func (c *CronClient) ListAndPrint(allNamespaces bool, format string) error {
outputFormat := utils.TransferPrintFormat(format)
if outputFormat == types.UnknownFormat {
return fmt.Errorf("Unknown output format,only support:[wide|json|yaml]")
}
cronInfos, err := cron.ListCrons(c.namespace, allNamespaces)
if err != nil {
return err
}
cron.DisplayAllCrons(cronInfos, allNamespaces, outputFormat)
return nil
}
func (c *CronClient) Get(name string) (*types.CronInfo, error) {
return cron.GetCronInfo(name, c.namespace)
}
func (c *CronClient) GetAndPrint(name string, format string) error {
outputFormat := utils.TransferPrintFormat(format)
if outputFormat == types.UnknownFormat {
return fmt.Errorf("Unknown output format,only support:[wide|json|yaml]")
}
cronInfo, err := cron.GetCronInfo(name, c.namespace)
if err != nil {
return err
}
cron.DisplayCron(cronInfo, outputFormat)
return nil
}
func (c *CronClient) Suspend(name string) error {
return cron.SuspendCron(name, c.namespace, true)
}
func (c *CronClient) Resume(name string) error {
return cron.SuspendCron(name, c.namespace, false)
}
func (c *CronClient) Delete(names ...string) error {
for _, name := range names {
cronInfo, err := cron.GetCronInfo(name, c.namespace)
if err != nil {
log.Errorf("failed to get cron info of %s, reason: %v", name, err)
continue
}
_ = cron.DeleteCron(name, c.namespace, cronInfo.Type)
}
return nil
}