forked from etix/mirrorbits
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
179 lines (159 loc) · 3.87 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
// Copyright (c) 2014-2019 Ludovic Fauvet
// Licensed under the MIT license
package main
import (
"fmt"
"os"
"os/signal"
"runtime/pprof"
"strings"
"syscall"
"time"
"github.com/etix/mirrorbits/cli"
. "github.com/etix/mirrorbits/config"
"github.com/etix/mirrorbits/core"
"github.com/etix/mirrorbits/daemon"
"github.com/etix/mirrorbits/database"
"github.com/etix/mirrorbits/http"
"github.com/etix/mirrorbits/logs"
"github.com/etix/mirrorbits/mirrors"
"github.com/etix/mirrorbits/process"
"github.com/etix/mirrorbits/rpc"
"github.com/op/go-logging"
"github.com/pkg/errors"
)
var (
log = logging.MustGetLogger("main")
)
func main() {
core.Parseflags()
if core.CpuProfile != "" {
f, err := os.Create(core.CpuProfile)
if err != nil {
log.Fatal(err)
}
defer f.Close()
pprof.StartCPUProfile(f)
defer pprof.StopCPUProfile()
}
if core.Daemon {
LoadConfig()
logs.ReloadLogs()
process.WritePidFile()
// Show our nice welcome logo
fmt.Printf(core.Banner+"\n\n", core.VERSION)
/* Setup RPC */
rpcs := new(rpc.CLI)
if err := rpcs.Start(); err != nil {
log.Fatal(errors.Wrap(err, "rpc error"))
}
/* Connect to the database */
r := database.NewRedis()
r.ConnectPubsub()
rpcs.SetDatabase(r)
c := mirrors.NewCache(r)
rpcs.SetCache(c)
h := http.HTTPServer(r, c)
/* Start the background monitor */
m := daemon.NewMonitor(r, c)
if core.Monitor {
go m.MonitorLoop()
}
/* Handle SIGNALS */
k := make(chan os.Signal, 1)
rpcs.SetSignals(k)
signal.Notify(k,
syscall.SIGINT, // Terminate
syscall.SIGTERM, // Terminate
syscall.SIGQUIT, // Stop gracefully
syscall.SIGHUP, // Reload config
syscall.SIGUSR1, // Reopen log files
syscall.SIGUSR2, // Seamless binary upgrade
)
go func() {
for {
sig := <-k
switch sig {
case syscall.SIGINT:
fallthrough
case syscall.SIGTERM:
process.RemovePidFile()
os.Exit(0)
case syscall.SIGQUIT:
m.Stop()
rpcs.Close()
if h.Listener != nil {
log.Notice("Waiting for running tasks to finish...")
h.Stop(5 * time.Second)
} else {
process.RemovePidFile()
os.Exit(0)
}
case syscall.SIGHUP:
listenAddress := GetConfig().ListenAddress
if err := ReloadConfig(); err != nil {
log.Warningf("SIGHUP Received: %s\n", err)
} else {
log.Notice("SIGHUP Received: Reloading configuration...")
}
if GetConfig().ListenAddress != listenAddress {
h.Restarting = true
h.Stop(1 * time.Second)
}
h.Reload()
case syscall.SIGUSR1:
log.Notice("SIGUSR1 Received: Re-opening logs...")
logs.ReloadLogs()
case syscall.SIGUSR2:
log.Notice("SIGUSR2 Received: Seamless binary upgrade...")
rpcs.Close()
err := process.Relaunch(*h.Listener)
if err != nil {
log.Errorf("Relaunch failed: %s\n", err)
}
}
}
}()
// Recover an existing listener (see process.go)
if l, ppid, err := process.Recover(); err == nil {
h.SetListener(l)
go func() {
time.Sleep(100 * time.Millisecond)
process.KillParent(ppid)
}()
}
/* Finally start the HTTP server */
var err error
for {
err = h.RunServer()
if h.Restarting {
h.Restarting = false
continue
}
// This check is ugly but there's still no way to detect this error by type
if err != nil && strings.Contains(err.Error(), "use of closed network connection") {
// This error is expected during a graceful shutdown
err = nil
}
break
}
log.Debug("Waiting for monitor termination")
m.Wait()
log.Debug("Terminating server")
h.Terminate()
r.Close()
process.RemovePidFile()
if err != nil {
log.Fatal(err)
} else {
log.Notice("Server stopped gracefully.")
}
} else {
args := os.Args[len(os.Args)-core.NArg:]
if err := cli.ParseCommands(args...); err != nil {
fmt.Fprintf(os.Stderr, "%s\n", err)
os.Exit(1)
}
}
os.Exit(0)
}