Documentation
¶
Overview ¶
Package sigmon simplifies os.Signal handling.
The benefits of this over a more simplistic approach are eased signal bypassability, eased signal handling replaceability, the rectification of system signal quirks, and operating system portability (Windows will ignore USR1 and USR2 signals, and some testing is bypassed).
Example ¶
sm := sigmon.New(nil) sm.Start() // Do things which cannot be affected by OS signals other than SIGKILL... sm.Set(handle) // Do things which can be affected by handled OS signals... sm.Stop() // OS signals will be handled normally.
Example (Elaborated) ¶
sm := sigmon.New(nil) sm.Start() db := newDataBase(creds) db.Migrate() app := newWebApp(db) app.ListenAndServe() sm.Set(func(s *sigmon.State) { switch s.Signal() { case sigmon.SIGHUP: app.Restart() default: app.Shutdown() } }) app.Wait()
Example (HandlerFunc) ¶
handle = func(s *sigmon.State) { switch s.Signal() { case sigmon.SIGHUP: server.Restart() default: server.Shutdown() } }
Example (HandlerFuncSyscall) ¶
handle = func(s *sigmon.State) { switch syscall.Signal(s.Signal()) { case syscall.SIGHUP: server.Restart() default: server.Shutdown() } }
Index ¶
Examples ¶
Constants ¶
const ( SIGHUP = Signal(syscall.SIGHUP) SIGINT = Signal(syscall.SIGINT) SIGTERM = Signal(syscall.SIGTERM) SIGUSR1 = Signal(syscall.SIGUSR1) SIGUSR2 = Signal(syscall.SIGUSR2) )
Signal constants define handled system signals.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type HandlerFunc ¶
type HandlerFunc func(*State)
HandlerFunc is used to communicate system signal handling behavior.
type SignalMonitor ¶
SignalMonitor helps manage system signal handling.
func New ¶
func New(fn HandlerFunc) *SignalMonitor
New allocates a new SignalMonitor and related helper types, and returns the SignalMonitor. When a nil arg is provided, no action will be taken during signal handling. Start must be called in order to begin intercepting or ignoring system signals.
func (*SignalMonitor) Set ¶
func (m *SignalMonitor) Set(fn HandlerFunc)
Set stores a HandlerFunc to be called when a system signal is received. If a nil arg is provided, no action will be taken during signal handling. Only the most recently set HandlerFunc will be used.
func (*SignalMonitor) Start ¶
func (m *SignalMonitor) Start()
Start establishes system signal awareness.
func (*SignalMonitor) Stop ¶
func (m *SignalMonitor) Stop()
Stop disestablishes system signal awareness. Subsequently called system signals are handled normally.