Skip to content
This repository has been archived by the owner on May 6, 2024. It is now read-only.

kilianpaquier/exithandler

Repository files navigation

exithandler

Archive note: Just copy/paste the related code listening to context signal 😉.

GitHub Actions GitHub Release GitHub Issues GitHub License Coverage Go Version Go Report Card


How to use ?

go get -u github.com/kilianpaquier/exithandler@latest

Features

The exithandler package exposes two useful functions to handle program terminations:

The first one is Handle which will blocked on SIGINT and SIGTERM signals until one of those are sent and then executes the provided function.

func main() {
    // some things to be defined

    go exithandler.Handle(ctx, func(context.Context) {
        // some things to close or execute when the program terminates
    })
}

The second one is HandleFunc which does the exact same thing, the only difference is that it returns the function which will wait and does not wait directly (as provided in below example).

func main() {
    // some things to be defined

    exithandler := exithandler.HandleFunc(ctx, func(context.Context) {
        // some things to close or execute when the program terminates
    })

    // other things to do

    exithandler()
}