Code Monkey home page Code Monkey logo

go-exit's Introduction

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")
}

go-exit's People

Contributors

cheggaaa avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.