88 "os/signal"
99 "sync"
1010 "syscall"
11- "time"
1211
1312 "github.com/pelletier/go-toml/v2"
1413
@@ -21,8 +20,9 @@ import (
2120 "github.com/dnstapir/observation-encoder/internal/nats"
2221)
2322
24- const c_ENVVAR_OVERRIDE_NATS_URL = "DNSTAPIR_NATS_URL"
23+ const env_DNSTAPIR_NATS_URL = "DNSTAPIR_NATS_URL"
2524
25+ /* Rewritten if building with make */
2626var commit = "BAD-BUILD"
2727
2828type conf struct {
@@ -84,13 +84,16 @@ func main() {
8484
8585 confDecoder := toml.NewDecoder(file)
8686 if confDecoder == nil {
87- log.Error("Problem decoding config file '%s', exiting...", configFile)
87+ log.Error("Problem creating decoder for config file '%s', exiting...", configFile)
8888 os.Exit(-1)
8989 }
9090
9191 confDecoder.DisallowUnknownFields()
92- confDecoder.Decode(&mainConf)
93- file.Close() // TODO okay to close here while also using defer above?
92+ err = confDecoder.Decode(&mainConf)
93+ if err != nil {
94+ log.Error("Problem decoding config file '%s': %s", configFile, err)
95+ os.Exit(-1)
96+ }
9497
9598 debugFlag = debugFlag || mainConf.Debug
9699
@@ -105,12 +108,13 @@ func main() {
105108 })
106109 if err != nil {
107110 log.Error("Error creating nats log: %s", err)
111+ os.Exit(-1)
108112 }
109113
110- envNatsUrl, overrideNatsUrl := os.LookupEnv(c_ENVVAR_OVERRIDE_NATS_URL )
114+ envNatsUrl, overrideNatsUrl := os.LookupEnv(env_DNSTAPIR_NATS_URL )
111115 if overrideNatsUrl {
112116 mainConf.Nats.Url = envNatsUrl
113- log.Info("Overriding NATS url with environment variable '%s'", c_ENVVAR_OVERRIDE_NATS_URL )
117+ log.Info("Overriding NATS url with environment variable '%s'", env_DNSTAPIR_NATS_URL )
114118 }
115119
116120 mainConf.Nats.Log = natslog
@@ -131,6 +135,7 @@ func main() {
131135 })
132136 if err != nil {
133137 log.Error("Error creating libtapir log: %s", err)
138+ os.Exit(-1)
134139 }
135140
136141 mainConf.Libtapir.Log = libtapirlog
@@ -151,6 +156,7 @@ func main() {
151156 })
152157 if err != nil {
153158 log.Error("Error creating app log: %s", err)
159+ os.Exit(-1)
154160 }
155161
156162 mainConf.Log = applog
@@ -173,6 +179,7 @@ func main() {
173179 })
174180 if err != nil {
175181 log.Error("Error creating cert log: %s", err)
182+ os.Exit(-1)
176183 }
177184
178185 mainConf.Cert.Log = certlog
@@ -193,6 +200,7 @@ func main() {
193200 })
194201 if err != nil {
195202 log.Error("Error creating API log: %s", err)
203+ os.Exit(-1)
196204 }
197205 mainConf.Api.Log = apilog
198206 mainConf.Api.App = appHandle
@@ -214,20 +222,18 @@ func main() {
214222 defer signal.Stop(sigChan)
215223
216224 ctx, cancel := context.WithCancel(context.Background())
217- exitCh := make(chan common.Exit)
225+ exitCh := make(chan common.Exit, 10) // TODO adjustable buffer?
218226
219227 log.Info("Starting threads...")
220228
221- go appHandle.Run(ctx, exitCh)
222-
223- go apiHandle.Run(ctx, exitCh)
224- go certHandle.Run(ctx, exitCh)
229+ var wg sync.WaitGroup
230+ wg.Go(func() { appHandle.Run(ctx, exitCh) })
231+ wg.Go(func() { apiHandle.Run(ctx, exitCh) } )
232+ wg.Go(func() { certHandle.Run(ctx, exitCh) } )
225233
226234 log.Info("Threads started!")
227235
228236 exitLoop := false
229- var wg sync.WaitGroup
230- wg.Add(1)
231237 for {
232238 select {
233239 case s, ok := <-sigChan:
@@ -255,44 +261,14 @@ func main() {
255261 }
256262 if exitLoop || (sigChan == nil && exitCh == nil) {
257263 log.Info("Leaving toplevel loop")
258- wg.Done()
259264 break
260265 }
261266 }
262267
263- log.Info("Cancelling, giving threads some time to finish... ")
268+ log.Info("Cancelling threads")
264269 cancel()
265- timeout := make(chan bool, 1)
266- go func() {
267- time.Sleep(2 * time.Second)
268- timeout <- true
269- }()
270-
271- TIMEOUT_LOOP:
272- for {
273- select {
274- case exit, ok := <-exitCh:
275- if ok {
276- if exit.Err != nil {
277- log.Error("%s exited with error: '%s'", exit.ID, exit.Err)
278- } else {
279- log.Info("%s done!", exit.ID)
280- }
281- } else {
282- log.Info("exit channel was closed during shutdown")
283- exitCh = nil
284- }
285- case <-timeout:
286- log.Debug("Time's up. Proceeding with shutdown.")
287- break TIMEOUT_LOOP
288- }
289- if exitCh == nil {
290- log.Warning("exit channel closed unexpectedly")
291- break TIMEOUT_LOOP
292- }
293- }
294270
295- close(exitCh )
271+ log.Info("Waiting for threads to finish" )
296272 wg.Wait()
297273 log.Info("Exiting...")
298274 os.Exit(0)
0 commit comments