Skip to content

Commit

Permalink
Fix missing configuration options panics (#105)
Browse files Browse the repository at this point in the history
Fix invalid minWait for backoff notifier when SubscriptionBackoffMax not set
  • Loading branch information
AlfredBroda authored Apr 9, 2018
1 parent add8f4b commit 0d1e155
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 4 deletions.
5 changes: 3 additions & 2 deletions cmd/executor/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ func readConfiguration(config interface{}) {

func main() {
log.Infof("Allegro Mesos Executor (version: %s)", Version)

cfg, err := config.FromEnv()
if err != nil {
log.WithError(err).Fatal("Failed to load Mesos configuration")
Expand All @@ -118,8 +119,8 @@ func main() {
Config.MesosConfig.Checkpoint = cfg.Checkpoint
Config.MesosConfig.RecoveryTimeout = cfg.RecoveryTimeout
Config.MesosConfig.SubscriptionBackoffMax = cfg.SubscriptionBackoffMax
exec := executor.NewExecutor(Config, createHooks()...)
if err := exec.Start(); err != nil {
err = executor.StartExecutor(Config, createHooks())
if err != nil {
log.WithError(err).Fatal("Executor exited with error")
}
log.Info("Executor exited successfully")
Expand Down
26 changes: 26 additions & 0 deletions executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,32 @@ func NewExecutor(cfg Config, hooks ...hook.Hook) *Executor {
}
}

// StartExecutor creates a new executor instance nad starts it
func StartExecutor(conf Config, hooks []hook.Hook) error {
exec := NewExecutor(sanitizeConfig(conf), hooks...)
return exec.Start()
}

func sanitizeConfig(conf Config) Config {
if conf.RandomExpirationRange <= 0 {
conf.RandomExpirationRange = 3 * time.Hour
}
if conf.APIPath == "" {
conf.APIPath = "/api/v1/executor"
}
if conf.HTTPTimeout <= 0 {
conf.HTTPTimeout = 10 * time.Second
}
if conf.MesosConfig.RecoveryTimeout <= 0 {
conf.MesosConfig.RecoveryTimeout = time.Second
}
if conf.MesosConfig.SubscriptionBackoffMax < time.Second {
conf.MesosConfig.SubscriptionBackoffMax = time.Second
}

return conf
}

// Start registers executor in Mesos agent and waits for events from it.
func (e *Executor) Start() error {

Expand Down
9 changes: 7 additions & 2 deletions executor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@ import (
"crypto/x509"
"errors"
"testing"

"time"

mesos "github.com/mesos/mesos-go/api/v1/lib"
"github.com/mesos/mesos-go/api/v1/lib"
"github.com/mesos/mesos-go/api/v1/lib/executor"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
Expand Down Expand Up @@ -382,6 +381,12 @@ func TestIfNotPanicsWhenKillWithoutLaunch(t *testing.T) {
})
}

func TestIfExecutorStartsWithoutConfig(t *testing.T) {
assert.NotPanics(t, func() {
StartExecutor(Config{}, []hook.Hook{})
})
}

type mockClock struct {
mock.Mock
}
Expand Down

0 comments on commit 0d1e155

Please sign in to comment.