/
signal.go
63 lines (50 loc) · 1.7 KB
/
signal.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
/*
Copyright 2021 G. Benjamin Morrison
This file is part of getwtxt-ng.
getwtxt-ng is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
getwtxt-ng is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with getwtxt-ng. If not, see <https://www.gnu.org/licenses/>.
*/
package main
import (
"os"
"os/signal"
"syscall"
log "github.com/sirupsen/logrus"
)
func signalWatcher(conf *Config, tickerExit chan<- struct{}, logger *log.Logger) {
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt, syscall.SIGHUP)
go func() {
for sig := range c {
switch sig {
case syscall.SIGINT:
conf.mu.Lock()
logger.Infof("Caught %s", sig)
logger.Info("Shutting down sync ticker")
tickerExit <- struct{}{}
logger.Info("Closing log files and switching to stderr")
logger.SetOutput(os.Stderr)
if err := conf.ServerConfig.MessageLogFd.Close(); err != nil {
logger.Infof("When closing message log: %s\n", err)
}
if err := conf.ServerConfig.RequestLogFd.Close(); err != nil {
logger.Infof("When closing request log: %s\n", err)
}
os.Exit(130)
case syscall.SIGHUP:
logger.Infof("Caught %s: reloading configuration...\n", sig)
if err := conf.reload(*flagConfig, logger); err != nil {
logger.Infof(err.Error())
}
}
}
}()
}