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 }