Skip to content

Instantly share code, notes, and snippets.

@Solution
Created December 9, 2018 12:58
Show Gist options
  • Save Solution/8c59675e7ac64520a86b06bc2d89b949 to your computer and use it in GitHub Desktop.
Save Solution/8c59675e7ac64520a86b06bc2d89b949 to your computer and use it in GitHub Desktop.
Simple grupttor example
package main
import (
"fmt"
"github.com/PragGoLabs/grupttor"
"github.com/PragGoLabs/grupttor/handlers"
"github.com/PragGoLabs/grupttor/hooks"
"os"
"syscall"
"time"
)
func main() {
// you init interruptor, called gruptor :-)
interruptor := grupttor.NewGrupttor(
// pass handle wrap
handlers.NewWrapHandler(
// the interrupt handler, which is called first
// when the interruption is passed
func(interrupter *grupttor.Grupttor) error {
fmt.Println("Received interrupt signal")
time.Sleep(4*time.Second)
return interrupter.Stop()
},
// the stop handler
// run based by interrupt handler
// you've to call when you have done everything
func(interrupter *grupttor.Grupttor) error {
// exit application
os.Exit(0)
return nil
},
),
[]grupttor.Hook{},
)
// add the hook
// this is on of the basic hooks, system interrupt, based by system calls
// you can specify concrete signals
interruptor.AddHook(hooks.NewSystemInterruptHook([]os.Signal{syscall.SIGKILL, syscall.SIGTERM}))
// and start goroutine on backend, which will wait for signals and handle all the logic
go interruptor.StartAndWait()
// infinite loop, just for example
for {
time.Sleep(2*time.Second)
fmt.Println("Running loop and waiting for kill or sigterm")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment