shutdown

package module
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Feb 8, 2019 License: MIT Imports: 4 Imported by: 17

README

Golang App Shutdown Hooks

GoDoc Report Coverage Status Build Status

This package provides convenient interface for working with os.Signal.

Multiple hooks can be applied, they will be called simultaneously on app shutdown.

Example

package main

import (
	"log"
	"time"

	"github.com/ztrue/shutdown"
)

func main() {
	shutdown.Add(func() {
		// Write log.
		// Stop writing files.
		// Close connections.
		// Etc.
		log.Println("Stopping...")
		log.Println("3")
		time.Sleep(time.Second)
		log.Println("2")
		time.Sleep(time.Second)
		log.Println("1")
		time.Sleep(time.Second)
		log.Println("0, stopped")
	})

	// App emulation.
	go func() {
		log.Println("App running, press CTRL + C to stop")
		select {}
	}()

	shutdown.Listen()
}

Find more executable examples in examples dir.

How to Use

Import
import "github.com/ztrue/shutdown"
Add Shutdown Hook
shutdown.Add(func() {
	log.Println("Stopping")
})
Remove Hook
key := shutdown.Add(func() {
	log.Println("Stopping")
})

shutdown.Remove(key)
Hook With Custom Key
shutdown.AddWithKey("mykey", func() {
	log.Println("Stopping")
})

shutdown.Remove("mykey")
Hook With Signal Parameter
shutdown.AddWithParam(func(os.Signal) {
	log.Println("Stopping because of", os.Signal)
})
Listen for Specific Signals
shutdown.Listen(syscall.SIGINT, syscall.SIGTERM)

Documentation

Overview

Package shutdown provides convenient interface for working with os.Signal.

Multiple hooks can be applied, they will be called simultaneously on app shutdown.

Index

Constants

This section is empty.

Variables

View Source
var DefaultShutdown = New()

DefaultShutdown is a default instance.

Functions

func Add

func Add(fn func()) string

Add adds a shutdown hook and returns hook identificator (key).

func AddWithKey

func AddWithKey(key string, fn func())

AddWithKey adds a shutdown hook with provided identificator (key).

func AddWithKeyWithParam

func AddWithKeyWithParam(key string, fn func(os.Signal))

AddWithKeyWithParam adds a shutdown hook with signal parameter with provided identificator (key).

func AddWithParam

func AddWithParam(fn func(os.Signal)) string

AddWithParam adds a shutdown hook with signal parameter and returns hook identificator (key).

func Hooks

func Hooks() map[string]func(os.Signal)

Hooks returns a copy of current hooks.

func Listen

func Listen(signals ...os.Signal)

Listen waits for provided OS signals. It will wait for any signal if no signals provided.

func Remove

func Remove(key string)

Remove cancels hook by identificator (key).

func Reset

func Reset()

Reset cancels all hooks.

Types

type Shutdown

type Shutdown struct {
	// contains filtered or unexported fields
}

Shutdown is an instance of shutdown handler.

func New

func New() *Shutdown

New creates a new Shutdown instance.

func (*Shutdown) Add

func (s *Shutdown) Add(fn func()) string

Add adds a shutdown hook and returns hook identificator (key).

func (*Shutdown) AddWithKey

func (s *Shutdown) AddWithKey(key string, fn func())

AddWithKey adds a shutdown hook with provided identificator (key).

func (*Shutdown) AddWithKeyWithParam

func (s *Shutdown) AddWithKeyWithParam(key string, fn func(os.Signal))

AddWithKeyWithParam adds a shutdown hook with signal parameter with provided identificator (key).

func (*Shutdown) AddWithParam

func (s *Shutdown) AddWithParam(fn func(os.Signal)) string

AddWithParam adds a shutdown hook with signal parameter and returns hook identificator (key).

func (*Shutdown) Hooks

func (s *Shutdown) Hooks() map[string]func(os.Signal)

Hooks returns a copy of current hooks.

func (*Shutdown) Listen

func (s *Shutdown) Listen(signals ...os.Signal)

Listen waits for provided OS signals. It will wait for any signal if no signals provided.

func (*Shutdown) Remove

func (s *Shutdown) Remove(key string)

Remove cancels hook by identificator (key).

func (*Shutdown) Reset

func (s *Shutdown) Reset()

Reset cancels all hooks.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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