update signal handler

This commit is contained in:
Aliberk Sandıkçı 2023-07-31 16:34:01 +03:00
parent c91ea933cd
commit 013d59c31e
Signed by: asandikci
GPG key ID: 25C67A03B5666BC1
2 changed files with 16 additions and 20 deletions

View file

@ -3,6 +3,8 @@ package main
import (
"godaemon1/service"
"log"
"os"
"os/signal"
"time"
"github.com/sevlyar/go-daemon"
@ -27,7 +29,16 @@ func main() {
}
defer cntxt.Release()
service.SigHandler()
// TODO make both in external package service these lines but also async
sigChannel := make(chan os.Signal, 1)
signal.Notify(sigChannel)
go func() {
for {
s := <-sigChannel
service.SigHandler(s)
}
}()
log.Print("- - - - - - - - - - - - - - -")
log.Print("daemon started")

View file

@ -3,32 +3,17 @@ package service
import (
"log"
"os"
"os/signal"
"syscall"
)
func handler(signal os.Signal) {
func SigHandler(signal os.Signal) {
if signal == syscall.SIGTERM {
log.Println("Got signal: SIGTERM")
} else if signal == syscall.SIGINT {
log.Println("Got signal: SIGINT")
} else if signal == syscall.SIGKILL {
log.Println("Got signal: SIGINT")
} else {
log.Println("Got another signal")
log.Printf("Got another signal: %v\n", signal)
}
}
func SigHandler() {
sigChannel := make(chan os.Signal, 1)
signal.Notify(sigChannel)
exitChannel := make(chan int)
go func() {
for {
s := <-sigChannel
handler(s)
}
}()
exitcode := <-exitChannel
os.Exit(exitcode)
}