sigmon

package module
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 25, 2018 License: MIT Imports: 4 Imported by: 3

README

sigmon

go get github.com/codemodus/sigmon

Package sigmon simplifies os.Signal handling.

Usage

type Signal
type SignalMonitor
    func New(handler func(*SignalMonitor)) (s *SignalMonitor)
    func (s *SignalMonitor) Run()
    func (s *SignalMonitor) Set(handler func(*SignalMonitor))
    func (s *SignalMonitor) Sig() Signal
    func (s *SignalMonitor) Stop()
Setup
import (
    "github.com/codemodus/sigmon"
)

func main() {
    sm := sigmon.New(nil)
    sm.Run()
    // Do things which cannot be affected by OS signals...

    sm.Set(signalHandler)
    // Do things which can be affected by OS signals...

    sm.Set(nil)
    // Do more things which cannot be affected by OS signals...

    sm.Stop()
    // OS signals will be handled normally.
}
Signal Handler
func signalHandler(sm *sigmon.SignalMonitor) {
    switch sm.Sig() {
    case sigmon.SIGHUP:
        // Reload
    case sigmon.SIGINT, sigmon.SIGTERM:
        // Stop
    case sigmon.SIGUSR1, sigmon.SIGUSR2:
        // More
    }
}
Signal Handler With Context
func main() {
    sigCtx := &signalContext{id: 123}

    // The setOutput method is ran on any signal and will store the signal text.
    sm := sigmon.New(sigCtx.setOutput)
    sm.Run()

    // Simulate system signal call (windows does not support self-signaling).
    if err := callOSSignal(syscall.SIGINT); err != nil {
        fmt.Fprintln(os.Stderr, err)
    }

    sm.Stop()

    // The output method returns the called signal text and sigCtx.id value.
    fmt.Println(sigCtx.output()) // Outputs: "INT 123"
}

More Info

Windows Compatibility

sigmon will run on Windows systems without error. In order for this to be, notifications of USR1 and USR2 signals are detented as they are not supported whatsoever in Windows. All tests work on *nix systems, but are not run on Windows. It is up to the user to assess whether their application is receiving INT, TERM, and, HUP signals properly along with what that may mean for the design of the affected system.

Documentation

View the GoDoc

Benchmarks

N/A

Documentation

Overview

Package sigmon simplifies os.Signal handling.

Example
package main

import (
	"github.com/codemodus/sigmon"
)

func main() {
	sm := sigmon.New(nil)
	sm.Run()
	// Do things which cannot be affected by OS signals...

	sm.Set(signalHandler)
	// Do things which can be affected by OS signals...

	sm.Set(nil)
	// Do more things which cannot be affected by OS signals...

	sm.Stop()
	// OS signals will be handled normally.
}

func signalHandler(sm *sigmon.SignalMonitor) {

}
Example (PassingContext)
sigCtx := &signalContext{id: 123}

// The setOutput method stores the signal type when any signal is handled.
sm := sigmon.New(sigCtx.setOutput)
sm.Run()

// Simulate a system signal (windows does not support self-signaling).
if err := callOSSignal(syscall.SIGINT); err != nil {
	fmt.Fprintln(os.Stderr, err)
}

sm.Stop()

// The output method returns the called signal type and sigCtx.id value.
fmt.Println(sigCtx.output())
Output:

INT 123

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Signal

type Signal string

Signal wraps the string type to reduce confusion when checking Sig.

const (
	SIGHUP  Signal = "HUP"
	SIGINT  Signal = "INT"
	SIGTERM Signal = "TERM"
	SIGUSR1 Signal = "USR1"
	SIGUSR2 Signal = "USR2"
)

Signal constants are string representations of handled os.Signals.

type SignalMonitor

type SignalMonitor struct {
	sync.Mutex
	// contains filtered or unexported fields
}

SignalMonitor helps manage signal handling.

func New

func New(handler func(*SignalMonitor)) (s *SignalMonitor)

New takes a function and returns a SignalMonitor. When a nil arg is provided, no action will be taken during signal handling. Run must be called in order to begin handling.

func (*SignalMonitor) Run

func (m *SignalMonitor) Run()

Run starts signal handling.

func (*SignalMonitor) Set

func (m *SignalMonitor) Set(handler func(*SignalMonitor))

Set allows the handler function to be added or removed. If no function has been provided, no action will be taken during signal handling. Only the most recently passed function holds any effect.

func (*SignalMonitor) Sig

func (m *SignalMonitor) Sig() Signal

Sig returns a typed string (Signal) representing the most recently called os.Signal.

func (*SignalMonitor) Stop

func (m *SignalMonitor) Stop()

Stop discontinues all os.Signal handling.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL