signal

package
v0.0.0-...-38fe691 Latest Latest
Warning

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

Go to latest
Published: Nov 19, 2015 License: MIT Imports: 4 Imported by: 47

README

Signal

Signal can handle system signals easyly.

Example

package signal_test

import (
	"fmt"
	"os"
	"syscall"
	"time"

	// Import the library
	"github.com/mikespook/golib/signal"
)

func main() {
	signal.Bind(syscall.SIGUSR1, func() uint {
		fmt.Println("SIGUSR1 handler #1")
		return signal.Continue
	})
	signal.Bind(syscall.SIGUSR1, func() uint {
		fmt.Println("SIGUSR1 handler #2")
		return signal.Break
	})

	// Because the previous handler returns `Break` permanently,
	// this handler will never be excuted.
	signal.Bind(syscall.SIGUSR1, func() uint {
		fmt.Println("SIGUSR1 handler #3")
		return signal.Continue
	})

	// Bind and Unbind
	handler := signal.Bind(syscall.SIGUSR1, func() uint {
		fmt.Println("SIGUSR1 handler #4")
		return signal.Continue
	})
	handler.Unbind()
	// Another alternative way is:
	// signal.Unbind(syscall.SIGUSR1, handler.Id)

	signal.Bind(syscall.SIGINT, func() uint { return signal.BreakExit })

	// Stop automatically after 2 minutes
	go func() {
		time.Sleep(time.Second * 120)
		if err := signal.Send(os.Getpid(), os.Interrupt); err != nil {
			fmt.Println(err)
		}
	}()

	// Block here
	s := signal.Wait()
	fmt.Printf("Exit by signal: %s\n", s)
}

Documentation

Index

Constants

View Source
const (
	Break = iota + 1
	BreakExit
	Continue
	ContinueExit
)

Variables

View Source
var (
	Default = New(nil)
)

Functions

func Bind

func Bind(s os.Signal, cb Callback) *handler

func Close

func Close()

func New

func New(id idgen.IdGen) (sh *handlers)

func Send

func Send(pid int, signal os.Signal) error

func Unbind

func Unbind(s os.Signal, id interface{}) bool

func Wait

func Wait() os.Signal

Types

type Callback

type Callback func() uint

Returns Break, BreakExit, Continue and ContinueExit for different purposes

Jump to

Keyboard shortcuts

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