2024-03-30 02:05:51 +00:00
|
|
|
package daemon
|
|
|
|
|
|
|
|
import (
|
2024-03-31 10:17:00 +00:00
|
|
|
"context"
|
2024-03-30 02:05:51 +00:00
|
|
|
"os"
|
2024-03-31 10:17:00 +00:00
|
|
|
"syscall"
|
2024-03-30 02:05:51 +00:00
|
|
|
|
2024-04-07 06:00:59 +00:00
|
|
|
log "git.staur.ca/stobbsm/simplelog"
|
2024-03-30 02:05:51 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// path to store the PID, configurable
|
|
|
|
var RunPath string
|
|
|
|
var PIDFile string
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
// set the RunPath to store the PID
|
|
|
|
RunPath = `/run/`
|
|
|
|
PIDFile = RunPath + `clustvirt.pid`
|
|
|
|
}
|
|
|
|
|
|
|
|
func pid() int {
|
|
|
|
return os.Getpid()
|
|
|
|
}
|
2024-03-31 10:17:00 +00:00
|
|
|
|
|
|
|
func sigHandler(c <-chan os.Signal) context.Context {
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
sig := <-c
|
|
|
|
switch sig {
|
|
|
|
case syscall.SIGTERM:
|
|
|
|
log.Info("daemon.sigHandler").
|
|
|
|
Str("signal", "SIGTERM").
|
|
|
|
Msg("stopping clustvirt gracefully")
|
|
|
|
cancel()
|
|
|
|
<-ctx.Done()
|
|
|
|
os.Exit(0)
|
|
|
|
case syscall.SIGKILL:
|
|
|
|
log.Warn("daemon.sigHandler").
|
|
|
|
Str("signal", "SIGKILL").
|
|
|
|
Msg("killed. avenge me...")
|
|
|
|
os.Exit(1)
|
|
|
|
case syscall.SIGUSR1:
|
|
|
|
log.Info("daemon.sigHandler").
|
|
|
|
Str("signal", "SIGUSR1").
|
|
|
|
Msg("reloading configuration")
|
|
|
|
// TODO: Reload the application configuration here
|
|
|
|
case syscall.SIGUSR2:
|
|
|
|
log.Info("daemon.sigHandler").
|
|
|
|
Str("signal", "SIGUSR2").
|
|
|
|
Msg("restarting...")
|
|
|
|
// TODO: implement restart logic here
|
|
|
|
default:
|
|
|
|
log.Warn("daemon.sigHandler").
|
|
|
|
Str("signal", sig.String()).
|
|
|
|
Msg("unhandled signal recieved")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
return ctx
|
|
|
|
}
|