This repository has been archived by the owner on Feb 13, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
butteredscones.go
96 lines (79 loc) · 2.39 KB
/
butteredscones.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
package main
import (
"flag"
"fmt"
"os"
"os/signal"
"syscall"
"time"
"github.com/boltdb/bolt"
"github.com/digitalocean/butteredscones"
"github.com/digitalocean/butteredscones/client"
"github.com/digitalocean/butteredscones/lumberjack"
"github.com/technoweenie/grohl"
)
func main() {
grohl.AddContext("app", "buttered-scones")
var configFile string
flag.StringVar(&configFile, "config", "", "configuration file path")
flag.Parse()
if configFile == "" {
flag.Usage()
os.Exit(1)
}
config, err := butteredscones.LoadConfiguration(configFile)
if err != nil {
fmt.Printf("error opening configuration file: %s\n", err.Error())
os.Exit(1)
}
clients := make([]client.Client, 0, len(config.Network.Servers))
for _, server := range config.Network.Servers {
tlsConfig, err := config.BuildTLSConfig()
if err != nil {
fmt.Printf("%s\n", err.Error())
os.Exit(1)
}
tlsConfig.ServerName = server.Name
options := &lumberjack.ClientOptions{
Network: "tcp",
Address: server.Addr,
TLSConfig: tlsConfig,
ConnectionTimeout: time.Duration(config.Network.Timeout) * time.Second,
SendTimeout: time.Duration(config.Network.Timeout) * time.Second,
}
client := lumberjack.NewClient(options)
clients = append(clients, client)
}
// clients := []Client{&StdoutClient{}}
db, err := bolt.Open(config.State, 0600, &bolt.Options{Timeout: 2 * time.Second})
if err != nil {
fmt.Printf("error opening state database: %s\n", err.Error())
os.Exit(1)
}
snapshotter := &butteredscones.BoltSnapshotter{DB: db}
if config.Statistics.Addr != "" {
stats_server := &butteredscones.StatisticsServer{
Statistics: butteredscones.GlobalStatistics,
Addr: config.Statistics.Addr,
}
go func() {
err := stats_server.ListenAndServe()
grohl.Report(err, grohl.Data{"msg": "stats server failed to start"})
}()
}
// Default spool size
spoolSize := config.Network.SpoolSize
if spoolSize == 0 {
spoolSize = 1024
}
supervisor := butteredscones.NewSupervisor(config.Files, clients, snapshotter, config.MaxLength)
supervisor.SpoolSize = spoolSize
supervisor.GlobRefresh = 15 * time.Second
supervisor.Start()
signalCh := make(chan os.Signal, 1)
go signal.Notify(signalCh, syscall.SIGTERM, syscall.SIGINT)
signal := <-signalCh
fmt.Printf("Received %s, shutting down cleanly ...\n", signal)
supervisor.Stop()
fmt.Printf("Done shutting down\n")
}