-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathremoteweather.go
97 lines (78 loc) · 2.13 KB
/
remoteweather.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
package main
import (
"context"
"flag"
"fmt"
"os"
"os/signal"
"path/filepath"
"runtime"
"sync"
"syscall"
"go.uber.org/zap"
)
const version = "3.0-" + runtime.GOOS + "/" + runtime.GOARCH
var zapLogger *zap.Logger
var log *zap.SugaredLogger
var debug *bool
func main() {
var wg sync.WaitGroup
var err error
cfgFile := flag.String("config", "config.yaml", "Path to config file (default: ./config.yaml)")
debug = flag.Bool("debug", false, "Turn on debugging output")
flag.Parse()
// Set up our logger
if *debug {
zapLogger, err = zap.NewDevelopment()
} else {
zapLogger, err = zap.NewProduction()
}
if err != nil {
fmt.Printf("can't initialize zap logger: %v", err)
panic(0)
}
defer zapLogger.Sync()
log = zapLogger.Sugar()
// Read our server configuration
filename, _ := filepath.Abs(*cfgFile)
cfg, err := NewConfig(filename)
if err != nil {
log.Fatal("error reading config file. Did you pass the -config flag? Run with -h for help.\n", err)
}
sigs := make(chan os.Signal, 1)
done := make(chan struct{}, 1)
ctx := context.Background()
ctx, cancel := context.WithCancel(ctx)
// Initialize the storage manager
distributor, err := NewStorageManager(ctx, &wg, &cfg)
if err != nil {
log.Fatal(err)
}
// Initialize the weather station manager
wsm, err := NewWeatherStationManager(ctx, &wg, &cfg, distributor.ReadingDistributor, log)
if err != nil {
log.Fatalf("could not create weather station manager: %v", err)
}
go wsm.StartWeatherStations()
// Initialize the controller manager
cm, err := NewControllerManager(ctx, &wg, &cfg, log)
if err != nil {
log.Fatalf("could not create controller manager: %v", err)
}
err = cm.StartControllers()
if err != nil {
log.Fatalf("could not start controllers: %v", err)
}
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
go func(cancel context.CancelFunc) {
// If we get a SIGINT or SIGTERM, cancel the context and unblock 'done'
// to trigger a program shutdown
<-sigs
cancel()
close(done)
}(cancel)
// Wait for 'done' to unblock before terminating
<-done
// Also wait for all of our workers to terminate before terminating the program
wg.Wait()
}