dont spawn new subprocess while shutting down

This commit is contained in:
bergquist
2017-12-25 13:48:50 +01:00
parent 05362a9666
commit 75a54e85dc
7 changed files with 20 additions and 14 deletions

View File

@ -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()
}
}