-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.go
200 lines (166 loc) · 3.82 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
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
package main
import (
"flag"
"fmt"
"log"
"os"
"os/signal"
"path/filepath"
"sync"
"syscall"
"github.com/BurntSushi/toml"
"github.com/gabrielperezs/goreactor/inputs"
"github.com/gabrielperezs/goreactor/logstreams"
"github.com/gabrielperezs/goreactor/outputs"
"github.com/gabrielperezs/goreactor/reactor"
"github.com/gallir/dynsemaphore"
)
// Config contains all the configuration parameters needed
// by all the plugins and filled with the TOML parser
type Config struct {
MaxConcurrency int
LogStream interface{}
Reactor []interface{}
}
var (
running []*reactor.Reactor
conf Config
configFile string
configDir string
debug bool
mu sync.Mutex
chMain = make(chan bool)
)
func main() {
flag.StringVar(&configFile, "config", "config.conf", "Configuration file")
flag.StringVar(&configDir, "d", "", "Configuration directory")
flag.BoolVar(&debug, "debug", false, "Debug mode")
flag.Parse()
if !debug {
log.SetFlags(0)
}
go sing()
reload()
start()
<-chMain
}
func start() {
hostname, _ := os.Hostname()
lg, err := logstreams.Get(conf.LogStream)
if err != nil {
log.Printf("%s", err.Error())
}
dynsem := dynsemaphore.New(conf.MaxConcurrency)
if conf.MaxConcurrency != 0 {
log.Println("Max Concurrency set to", dynsem.GetConcurrency())
}
for _, r := range conf.Reactor {
nr := reactor.NewReactor(r)
nr.SetLogStreams(lg)
nr.SetHostname(hostname)
nr.SetConcurrencyControl(dynsem)
var err error
nr.I, err = inputs.Get(nr, r)
if err != nil {
log.Printf("ERROR: %s %s", r, err)
}
nr.O, err = outputs.Get(nr, r)
if err != nil {
log.Printf("ERROR: %s %s", r, err)
}
nr.Start()
running = append(running, nr)
}
}
func exit() {
for _, r := range running {
r.Stop() // To force to stop receiving input (SQS)
}
for _, r := range running {
r.Exit()
}
chMain <- true
}
func restart() {
for _, r := range running {
r.Stop() // To force to stop receiving input (SQS)
}
for _, r := range running {
r.Exit()
}
running = nil
start()
}
func reload() {
c, err := readConfig(configFile, configDir)
if err != nil {
log.Printf("ERROR reading config file or directory %s,%s: %s", configFile, configDir, err)
return
}
mu.Lock()
conf = *c
mu.Unlock()
}
func sing() {
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM, syscall.SIGHUP)
for {
switch <-sigs {
case syscall.SIGHUP:
log.Printf("Rotate logs")
reload()
restart()
case syscall.SIGKILL, syscall.SIGTERM, syscall.SIGINT, os.Interrupt:
log.Printf("Exiting...")
exit()
chMain <- true
default:
}
}
}
func readConfig(configFile, configDir string) (c *Config, err error) {
c = &Config{
Reactor: make([]interface{}, 0),
}
if configDir == "" {
//Configuration is a file
_, err = toml.DecodeFile(configFile, &c)
return c, err
}
//Configuration is a directory
fileInfo, err := os.Stat(configDir)
if err != nil {
return nil, err
}
if !fileInfo.IsDir() {
return c, fmt.Errorf("configuration directory is not a directory: %s", configDir)
}
files, err := os.ReadDir(configDir)
if err != nil {
return nil, err
}
for _, file := range files {
if file.IsDir() {
continue
}
if filepath.Ext(file.Name()) != ".conf" {
continue
}
pathTemp := fmt.Sprintf("%s/%s", configDir, file.Name())
var configTemp *Config
if _, err := toml.DecodeFile(pathTemp, &configTemp); err != nil {
log.Printf("ERROR reading config file %s: %s", pathTemp, err)
continue
}
c.Reactor = append(c.Reactor, configTemp.Reactor...)
// Read first LogStream only
if c.LogStream == nil && configTemp.LogStream != nil {
c.LogStream = configTemp.LogStream
}
// Read first MaxConcurrency only
if c.MaxConcurrency == 0 && configTemp.MaxConcurrency != 0 {
c.MaxConcurrency = configTemp.MaxConcurrency
}
}
return c, nil
}