From 75a54e85dc64e2ba82906ab763948ca06c34fcbe Mon Sep 17 00:00:00 2001 From: bergquist Date: Mon, 25 Dec 2017 13:48:50 +0100 Subject: [PATCH] dont spawn new subprocess while shutting down --- pkg/cmd/grafana-server/server.go | 2 +- pkg/plugins/dashboard_importer_test.go | 3 ++- pkg/plugins/dashboards_test.go | 3 ++- pkg/plugins/datasource_plugin.go | 11 ++++++++--- pkg/plugins/models.go | 2 -- pkg/plugins/plugins.go | 8 ++++---- pkg/plugins/plugins_test.go | 5 +++-- 7 files changed, 20 insertions(+), 14 deletions(-) diff --git a/pkg/cmd/grafana-server/server.go b/pkg/cmd/grafana-server/server.go index dd14e33f26e..8ed3196e4ad 100644 --- a/pkg/cmd/grafana-server/server.go +++ b/pkg/cmd/grafana-server/server.go @@ -63,7 +63,7 @@ func (g *GrafanaServerImpl) Start() error { login.Init() social.NewOAuthService() - pluginManager, err := plugins.NewPluginManager() + pluginManager, err := plugins.NewPluginManager(g.context) if err != nil { return fmt.Errorf("Failed to start plugins. error: %v", err) } diff --git a/pkg/plugins/dashboard_importer_test.go b/pkg/plugins/dashboard_importer_test.go index 78df94309f8..3094e6f6d52 100644 --- a/pkg/plugins/dashboard_importer_test.go +++ b/pkg/plugins/dashboard_importer_test.go @@ -1,6 +1,7 @@ package plugins import ( + "context" "io/ioutil" "testing" @@ -91,7 +92,7 @@ func pluginScenario(desc string, t *testing.T, fn func()) { setting.Cfg = ini.Empty() sec, _ := setting.Cfg.NewSection("plugin.test-app") sec.NewKey("path", "../../tests/test-app") - err := Init() + err := Init(context.TODO()) So(err, ShouldBeNil) diff --git a/pkg/plugins/dashboards_test.go b/pkg/plugins/dashboards_test.go index 980d7bb91bd..e1a927eb36f 100644 --- a/pkg/plugins/dashboards_test.go +++ b/pkg/plugins/dashboards_test.go @@ -1,6 +1,7 @@ package plugins import ( + "context" "testing" "github.com/grafana/grafana/pkg/bus" @@ -17,7 +18,7 @@ func TestPluginDashboards(t *testing.T) { setting.Cfg = ini.Empty() sec, _ := setting.Cfg.NewSection("plugin.test-app") sec.NewKey("path", "../../tests/test-app") - err := Init() + err := Init(context.TODO()) So(err, ShouldBeNil) diff --git a/pkg/plugins/datasource_plugin.go b/pkg/plugins/datasource_plugin.go index 1fe54482c7e..fb164f29c6c 100644 --- a/pkg/plugins/datasource_plugin.go +++ b/pkg/plugins/datasource_plugin.go @@ -1,6 +1,7 @@ package plugins import ( + "context" "encoding/json" "fmt" "os" @@ -69,11 +70,11 @@ func buildExecutablePath(pluginDir, executable, os, arch string) string { return path.Join(pluginDir, fmt.Sprintf("%s_%s_%s", executable, strings.ToLower(os), strings.ToLower(arch))) } -func (p *DataSourcePlugin) initBackendPlugin(log log.Logger) error { +func (p *DataSourcePlugin) initBackendPlugin(ctx context.Context, log log.Logger) error { p.log = log.New("plugin-id", p.Id) p.spawnSubProcess() - go p.reattachKilledProcess() + go p.reattachKilledProcess(ctx) return nil } @@ -108,14 +109,17 @@ func (p *DataSourcePlugin) spawnSubProcess() error { return nil } -func (p *DataSourcePlugin) reattachKilledProcess() { +func (p *DataSourcePlugin) reattachKilledProcess(ctx context.Context) error { ticker := time.NewTicker(time.Second * 1) for { select { + case <-ctx.Done(): + return ctx.Err() case <-ticker.C: if p.client.Exited() { err := p.spawnSubProcess() + p.log.Debug("Spawning new sub process", "name", p.Name, "id", p.Id) if err != nil { p.log.Error("Failed to spawn subprocess") } @@ -126,6 +130,7 @@ func (p *DataSourcePlugin) reattachKilledProcess() { func (p *DataSourcePlugin) Kill() { if p.client != nil { + p.log.Debug("Killing subprocess ", "name", p.Name) p.client.Kill() } } diff --git a/pkg/plugins/models.go b/pkg/plugins/models.go index b306fa69cdd..541b37c8a8a 100644 --- a/pkg/plugins/models.go +++ b/pkg/plugins/models.go @@ -41,8 +41,6 @@ type PluginBase struct { HideFromList bool `json:"hideFromList,omitempty"` State string `json:"state,omitempty"` - - IncludedInAppId string `json:"-"` PluginDir string `json:"-"` DefaultNavUrl string `json:"-"` diff --git a/pkg/plugins/plugins.go b/pkg/plugins/plugins.go index 540321ceca3..28cc58281d9 100644 --- a/pkg/plugins/plugins.go +++ b/pkg/plugins/plugins.go @@ -42,8 +42,8 @@ type PluginManager struct { log log.Logger } -func NewPluginManager() (*PluginManager, error) { - Init() +func NewPluginManager(ctx context.Context) (*PluginManager, error) { + Init(ctx) return &PluginManager{ log: log.New("plugins"), }, nil @@ -60,7 +60,7 @@ func (p *PluginManager) Run(ctx context.Context) error { return ctx.Err() } -func Init() error { +func Init(ctx context.Context) error { plog = log.New("plugins") DataSources = make(map[string]*DataSourcePlugin) @@ -98,7 +98,7 @@ func Init() error { } for _, ds := range DataSources { if ds.Backend { - ds.initBackendPlugin(plog) + ds.initBackendPlugin(ctx, plog) } ds.initFrontendPlugin() diff --git a/pkg/plugins/plugins_test.go b/pkg/plugins/plugins_test.go index f2dbc1e2e82..e2dc17f3ae7 100644 --- a/pkg/plugins/plugins_test.go +++ b/pkg/plugins/plugins_test.go @@ -1,6 +1,7 @@ package plugins import ( + "context" "path/filepath" "testing" @@ -14,7 +15,7 @@ func TestPluginScans(t *testing.T) { Convey("When scaning for plugins", t, func() { setting.StaticRootPath, _ = filepath.Abs("../../public/") setting.Cfg = ini.Empty() - err := Init() + err := Init(context.TODO()) So(err, ShouldBeNil) So(len(DataSources), ShouldBeGreaterThan, 1) @@ -29,7 +30,7 @@ func TestPluginScans(t *testing.T) { setting.Cfg = ini.Empty() sec, _ := setting.Cfg.NewSection("plugin.nginx-app") sec.NewKey("path", "../../tests/test-app") - err := Init() + err := Init(context.TODO()) So(err, ShouldBeNil) So(len(Apps), ShouldBeGreaterThan, 0)