| 1 |
// Copyright (c) 2014-2019 Ludovic Fauvet |
| 2 |
// Licensed under the MIT license |
| 3 |
|
| 4 |
package main |
| 5 |
|
| 6 |
import ( |
| 7 |
"fmt" |
| 8 |
"os" |
| 9 |
"os/signal" |
| 10 |
"runtime/pprof" |
| 11 |
"strings" |
| 12 |
"syscall" |
| 13 |
"time" |
| 14 |
|
| 15 |
"github.com/etix/mirrorbits/cli" |
| 16 |
. "github.com/etix/mirrorbits/config" |
| 17 |
"github.com/etix/mirrorbits/core" |
| 18 |
"github.com/etix/mirrorbits/daemon" |
| 19 |
"github.com/etix/mirrorbits/database" |
| 20 |
"github.com/etix/mirrorbits/http" |
| 21 |
"github.com/etix/mirrorbits/logs" |
| 22 |
"github.com/etix/mirrorbits/mirrors" |
| 23 |
"github.com/etix/mirrorbits/process" |
| 24 |
"github.com/etix/mirrorbits/rpc" |
| 25 |
"github.com/op/go-logging" |
| 26 |
) |
| 27 |
|
| 28 |
var ( |
| 29 |
log = logging.MustGetLogger("main") |
| 30 |
) |
| 31 |
|
| 32 |
func main() { |
| 33 |
core.Parseflags() |
| 34 |
|
| 35 |
if core.CpuProfile != "" { |
| 36 |
f, err := os.Create(core.CpuProfile) |
| 37 |
if err != nil { |
| 38 |
log.Fatal(err) |
| 39 |
} |
| 40 |
defer f.Close() |
| 41 |
pprof.StartCPUProfile(f) |
| 42 |
defer pprof.StopCPUProfile() |
| 43 |
} |
| 44 |
|
| 45 |
if core.Daemon { |
| 46 |
LoadConfig() |
| 47 |
logs.ReloadLogs() |
| 48 |
|
| 49 |
process.WritePidFile() |
| 50 |
|
| 51 |
// Show our nice welcome logo |
| 52 |
fmt.Printf(core.Banner+"\n\n", core.VERSION) |
| 53 |
|
| 54 |
/* Setup RPC */ |
| 55 |
rpcs := new(rpc.CLI) |
| 56 |
if err := rpcs.Start(); err != nil { |
| 57 |
log.Fatal(fmt.Errorf("rpc error: %w", err)) |
| 58 |
} |
| 59 |
|
| 60 |
/* Connect to the database */ |
| 61 |
r := database.NewRedis() |
| 62 |
r.ConnectPubsub() |
| 63 |
rpcs.SetDatabase(r) |
| 64 |
c := mirrors.NewCache(r) |
| 65 |
rpcs.SetCache(c) |
| 66 |
h := http.HTTPServer(r, c) |
| 67 |
|
| 68 |
/* Start the background monitor */ |
| 69 |
m := daemon.NewMonitor(r, c) |
| 70 |
if core.Monitor { |
| 71 |
go m.MonitorLoop() |
| 72 |
} |
| 73 |
|
| 74 |
/* Handle SIGNALS */ |
| 75 |
k := make(chan os.Signal, 1) |
| 76 |
rpcs.SetSignals(k) |
| 77 |
signal.Notify(k, |
| 78 |
syscall.SIGINT, // Terminate |
| 79 |
syscall.SIGTERM, // Terminate |
| 80 |
syscall.SIGQUIT, // Stop gracefully |
| 81 |
syscall.SIGHUP, // Reload config |
| 82 |
syscall.SIGUSR1, // Reopen log files |
| 83 |
syscall.SIGUSR2, // Seamless binary upgrade |
| 84 |
) |
| 85 |
go func() { |
| 86 |
for { |
| 87 |
sig := <-k |
| 88 |
switch sig { |
| 89 |
case syscall.SIGINT: |
| 90 |
fallthrough |
| 91 |
case syscall.SIGTERM: |
| 92 |
process.RemovePidFile() |
| 93 |
os.Exit(0) |
| 94 |
case syscall.SIGQUIT: |
| 95 |
m.Stop() |
| 96 |
rpcs.Close() |
| 97 |
if h.Listener != nil { |
| 98 |
log.Notice("Waiting for running tasks to finish...") |
| 99 |
h.Stop(5 * time.Second) |
| 100 |
} else { |
| 101 |
process.RemovePidFile() |
| 102 |
os.Exit(0) |
| 103 |
} |
| 104 |
case syscall.SIGHUP: |
| 105 |
listenAddress := GetConfig().ListenAddress |
| 106 |
if err := ReloadConfig(); err != nil { |
| 107 |
log.Warningf("SIGHUP Received: %s\n", err) |
| 108 |
} else { |
| 109 |
log.Notice("SIGHUP Received: Reloading configuration...") |
| 110 |
} |
| 111 |
if GetConfig().ListenAddress != listenAddress { |
| 112 |
h.Restarting = true |
| 113 |
h.Stop(1 * time.Second) |
| 114 |
} |
| 115 |
h.Reload() |
| 116 |
logs.ReloadLogs() |
| 117 |
case syscall.SIGUSR1: |
| 118 |
log.Notice("SIGUSR1 Received: Re-opening logs...") |
| 119 |
logs.ReloadLogs() |
| 120 |
case syscall.SIGUSR2: |
| 121 |
log.Notice("SIGUSR2 Received: Seamless binary upgrade...") |
| 122 |
rpcs.Close() |
| 123 |
err := process.Relaunch(*h.Listener) |
| 124 |
if err != nil { |
| 125 |
log.Errorf("Relaunch failed: %s\n", err) |
| 126 |
} |
| 127 |
} |
| 128 |
} |
| 129 |
}() |
| 130 |
|
| 131 |
// Recover an existing listener (see process.go) |
| 132 |
if l, ppid, err := process.Recover(); err == nil { |
| 133 |
h.SetListener(l) |
| 134 |
go func() { |
| 135 |
time.Sleep(100 * time.Millisecond) |
| 136 |
process.KillParent(ppid) |
| 137 |
}() |
| 138 |
} |
| 139 |
|
| 140 |
/* Finally start the HTTP server */ |
| 141 |
var err error |
| 142 |
for { |
| 143 |
err = h.RunServer() |
| 144 |
if h.Restarting { |
| 145 |
h.Restarting = false |
| 146 |
continue |
| 147 |
} |
| 148 |
// This check is ugly but there's still no way to detect this error by type |
| 149 |
if err != nil && strings.Contains(err.Error(), "use of closed network connection") { |
| 150 |
// This error is expected during a graceful shutdown |
| 151 |
err = nil |
| 152 |
} |
| 153 |
break |
| 154 |
} |
| 155 |
|
| 156 |
log.Debug("Waiting for monitor termination") |
| 157 |
m.Wait() |
| 158 |
|
| 159 |
log.Debug("Terminating server") |
| 160 |
h.Terminate() |
| 161 |
|
| 162 |
r.Close() |
| 163 |
|
| 164 |
process.RemovePidFile() |
| 165 |
|
| 166 |
if err != nil { |
| 167 |
log.Fatal(err) |
| 168 |
} else { |
| 169 |
log.Notice("Server stopped gracefully.") |
| 170 |
} |
| 171 |
} else { |
| 172 |
args := os.Args[len(os.Args)-core.NArg:] |
| 173 |
if err := cli.ParseCommands(args...); err != nil { |
| 174 |
fmt.Fprintf(os.Stderr, "%s\n", err) |
| 175 |
os.Exit(1) |
| 176 |
} |
| 177 |
} |
| 178 |
os.Exit(0) |
| 179 |
} |