2024-04-11 11:07:26 +02:00
|
|
|
// Package terminator provides a simple way to await process termination.
|
|
|
|
//
|
|
|
|
// Just wait until the Terminate channel closes:
|
|
|
|
//
|
|
|
|
// <-terminator.Terminate
|
|
|
|
//
|
|
|
|
// it will fall through, once your process receives Ctrl-C, SIGINT, SIGTERM or SIGKILL signal.
|
|
|
|
package terminator
|
2020-05-29 16:35:56 +02:00
|
|
|
|
|
|
|
import (
|
2024-04-11 11:07:26 +02:00
|
|
|
"os"
|
2020-05-29 16:35:56 +02:00
|
|
|
"os/signal"
|
|
|
|
"syscall"
|
|
|
|
)
|
|
|
|
|
2024-04-11 11:07:26 +02:00
|
|
|
// enable to react to
|
|
|
|
//
|
|
|
|
// <-terminator.Terminate
|
|
|
|
//
|
2020-05-29 16:35:56 +02:00
|
|
|
// nicely
|
|
|
|
var Terminate = make(chan struct{})
|
|
|
|
|
|
|
|
func init() {
|
2024-04-11 11:07:26 +02:00
|
|
|
sigs := make(chan os.Signal)
|
|
|
|
signal.Notify(sigs, os.Interrupt, os.Kill)
|
2020-05-29 16:35:56 +02:00
|
|
|
signal.Notify(sigs, syscall.SIGTERM)
|
|
|
|
|
|
|
|
// When notified about SIGINT, SIGTERM, or SIGKILL close the Terminator channel
|
|
|
|
// in order to notify interested parties to quit.
|
|
|
|
go func(c <-chan os.Signal) {
|
|
|
|
<-c
|
2020-05-29 16:37:38 +02:00
|
|
|
close(Terminate)
|
2020-05-29 16:35:56 +02:00
|
|
|
}(sigs)
|
|
|
|
}
|