70 lines
1.6 KiB
Go
70 lines
1.6 KiB
Go
package main
|
|
|
|
import "haza"
|
|
import "sync"
|
|
import "os"
|
|
import "os/signal"
|
|
import "syscall"
|
|
|
|
type signal_handler struct {
|
|
svc haza.Service
|
|
}
|
|
|
|
func (sh *signal_handler) RunTask(wg *sync.WaitGroup) {
|
|
var sighup_chan chan os.Signal
|
|
var sigterm_chan chan os.Signal
|
|
var sig os.Signal
|
|
|
|
if wg != nil {
|
|
defer wg.Done()
|
|
}
|
|
|
|
sighup_chan = make(chan os.Signal, 1)
|
|
sigterm_chan = make(chan os.Signal, 1)
|
|
|
|
signal.Notify(sighup_chan, syscall.SIGHUP)
|
|
signal.Notify(sigterm_chan, syscall.SIGTERM, os.Interrupt)
|
|
|
|
chan_loop:
|
|
for {
|
|
select {
|
|
case <-sighup_chan:
|
|
sh.svc.FixServices()
|
|
|
|
case sig = <-sigterm_chan:
|
|
sh.svc.StopServices()
|
|
sh.svc.WriteLog ("", haza.LOG_INFO, "Received %s signal", sig)
|
|
break chan_loop
|
|
}
|
|
}
|
|
|
|
//signal.Reset(syscall.SIGHUP)
|
|
//signal.Reset(syscall.SIGTERM)
|
|
signal.Stop(sighup_chan)
|
|
signal.Stop(sigterm_chan)
|
|
}
|
|
|
|
func (sh *signal_handler) StartService(data interface{}) {
|
|
// this isn't actually used standalone..
|
|
// if we are to implement it, it must use the wait group for signal handler itself
|
|
// however, this service is run through another service.
|
|
// sh.wg.Add(1)
|
|
// go sh.RunTask(&sh.wg)
|
|
}
|
|
|
|
func (sh *signal_handler) StopServices() {
|
|
syscall.Kill(syscall.Getpid(), syscall.SIGTERM) // TODO: find a better to terminate the signal handler...
|
|
}
|
|
|
|
func (sh *signal_handler) FixServices() {
|
|
}
|
|
|
|
func (sh *signal_handler) WaitForTermination() {
|
|
// not implemented. see the comment in StartServices()
|
|
// sh.wg.Wait()
|
|
}
|
|
|
|
func (sh *signal_handler) WriteLog(id string, level haza.LogLevel, fmt string, args ...interface{}) {
|
|
sh.svc.WriteLog(id, level, fmt, args...)
|
|
}
|