-
Notifications
You must be signed in to change notification settings - Fork 392
/
Copy pathdkron.go
85 lines (69 loc) · 2.48 KB
/
dkron.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
package cmd
import (
"fmt"
"os"
"strings"
"github.com/distribworks/dkron/v4/dkron"
"github.com/distribworks/dkron/v4/logging"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
var cfgFile string
var config = dkron.DefaultConfig()
var rpcAddr string
var ip string
// dkronCmd represents the dkron command
var dkronCmd = &cobra.Command{
Use: "dkron",
Short: "Open source distributed job scheduling system",
Long: `Dkron is a system service that runs scheduled jobs at given intervals or times,
just like the cron unix service but distributed in several machines in a cluster.
If a machine fails (the leader), a follower will take over and keep running the scheduled jobs without human intervention.`,
}
// Execute adds all child commands to the root command and sets flags appropriately.
// This is called by main.main(). It only needs to happen once to the rootCmd.
func Execute() {
if err := dkronCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
}
}
// initConfig reads in config file and ENV variables if set.
func initConfig() error {
if cfgFile != "" {
// Use config file from the flag.
viper.SetConfigFile(cfgFile)
} else {
viper.SetConfigName("dkron") // name of config file (without extension)
viper.AddConfigPath("/etc/dkron") // call multiple times to add many search paths
viper.AddConfigPath("$HOME/.dkron") // call multiple times to add many search paths
viper.AddConfigPath("./config") // call multiple times to add many search paths
}
viper.SetEnvPrefix("dkron")
replacer := strings.NewReplacer("-", "_")
viper.SetEnvKeyReplacer(replacer)
viper.AutomaticEnv() // read in environment variables that match
// Add hook to set error logs to stderr and regular logs to stdout
logrus.AddHook(&logging.LogSplitter{})
err := viper.ReadInConfig() // Find and read the config file
if err != nil { // Handle errors reading the config file
logrus.WithError(err).Info("No valid config found: Applying default values.")
}
if err := viper.Unmarshal(config); err != nil {
return fmt.Errorf("config: Error unmarshalling config: %s", err)
}
cliTags := viper.GetStringSlice("tag")
var tags map[string]string
if len(cliTags) > 0 {
tags, err = UnmarshalTags(cliTags)
if err != nil {
return fmt.Errorf("config: Error unmarshalling cli tags: %s", err)
}
} else {
tags = viper.GetStringMapString("tags")
}
config.Tags = tags
dkron.InitLogger(viper.GetString("log-level"), config.NodeName)
return nil
}