feat: catch Interrupt and SIGTERM signals to ensure plugin quitted

This commit is contained in:
debugtalk
2022-01-18 17:33:26 +08:00
parent 9db54544d9
commit c222b22132
2 changed files with 16 additions and 0 deletions
+1
View File
@@ -137,6 +137,7 @@ func (p *HashicorpPlugin) Call(funcName string, args ...interface{}) (interface{
func (p *HashicorpPlugin) Quit() error { func (p *HashicorpPlugin) Quit() error {
// kill hashicorp plugin process // kill hashicorp plugin process
log.Warn().Msg("quit hashicorp plugin process")
pluginHost.Quit() pluginHost.Quit()
return nil return nil
} }
+15
View File
@@ -9,8 +9,11 @@ import (
"net/http" "net/http"
"net/http/httputil" "net/http/httputil"
"net/url" "net/url"
"os"
"os/signal"
"strconv" "strconv"
"strings" "strings"
"syscall"
"testing" "testing"
"time" "time"
@@ -213,6 +216,18 @@ func initPlugin(path string) (plugin common.Plugin, err error) {
go ga.SendEvent(event) go ga.SendEvent(event)
}() }()
plugin, err = common.Init(path) plugin, err = common.Init(path)
// catch Interrupt and SIGTERM signals to ensure plugin quitted
c := make(chan os.Signal)
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
go func() {
<-c
if plugin != nil {
plugin.Quit()
}
os.Exit(1)
}()
return return
} }