shutdown

package module
v0.0.0-...-a8353ff Latest Latest
Warning

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

Go to latest
Published: Dec 30, 2015 License: Apache-2.0 Imports: 5 Imported by: 1

README

shutdown

offer a convenient Go package to intercept Ctrl+C or kill signal and do everything you want before program exit in golang

Installation

To install, simply run in a terminal:

go get github.com/ZGeomantic/shutdown

Usage

The following example (taken from /example/example.go) shows how to add the functions or struct's entity you want to handle after Ctrl+C is pressed and before the program is shutdown.

package main

import (
	"fmt"
	"github.com/ZGeomantic/shutdown"
)

type Foo struct {
	Source string
}

func (this *Foo) OnShutdown() {
	fmt.Println("close anything you need to close here as a struct")
}

func BarFunc() {
	fmt.Println("close anything you need to close here as a function")
}

func main() {
	var foo Foo
	shutdown.Register(&foo)
	shutdown.Register(shutdown.Func(BarFunc))
	shutdown.WaitingShutDown()
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Register

func Register(closer GracefulClose)

func WaitingShutDown

func WaitingShutDown()

对外暴露的阻塞式的等待方法

Types

type Func

type Func func()

如果无法让被关闭的对象实现接口,可以通过类型转换一个func()形式的闭包函数,来简单地实现GracefulClose接口 if the shutdown logic is just a function but not a struct, that's ok. just cast it type to Func. for example:

 func YourCloseProcedure(){
	 your code
}

Call it like this: Register(shutdown.Func(YourCloseProcedure))

func (Func) OnShutdown

func (shutdown Func) OnShutdown()

type GracefulClose

type GracefulClose interface {
	OnShutdown()
}

被关闭者要实现的接口 the only interface that need to be implemented

type ShutdownQueue

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

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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