clustvirt/daemon/main.go
Matthew Stobbs 1113062d9e moving some modules to their own thing
- lib/log is now git.staur.ca/stobbsm/simplelog
- router is now git.staur.ca/stobbsm/simpleroute
2024-04-07 00:00:59 -06:00

62 lines
1.3 KiB
Go

package daemon
import (
"context"
"os"
"syscall"
log "git.staur.ca/stobbsm/simplelog"
)
// 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()
}
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
}