From 142bc13c948491fa17641605034ee1f9e0b73258 Mon Sep 17 00:00:00 2001 From: debugtalk Date: Tue, 18 Jan 2022 17:33:26 +0800 Subject: [PATCH] feat: catch Interrupt and SIGTERM signals to ensure plugin quitted --- plugin/common/init.go | 1 + runner.go | 15 +++++++++++++++ 2 files changed, 16 insertions(+) diff --git a/plugin/common/init.go b/plugin/common/init.go index 931668f8..399d38da 100644 --- a/plugin/common/init.go +++ b/plugin/common/init.go @@ -137,6 +137,7 @@ func (p *HashicorpPlugin) Call(funcName string, args ...interface{}) (interface{ func (p *HashicorpPlugin) Quit() error { // kill hashicorp plugin process + log.Warn().Msg("quit hashicorp plugin process") pluginHost.Quit() return nil } diff --git a/runner.go b/runner.go index f8f20899..a28f28a2 100644 --- a/runner.go +++ b/runner.go @@ -9,8 +9,11 @@ import ( "net/http" "net/http/httputil" "net/url" + "os" + "os/signal" "strconv" "strings" + "syscall" "testing" "time" @@ -213,6 +216,18 @@ func initPlugin(path string) (plugin common.Plugin, err error) { go ga.SendEvent(event) }() 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 }