shutdown

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jun 19, 2019 License: MIT Imports: 3 Imported by: 10

README

Vardius - shutdown

Build Status Go Report Card codecov license

shutdown - Simple shutdown signals handler with callback

ABOUT

Contributors:

Want to contribute ? Feel free to send pull requests!

Have problems, bugs, feature ideas? We are using the github issue tracker to manage them.

HOW TO USE

  1. GoDoc
  2. Examples

Basic example

package main

import (
    "context"
    "net/http"
    "os"
    "time"
    "log"

    "github.com/vardius/shutdown"
)

func main() {
    ctx := context.Background()
    
    http.HandleFunc("/", func(w http.ResponseWriter, _ *http.Request) {
        io.WriteString(w, "Hello!\n")
    })

    stop := func() {
        ctx, cancel := context.WithTimeout(ctx, 5*time.Second)
        defer cancel()

        log.Printf("shutting down...\n")

        if err := srv.Shutdown(ctx); err != nil {
            log.Printf("shutdown error: %v\n", err)
        } else {
            log.Printf("gracefully stopped\n")
        }
    }

    go func() {
        log.Printf("%v\n", http.ListenAndServe(":8080", nil))
        stop()
        os.Exit(1)
    }()
}

License

This package is released under the MIT license. See the complete license in the package:

LICENSE

Documentation

Overview

Package shutdown provides simple shutdown signals handler with callback

Take the following example: First file `main.go` simply gets the repository from the container and prints it we use **MustInvoke** method to simply present the way where we keep type safety

package main

import (
	"context"
	"net/http"
	"os"
	"time"
	"log"

	"github.com/vardius/shutdown"
)

func main() {
	ctx := context.Background()

	http.HandleFunc("/", func(w http.ResponseWriter, _ *http.Request) {
		io.WriteString(w, "Hello!\n")
	})

	stop := func() {
		ctx, cancel := context.WithTimeout(ctx, 5*time.Second)
		defer cancel()

		log.Printf("shutting down...\n")

		if err := srv.Shutdown(ctx); err != nil {
			log.Printf("shutdown error: %v\n", err)
		} else {
			log.Printf("gracefully stopped\n")
		}
	}

	go func() {
		log.Printf("%v\n", http.ListenAndServe(":8080", nil))
		stop()
		os.Exit(1)
	}()
}

Package shutdown provides simple shutdown signals handler with callback handler

Example
package main

import (
	"fmt"
	"syscall"
	"time"

	"github.com/vardius/shutdown"
)

func main() {
	// mock shutdown signall Ctrl + C
	go func() {
		time.Sleep(10 * time.Millisecond)
		syscall.Kill(syscall.Getpid(), syscall.SIGINT)
	}()

	shutdown.GracefulStop(func() {
		fmt.Println("shutdown")
	})

}
Output:

shutdown

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func GracefulStop

func GracefulStop(stop func())

GracefulStop handles signal and graceful shutdown

Types

This section is empty.

Jump to

Keyboard shortcuts

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