mirror of
https://github.com/grafana/grafana.git
synced 2025-07-31 05:02:35 +08:00
Chore: Avoid stopping plugin (#74654)
This commit is contained in:

committed by
GitHub

parent
8cb93bf3e7
commit
1a8a19a9ed
@ -2,12 +2,15 @@ package process
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"time"
|
||||
|
||||
"github.com/grafana/grafana/pkg/plugins"
|
||||
)
|
||||
|
||||
var (
|
||||
keepPluginAliveTickerDuration = time.Second * 1
|
||||
)
|
||||
|
||||
type Service struct{}
|
||||
|
||||
func ProvideService() *Service {
|
||||
@ -19,7 +22,7 @@ func (*Service) Start(ctx context.Context, p *plugins.Plugin) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
if err := startPluginAndRestartKilledProcesses(ctx, p); err != nil {
|
||||
if err := startPluginAndKeepItAlive(ctx, p); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@ -40,7 +43,7 @@ func (*Service) Stop(ctx context.Context, p *plugins.Plugin) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func startPluginAndRestartKilledProcesses(ctx context.Context, p *plugins.Plugin) error {
|
||||
func startPluginAndKeepItAlive(ctx context.Context, p *plugins.Plugin) error {
|
||||
if err := p.Start(ctx); err != nil {
|
||||
return err
|
||||
}
|
||||
@ -49,41 +52,35 @@ func startPluginAndRestartKilledProcesses(ctx context.Context, p *plugins.Plugin
|
||||
return nil
|
||||
}
|
||||
|
||||
go func(ctx context.Context, p *plugins.Plugin) {
|
||||
if err := restartKilledProcess(ctx, p); err != nil {
|
||||
go func(p *plugins.Plugin) {
|
||||
if err := keepPluginAlive(p); err != nil {
|
||||
p.Logger().Error("Attempt to restart killed plugin process failed", "error", err)
|
||||
}
|
||||
}(ctx, p)
|
||||
}(p)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func restartKilledProcess(ctx context.Context, p *plugins.Plugin) error {
|
||||
ticker := time.NewTicker(time.Second * 1)
|
||||
// keepPluginAlive will restart the plugin if the process is killed or exits
|
||||
func keepPluginAlive(p *plugins.Plugin) error {
|
||||
ticker := time.NewTicker(keepPluginAliveTickerDuration)
|
||||
|
||||
for {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
if err := ctx.Err(); err != nil && !errors.Is(err, context.Canceled) {
|
||||
return err
|
||||
}
|
||||
return p.Stop(ctx)
|
||||
case <-ticker.C:
|
||||
if p.IsDecommissioned() {
|
||||
p.Logger().Debug("Plugin decommissioned")
|
||||
return nil
|
||||
}
|
||||
|
||||
if !p.Exited() {
|
||||
continue
|
||||
}
|
||||
|
||||
p.Logger().Debug("Restarting plugin")
|
||||
if err := p.Start(ctx); err != nil {
|
||||
p.Logger().Error("Failed to restart plugin", "error", err)
|
||||
continue
|
||||
}
|
||||
p.Logger().Debug("Plugin restarted")
|
||||
<-ticker.C
|
||||
if p.IsDecommissioned() {
|
||||
p.Logger().Debug("Plugin decommissioned")
|
||||
return nil
|
||||
}
|
||||
|
||||
if !p.Exited() {
|
||||
continue
|
||||
}
|
||||
|
||||
p.Logger().Debug("Restarting plugin")
|
||||
if err := p.Start(context.Background()); err != nil {
|
||||
p.Logger().Error("Failed to restart plugin", "error", err)
|
||||
continue
|
||||
}
|
||||
p.Logger().Debug("Plugin restarted")
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user