itzloop / gograce

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Gograce

gograce let's you run your programs gracefully managing signal handling, cleanup timeouts and force quit for you.

Red Sus

GitHub Workflow Status (with event) GitHub License Go Report Card PkgGoDev

Usage

package main

import (
    "context"
    "github.com/itzloop/gograce"
    "time"
)

func main() {
    grace := gograce.NewGraceful(gograce.Options{
        // Timeout sets a hard deadline for cleanup phase. If time out is specified, 
        // gograce will wait for that amount and then terminates forcefully
        Timeout:       10 * time.Second,

        // This controls whether or not sending terminate signal twice will forcefully
        // terminate the application
        NoForceQuit:   false,

        // Setting this will limit the number of go-routines running at the same time.
        MaxGoRoutines: 0,

        // Setting this will overwrite the default signals
        Signals:       nil,
    })

    app := App{}
    grace.GoWithContext(app.Start)
    grace.GoWithContext(app.Close)
    grace.FatalWait()
}

type App struct{}

func (app *App) Start(ctx context.Context) error {
    // run stuff
}


func (app *App) Close(ctx context.Context) error {
    <-ctx.Done()
    // do cleanup
}

For more information on how to use it refer to examples readme.

Testing

$ go test -v ./...

Contributing

TODO

About

License:MIT License


Languages

Language:Go 100.0%