shutdown

package module
v0.0.0-...-1239dd1 Latest Latest
Warning

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

Go to latest
Published: Mar 30, 2021 License: MIT-0 Imports: 6 Imported by: 0

README

shutdown

This package offers a simple way to watch OS termination signals and execute cleanup functions.
GoDoc

Install

go get -u github.com/iamken1204/shutdown

Usage

package main

import (
	"fmt"

	"github.com/iamken1204/shutdown"
)

func main() {
	fmt.Println("Starting the app.")

	srv := myServer{}
	s := shutdown.New()

	h := shutdown.NewHook(func() error {
		fmt.Println("executing my hook...")
		return nil
	})
	s.AddHook(
		h,
		shutdown.NewHook(func() error {
			fmt.Println("hook the 2nd is running")
			return nil
		}),
		srv,
	)

	s.Listen()
	fmt.Println("The app has been shut down.")
}

type myServer struct{}

func (myServer) Cleanup() error {
	fmt.Println("terminating myServer")
	return nil
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Hook

type Hook interface {
	// Cleanup executes pre-actions before the program shuts down.
	Cleanup() error
}

Hook handles cleanup function within.

func NewHook

func NewHook(fn func() error) Hook

NewHook offers a convinient way to generate a Hook from a pure function.

type Shutdown

type Shutdown struct {
	Timeout time.Duration
	// contains filtered or unexported fields
}

Shutdown contains cleanup hooks.

func New

func New() *Shutdown

New initializes a Shutdown instance.

func (*Shutdown) AddHook

func (s *Shutdown) AddHook(hooks ...Hook)

AddHook appends given hooks.

func (*Shutdown) Context

func (s *Shutdown) Context() context.Context

Context returns Shutdown's underlying Context.

func (*Shutdown) Listen

func (s *Shutdown) Listen()

Listen blocks the program until received terminating signals, then trigger all hook functions within.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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