Commit 75a54e85 by bergquist

dont spawn new subprocess while shutting down

parent 05362a96
...@@ -63,7 +63,7 @@ func (g *GrafanaServerImpl) Start() error { ...@@ -63,7 +63,7 @@ func (g *GrafanaServerImpl) Start() error {
login.Init() login.Init()
social.NewOAuthService() social.NewOAuthService()
pluginManager, err := plugins.NewPluginManager() pluginManager, err := plugins.NewPluginManager(g.context)
if err != nil { if err != nil {
return fmt.Errorf("Failed to start plugins. error: %v", err) return fmt.Errorf("Failed to start plugins. error: %v", err)
} }
......
package plugins package plugins
import ( import (
"context"
"io/ioutil" "io/ioutil"
"testing" "testing"
...@@ -91,7 +92,7 @@ func pluginScenario(desc string, t *testing.T, fn func()) { ...@@ -91,7 +92,7 @@ func pluginScenario(desc string, t *testing.T, fn func()) {
setting.Cfg = ini.Empty() setting.Cfg = ini.Empty()
sec, _ := setting.Cfg.NewSection("plugin.test-app") sec, _ := setting.Cfg.NewSection("plugin.test-app")
sec.NewKey("path", "../../tests/test-app") sec.NewKey("path", "../../tests/test-app")
err := Init() err := Init(context.TODO())
So(err, ShouldBeNil) So(err, ShouldBeNil)
......
package plugins package plugins
import ( import (
"context"
"testing" "testing"
"github.com/grafana/grafana/pkg/bus" "github.com/grafana/grafana/pkg/bus"
...@@ -17,7 +18,7 @@ func TestPluginDashboards(t *testing.T) { ...@@ -17,7 +18,7 @@ func TestPluginDashboards(t *testing.T) {
setting.Cfg = ini.Empty() setting.Cfg = ini.Empty()
sec, _ := setting.Cfg.NewSection("plugin.test-app") sec, _ := setting.Cfg.NewSection("plugin.test-app")
sec.NewKey("path", "../../tests/test-app") sec.NewKey("path", "../../tests/test-app")
err := Init() err := Init(context.TODO())
So(err, ShouldBeNil) So(err, ShouldBeNil)
......
package plugins package plugins
import ( import (
"context"
"encoding/json" "encoding/json"
"fmt" "fmt"
"os" "os"
...@@ -69,11 +70,11 @@ func buildExecutablePath(pluginDir, executable, os, arch string) string { ...@@ -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))) 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.log = log.New("plugin-id", p.Id)
p.spawnSubProcess() p.spawnSubProcess()
go p.reattachKilledProcess() go p.reattachKilledProcess(ctx)
return nil return nil
} }
...@@ -108,14 +109,17 @@ func (p *DataSourcePlugin) spawnSubProcess() error { ...@@ -108,14 +109,17 @@ func (p *DataSourcePlugin) spawnSubProcess() error {
return nil return nil
} }
func (p *DataSourcePlugin) reattachKilledProcess() { func (p *DataSourcePlugin) reattachKilledProcess(ctx context.Context) error {
ticker := time.NewTicker(time.Second * 1) ticker := time.NewTicker(time.Second * 1)
for { for {
select { select {
case <-ctx.Done():
return ctx.Err()
case <-ticker.C: case <-ticker.C:
if p.client.Exited() { if p.client.Exited() {
err := p.spawnSubProcess() err := p.spawnSubProcess()
p.log.Debug("Spawning new sub process", "name", p.Name, "id", p.Id)
if err != nil { if err != nil {
p.log.Error("Failed to spawn subprocess") p.log.Error("Failed to spawn subprocess")
} }
...@@ -126,6 +130,7 @@ func (p *DataSourcePlugin) reattachKilledProcess() { ...@@ -126,6 +130,7 @@ func (p *DataSourcePlugin) reattachKilledProcess() {
func (p *DataSourcePlugin) Kill() { func (p *DataSourcePlugin) Kill() {
if p.client != nil { if p.client != nil {
p.log.Debug("Killing subprocess ", "name", p.Name)
p.client.Kill() p.client.Kill()
} }
} }
...@@ -41,8 +41,6 @@ type PluginBase struct { ...@@ -41,8 +41,6 @@ type PluginBase struct {
HideFromList bool `json:"hideFromList,omitempty"` HideFromList bool `json:"hideFromList,omitempty"`
State string `json:"state,omitempty"` State string `json:"state,omitempty"`
IncludedInAppId string `json:"-"` IncludedInAppId string `json:"-"`
PluginDir string `json:"-"` PluginDir string `json:"-"`
DefaultNavUrl string `json:"-"` DefaultNavUrl string `json:"-"`
......
...@@ -42,8 +42,8 @@ type PluginManager struct { ...@@ -42,8 +42,8 @@ type PluginManager struct {
log log.Logger log log.Logger
} }
func NewPluginManager() (*PluginManager, error) { func NewPluginManager(ctx context.Context) (*PluginManager, error) {
Init() Init(ctx)
return &PluginManager{ return &PluginManager{
log: log.New("plugins"), log: log.New("plugins"),
}, nil }, nil
...@@ -60,7 +60,7 @@ func (p *PluginManager) Run(ctx context.Context) error { ...@@ -60,7 +60,7 @@ func (p *PluginManager) Run(ctx context.Context) error {
return ctx.Err() return ctx.Err()
} }
func Init() error { func Init(ctx context.Context) error {
plog = log.New("plugins") plog = log.New("plugins")
DataSources = make(map[string]*DataSourcePlugin) DataSources = make(map[string]*DataSourcePlugin)
...@@ -98,7 +98,7 @@ func Init() error { ...@@ -98,7 +98,7 @@ func Init() error {
} }
for _, ds := range DataSources { for _, ds := range DataSources {
if ds.Backend { if ds.Backend {
ds.initBackendPlugin(plog) ds.initBackendPlugin(ctx, plog)
} }
ds.initFrontendPlugin() ds.initFrontendPlugin()
......
package plugins package plugins
import ( import (
"context"
"path/filepath" "path/filepath"
"testing" "testing"
...@@ -14,7 +15,7 @@ func TestPluginScans(t *testing.T) { ...@@ -14,7 +15,7 @@ func TestPluginScans(t *testing.T) {
Convey("When scaning for plugins", t, func() { Convey("When scaning for plugins", t, func() {
setting.StaticRootPath, _ = filepath.Abs("../../public/") setting.StaticRootPath, _ = filepath.Abs("../../public/")
setting.Cfg = ini.Empty() setting.Cfg = ini.Empty()
err := Init() err := Init(context.TODO())
So(err, ShouldBeNil) So(err, ShouldBeNil)
So(len(DataSources), ShouldBeGreaterThan, 1) So(len(DataSources), ShouldBeGreaterThan, 1)
...@@ -29,7 +30,7 @@ func TestPluginScans(t *testing.T) { ...@@ -29,7 +30,7 @@ func TestPluginScans(t *testing.T) {
setting.Cfg = ini.Empty() setting.Cfg = ini.Empty()
sec, _ := setting.Cfg.NewSection("plugin.nginx-app") sec, _ := setting.Cfg.NewSection("plugin.nginx-app")
sec.NewKey("path", "../../tests/test-app") sec.NewKey("path", "../../tests/test-app")
err := Init() err := Init(context.TODO())
So(err, ShouldBeNil) So(err, ShouldBeNil)
So(len(Apps), ShouldBeGreaterThan, 0) So(len(Apps), ShouldBeGreaterThan, 0)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment