shutdown

package module
v1.0.4 Latest Latest
Warning

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

Go to latest
Published: Jul 29, 2024 License: MIT Imports: 7 Imported by: 0

README

GoDoc

shutdown

Utility library to handle daemons

Usage

import (
    "net"

    "github.com/KarpelesLab/shutdown"
)

func main() {
    shutdown.SetupSignals()

    // do the things you want to do
    go launchHttp()

    shutdown.Wait()
}

func launchHttp() {
    l, err := net.Listen("tcp", ":80")
    if err != nil {
        shutdown.Fatalf("failed to listen for the http server: %w", err)
        return
    }

    // cleanup of opened listen socket
    shutdown.Defer(func() {
        l.Close()
    })
    // ...
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Defer

func Defer(f func())

Defer will register the given function to be run upon program termination

func Fatalf

func Fatalf(format string, args ...any)

Fatalf reports an error, it is possible to wrap errors with %w

func Go added in v1.0.4

func Go(funcs ...func() error)

Go runs function(s) f in background, and if any of those functions return an error, it'll be handled as a fatal error and cause shutdown to trigger as if Fatalf was called

func SetupSignals

func SetupSignals()

SetupSignals will listen for interruptions (Ctrl-C or kill), and trigger a shutdown if a signal is received. If a second signal is sent (shutdown is taking too long), this will call os.Exit(1)

func Shutdown

func Shutdown()

Shutdown will trigger the normal shutdown of the program

func Wait

func Wait()

Wait will wait until shutdown, or if an error is sent via Fatalf. This function should be called as the last function in main()

Types

This section is empty.

Jump to

Keyboard shortcuts

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