shutdown

package module
v1.3.2 Latest Latest
Warning

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

Go to latest
Published: Feb 8, 2020 License: MIT Imports: 4 Imported by: 0

README

Go package Build Status Go Report Card Codacy Badge Maintainability

Shutdown

It's a package for graceful shutdown your app or process

Install

go get -u github.com/efureev/go-shutdown

Golang app shutdown.

Examples

import "github.com/efureev/go-shutdown"

func main() {
	//..
    
    shutdown.Wait()
}
import "github.com/efureev/go-shutdown"

func main() {
	//..
    
    shutdown.WaitWithLogger(logger, syscall.SIGINT, syscall.SIGTERM)
}
import "github.com/efureev/go-shutdown"

func main() {
	//..
    
    shutdown.
        OnDestroy(func() {
            module.processing.EndJobListen()
        }).
        SetLogger(module.Log()).
        Wait()
}

Documentation

Overview

Package `shutdown` is intended for graceful shutdown for your app/processes.

The simplest way to use `Shutdown`:

package main

import "github.com/efureev/go-shutdown"
func main() {
	//..

    shutdown.Wait()
}

For a full guide visit https://github.com/efureev/go-shutdown

Example (Basic)
package main

import (
	"fmt"
	"os"
	"syscall"
	"time"
)

var fn = func(s os.Signal) {
	time.Sleep(1 * time.Second)
	p, err := os.FindProcess(os.Getpid())

	if err != nil {
		panic(err.Error())
	}
	err = p.Signal(s)
	if err != nil {
		panic(err.Error())
	}
}

func main() {

	go fn(syscall.SIGTERM)
	err := Wait(syscall.SIGTERM)

	fmt.Fprint(os.Stdout, err)

}
Output:

<nil>
Example (With_destroy_func)
logger := new(mockLogger)
go fn(syscall.SIGTERM)

test := ``
err := OnDestroy(func() error {
	test = `test`
	return nil
}).
	SetLogger(logger).
	Wait()

fmt.Fprintln(os.Stdout, logger.Logs[0])
fmt.Fprintln(os.Stdout, logger.Logs[1])
fmt.Fprintln(os.Stdout, err)
fmt.Fprintln(os.Stdout, test)
Output:

shutdown started...
shutdown complete...
<nil>
test
Example (With_logger)
logger := new(mockLogger)
go fn(syscall.SIGTERM)
err := WaitWithLogger(logger, syscall.SIGINT, syscall.SIGTERM)

fmt.Fprintln(os.Stdout, logger.Logs[0])
fmt.Fprintln(os.Stdout, logger.Logs[1])
fmt.Fprintln(os.Stdout, err)
Output:

shutdown started...
shutdown complete...
<nil>

Index

Examples

Constants

This section is empty.

Variables

View Source
var DefaultShutdown = New()

DefaultShutdown is a default instance.

Functions

func End

func End()

End is alias for New().End()

func Wait

func Wait(signals ...os.Signal) error

Wait is alias for New().Wait(...)

func WaitWithLogger

func WaitWithLogger(logger ILogger, signals ...os.Signal) error

WaitWithLogger is alias for New().SetLogger(logger).Wait(...)

Types

type ILogger

type ILogger interface {
	Trace(args ...interface{})
	Info(args ...interface{})
}

ILogger interface

type Shutdown

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

Shutdown struct

func New

func New() *Shutdown

New creates new instance of Shutdown

func OnDestroy

func OnDestroy(fn func() error) *Shutdown

OnDestroy is alias for New().OnDestroy(fn)

func (*Shutdown) End

func (s *Shutdown) End()

End send signal for shutting down

func (*Shutdown) OnDestroy

func (s *Shutdown) OnDestroy(fn func() error) *Shutdown

OnDestroy apply user-function to execute on app/service terminating

func (*Shutdown) SetLogger

func (s *Shutdown) SetLogger(l ILogger) *Shutdown

SetLogger set instance logger

func (*Shutdown) Wait

func (s *Shutdown) Wait(signals ...os.Signal) (err error)

Wait waiting for signal

Jump to

Keyboard shortcuts

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