浏览代码

dont spawn new subprocess while shutting down

bergquist 8 年之前
父节点
当前提交
75a54e85dc

+ 1 - 1
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)
 	}

+ 2 - 1
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)
 

+ 2 - 1
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)
 

+ 8 - 3
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()
 	}
 }

+ 0 - 2
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:"-"`

+ 4 - 4
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()

+ 3 - 2
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)