/
old_job_config.go
99 lines (87 loc) · 2.45 KB
/
old_job_config.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
package config
import (
"io/ioutil"
"github.com/chef/automate/components/ingest-service/serveropts"
toml "github.com/pelletier/go-toml"
"github.com/pkg/errors"
)
// Enum jobs
const (
DeleteNodes int = iota
NodesMissing
MissingNodesForDeletion
)
// List of jobs
var JobList = map[int]string{
DeleteNodes: "delete_nodes",
NodesMissing: "missing_nodes",
MissingNodesForDeletion: "missing_nodes_for_deletion",
}
// JobConfig is the config for a job
type JobConfig struct {
// The ID of the job
ID int `toml:"id"`
// The threshold time that the job will use internally to do a task
Threshold string `toml:"threshold"`
// How often to run the job
Every string `toml:"every"`
// Is the job running
Running bool `toml:"running"`
}
type OldJobConfig struct {
Config *Config
DefaultJobConfigs []JobConfig
}
type Config struct {
JobsConfig []JobConfig `toml:"jobs_config"`
}
// ConfigForJob returns the configuration for the given job index. The
// job indexes are constants in this package and reflect this
// historical structure of the on-disk job configuration. The default
// configuration is used if the configuration for a given index cannot
// be found.
func (c *OldJobConfig) ConfigForJob(jobIndex int) (JobConfig, error) {
if c != nil && jobIndex < len(c.Config.JobsConfig) {
return c.Config.JobsConfig[jobIndex], nil
} else if jobIndex < len(c.DefaultJobConfigs) {
return c.DefaultJobConfigs[jobIndex], nil
}
return JobConfig{}, errors.New("no default configuration for job")
}
// NewOldJobConfig - Create a OldJobConfig
func NewOldJobConfig(opts serveropts.JobsConfig) *OldJobConfig {
return &OldJobConfig{
Config: &Config{},
DefaultJobConfigs: []JobConfig{
DeleteNodes: {
ID: DeleteNodes,
Threshold: "1d",
Every: "15m",
Running: false,
},
NodesMissing: {
ID: NodesMissing,
Threshold: "1d",
Every: "15m",
Running: opts.NodesMissingRunningDefault,
},
MissingNodesForDeletion: {
ID: MissingNodesForDeletion,
Threshold: "30d",
Every: "15m",
Running: opts.MissingNodesForDeletionRunningDefault,
},
},
}
}
func (c *OldJobConfig) FromFile(configFile string) error {
tomlData, err := ioutil.ReadFile(configFile)
if err != nil {
return errors.Wrap(err, "unable to read old job config")
}
err = toml.Unmarshal(tomlData, c.Config)
if err != nil {
return errors.Wrap(err, "unable to parse old job config")
}
return nil
}