Skip to content
This repository has been archived by the owner on Nov 8, 2022. It is now read-only.

Commit

Permalink
Fixes #1246: Makes plugin restart limit configurable
Browse files Browse the repository at this point in the history
Makes the MaxPluginRestartCount configurable from controls config
section in the globalconfig file. Updates the example configs to
include this field and adds tests for parsing of this option.
  • Loading branch information
croseborough committed Dec 6, 2016
1 parent 91ea244 commit 274bcb0
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 0 deletions.
9 changes: 9 additions & 0 deletions control/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ type Config struct {
ListenAddr string `json:"listen_addr,omitempty"yaml:"listen_addr"`
ListenPort int `json:"listen_port,omitempty"yaml:"listen_port"`
Pprof bool `json:"pprof"yaml:"pprof"`
MaxPluginRestarts int `json:"max_plugin_restarts"yaml:"max_plugin_restarts"`
}

const (
Expand Down Expand Up @@ -124,6 +125,9 @@ const (
},
"pprof": {
"type": "boolean"
},
"max_plugin_restarts": {
"type": "integer"
}
},
"additionalProperties": false
Expand All @@ -144,6 +148,7 @@ func GetDefaultConfig() *Config {
CacheExpiration: jsonutil.Duration{defaultCacheExpiration},
Plugins: newPluginConfig(),
Pprof: defaultPprof,
MaxPluginRestarts: MaxPluginRestartCount,
}
}

Expand Down Expand Up @@ -201,6 +206,10 @@ func (c *Config) UnmarshalJSON(data []byte) error {
if err := json.Unmarshal(v, &(c.Pprof)); err != nil {
return err
}
case "max_plugin_restarts":
if err := json.Unmarshal(v, &(c.MaxPluginRestarts)); err != nil {
return err
}
default:
return fmt.Errorf("Unrecognized key '%v' in global config file while parsing 'control'", k)
}
Expand Down
9 changes: 9 additions & 0 deletions control/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,9 @@ func TestControlConfigJSON(t *testing.T) {
Convey("MaxRunningPlugins should be set to 1", func() {
So(cfg.MaxRunningPlugins, ShouldEqual, 1)
})
Convey("max_plugin_restarts should be set to 10", func() {
So(cfg.MaxPluginRestarts, ShouldEqual, 10)
})
Convey("ListenAddr should be set to 0.0.0.0", func() {
So(cfg.ListenAddr, ShouldEqual, "0.0.0.0")
})
Expand Down Expand Up @@ -231,6 +234,9 @@ func TestControlConfigYaml(t *testing.T) {
Convey("MaxRunningPlugins should be set to 1", func() {
So(cfg.MaxRunningPlugins, ShouldEqual, 1)
})
Convey("max_plugin_restarts should be set to 10", func() {
So(cfg.MaxPluginRestarts, ShouldEqual, 10)
})
Convey("ListenAddr should be set to 0.0.0.0", func() {
So(cfg.ListenAddr, ShouldEqual, "0.0.0.0")
})
Expand Down Expand Up @@ -301,5 +307,8 @@ func TestControlDefaultConfig(t *testing.T) {
Convey("PluginTrust should equal 1", func() {
So(cfg.PluginTrust, ShouldEqual, 1)
})
Convey("max_plugin_restarts should be set to 3", func() {
So(cfg.MaxPluginRestarts, ShouldEqual, 3)
})
})
}
8 changes: 8 additions & 0 deletions control/control.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,13 +174,21 @@ func OptSetConfig(cfg *Config) PluginControlOpt {
}
}

// MaximumPluginRestarts
func MaxPluginRestarts(cfg *Config) PluginControlOpt {
return func(*pluginControl) {
MaxPluginRestartCount = cfg.MaxPluginRestarts
}
}

// New returns a new pluginControl instance
func New(cfg *Config) *pluginControl {
// construct a slice of options from the input configuration
opts := []PluginControlOpt{
MaxRunningPlugins(cfg.MaxRunningPlugins),
CacheExpiration(cfg.CacheExpiration.Duration),
OptSetConfig(cfg),
MaxPluginRestarts(cfg),
}
c := &pluginControl{}
c.Config = cfg
Expand Down
2 changes: 2 additions & 0 deletions control/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,9 @@ const (
PluginStopped
// PluginDisabled is the disabled state of a plugin
PluginDisabled
)

var (
// MaximumRestartOnDeadPluginEvent is the maximum count of restarting a plugin
// after the event of control_event.DeadAvailablePluginEvent
MaxPluginRestartCount = 3
Expand Down
1 change: 1 addition & 0 deletions examples/configs/snap-config-sample.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"gomaxprocs": 2,
"control": {
"auto_discover_path": "/some/directory/with/plugins",
"max_plugin_restarts": 10,
"cache_expiration": "750ms",
"listen_addr": "0.0.0.0",
"listen_port": 10082,
Expand Down
4 changes: 4 additions & 0 deletions examples/configs/snap-config-sample.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ control:
# not be loaded. Valid values are 0 - Off, 1 - Enabled, 2 - Warning
plugin_trust_level: 0

# max_plugin_restarts controls how many times a plugin is allowed to be restarted
# before failing.
max_plugin_restarts: 10

# plugins section contains plugin config settings that will be applied for
# plugins across tasks.
plugins:
Expand Down

0 comments on commit 274bcb0

Please sign in to comment.