This repository has been archived by the owner on May 27, 2019. It is now read-only.
/
main.go
116 lines (98 loc) · 2.72 KB
/
main.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
package main
import (
"flag"
"fmt"
"github.com/nevsnode/gordon/config"
"github.com/nevsnode/gordon/output"
"github.com/nevsnode/gordon/stats"
"github.com/nevsnode/gordon/taskqueue"
"github.com/nevsnode/gordon/utils"
"log"
"os"
"os/signal"
"syscall"
)
// GordonVersion is the current version of Gordon
const GordonVersion = "1.6.5"
const cliDefaultLogfile = "-"
var cli struct {
config string
configLong string
logfile string
test bool
testLong bool
verbose bool
verboseLong bool
version bool
}
func init() {
flag.StringVar(&cli.config, "c", "", "path to config file")
flag.StringVar(&cli.configLong, "conf", "", "path to config file")
flag.StringVar(&cli.logfile, "logfile", cliDefaultLogfile, "path to logfile (overwrites configuration)")
flag.BoolVar(&cli.test, "t", false, "test configuration file")
flag.BoolVar(&cli.testLong, "test", false, "test configuration file")
flag.BoolVar(&cli.verbose, "v", false, "enable verbose/debugging output")
flag.BoolVar(&cli.verboseLong, "verbose", false, "enable verbose/debugging output")
flag.BoolVar(&cli.version, "version", false, "show version")
}
func main() {
flag.Parse()
if cli.configLong != "" {
cli.config = cli.configLong
}
if cli.testLong {
cli.test = true
}
if cli.verboseLong {
cli.verbose = true
}
if cli.version == true {
fmt.Printf("Gordon version %s\n", GordonVersion)
os.Exit(0)
}
// When no configuration file was passed as a flag, use the default location.
if cli.config == "" {
cli.config = utils.Basepath(config.DefaultConfig)
}
conf, err := config.New(cli.config)
// When test-flag is set, respond accordingly
if cli.test {
if err != nil {
fmt.Println("Configuration is invalid:", err)
} else {
fmt.Println("Configuration is valid")
}
os.Exit(0)
}
if err != nil {
log.Fatal("Configuration is invalid:", err)
}
// Overwrite logfile, if value is passed as a flag
if cli.logfile != cliDefaultLogfile {
conf.Logfile = cli.logfile
}
stats.GordonVersion = GordonVersion
output.SetDebug(cli.verbose)
output.SetErrorScript(conf.ErrorScript)
output.SetTempDir(utils.Basepath(conf.TempDir))
// Set logfile for output, when configured
if conf.Logfile != "" {
err = output.SetLogfile(utils.Basepath(conf.Logfile))
if err != nil {
log.Fatal("output.SetLogfile(): ", err)
}
}
stats.Setup(conf.Stats)
taskqueue.Start(conf)
// Start another go-routine to initiate the graceful shutdown of all taskqueue-workers,
// when the application shall be terminated.
cc := make(chan os.Signal)
signal.Notify(cc, os.Interrupt, os.Kill, syscall.SIGTERM)
go func() {
<-cc
output.Debug("Stopping taskqueue")
taskqueue.Stop()
}()
output.Debug("Up and waiting for tasks")
taskqueue.Wait()
}