exit

package module
v1.0.5 Latest Latest
Warning

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

Go to latest
Published: Jun 30, 2019 License: BSD-2-Clause Imports: 7 Imported by: 0

README

This package provide handling exit signals (SIGKILL, SIGTERM, SIGQUIT and Interrupt)

It's not difficult to write, just dead simple to use)

Installation

go get -u github.com/cheggaaa/go-exit

Simple example
package main

import (
	"fmt"
	
	"github.com/cheggaaa/go-exit"
)

func main() {
	// start app in other goroutine
	go myAppStart()

	// wait stop signal
	sig := exit.Wait()
	fmt.Printf("'%v' recieved\n", sig)

	// and do app exit
	myAppStop()
}

// start your app here
func myAppStart() {
	fmt.Println("my app start")
	fmt.Println("send 'kill pid' or press Ctrl+C for exit")
}

func myAppStop() {
	// Do something for exit 
	fmt.Println("my app stop")
}

Advanced example
package main

import (
	"fmt"
	"os"
	"syscall"
	"time"
	
	"github.com/cheggaaa/go-exit"
)

func main() {
	// we can enable http profiling ("net/http/pprof")
	if e := exit.EnableHTTPProfiling(""); e != nil {
		fmt.Println("Oops can't enable pprof:", e)
	}
	
	// register callbacks (optionally)
	exit.On(myCallback)

	// start app in other goroutine
	go myAppStart()

	// wait stop signal
	sig := exit.Wait()
	fmt.Printf("'%v' recieved\n", sig)

	// we can determine reason for exit
	switch sig.(type) {
	case os.Signal:
		switch sig.(os.Signal) {
		case os.Interrupt:
			fmt.Println("'ctrl+c' or 'kill -2'")
		case syscall.SIGTERM:
			fmt.Println("maybe kill")
		default:
			fmt.Printf("os.Signal: '%v'\n", sig)
		}
	case string:
		fmt.Println("exit.Exit() was called")
	}

	// and do app exit
	myAppStop()
}

func myAppStart() {
	fmt.Println("my app start")
	fmt.Println("send 'kill pid' or press Ctrl+C for exit")
	// wait 30 seconds and do exit
	time.Sleep(time.Second * 30)
	exit.Exit("Work done!")
}

func myCallback() {
	fmt.Println("my callback")
}

func myAppStop() {
	// Do something for exit 
	fmt.Println("my app stop")
}

Documentation

Overview

Package exit - small and simple helper for handling exit signals (SIGKILL, SIGTERM, SIGQUIT and Interrupt)

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func EnableHTTPProfiling

func EnableHTTPProfiling(addr string) error

EnableHTTPProfiling helps easy wake up built in http profiler

func Exit

func Exit(message string)

Exit send close signal with given message

func On

func On(f func())

On adds exit callback callbacks will be executed before exit

func Wait

func Wait() interface{}

Wait the signals for exit Return catched signal

Types

This section is empty.

Jump to

Keyboard shortcuts

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