diff --git a/core/service/serviceconf.go b/core/service/serviceconf.go index 8ad3a960eac5..5511e5fbe70f 100644 --- a/core/service/serviceconf.go +++ b/core/service/serviceconf.go @@ -66,9 +66,6 @@ func (sc ServiceConf) SetUp() error { } trace.StartAgent(sc.Telemetry) proc.Setup(sc.Shutdown) - proc.AddShutdownListener(func() { - trace.StopAgent() - }) if len(sc.MetricsUrl) > 0 { stat.SetReportWriter(stat.NewRemoteWriter(sc.MetricsUrl)) @@ -80,6 +77,11 @@ func (sc ServiceConf) SetUp() error { return nil } +// TearDown tear down the service. +func (sc ServiceConf) TearDown() { + trace.StopAgent() +} + func (sc ServiceConf) initMode() { switch sc.Mode { case DevMode, TestMode, RtMode, PreMode: diff --git a/core/service/serviceconf_test.go b/core/service/serviceconf_test.go index 5e37421de8de..1aef1db1bfdd 100644 --- a/core/service/serviceconf_test.go +++ b/core/service/serviceconf_test.go @@ -20,6 +20,7 @@ func TestServiceConf(t *testing.T) { HealthPath: "/healthz", }, } + defer c.TearDown() c.MustSetUp() } @@ -32,5 +33,6 @@ func TestServiceConfWithMetricsUrl(t *testing.T) { Mode: "dev", MetricsUrl: "http://localhost:8080", } + defer c.TearDown() assert.NoError(t, c.SetUp()) } diff --git a/rest/server.go b/rest/server.go index 3809d876fdb4..42ed8ff89db6 100644 --- a/rest/server.go +++ b/rest/server.go @@ -111,6 +111,7 @@ func (s *Server) StartWithOpts(opts ...StartOption) { // Stop stops the Server. func (s *Server) Stop() { + s.ngin.conf.TearDown() logx.Close() } diff --git a/zrpc/server.go b/zrpc/server.go index 813fc358d298..595e3b6dd5f1 100644 --- a/zrpc/server.go +++ b/zrpc/server.go @@ -15,6 +15,7 @@ import ( // A RpcServer is a rpc server. type RpcServer struct { + conf RpcServerConf server internal.Server register internal.RegisterFn } @@ -57,6 +58,7 @@ func NewServer(c RpcServerConf, register internal.RegisterFn) (*RpcServer, error } rpcServer := &RpcServer{ + conf: c, server: server, register: register, } @@ -94,6 +96,7 @@ func (rs *RpcServer) Start() { // Stop stops the RpcServer. func (rs *RpcServer) Stop() { + rs.conf.TearDown() logx.Close() }