diff --git a/cmd/daemon.go b/cmd/daemon.go index afa83d3..09278b3 100644 --- a/cmd/daemon.go +++ b/cmd/daemon.go @@ -9,10 +9,14 @@ import ( "path/filepath" "strings" + "context" "github.com/Sirupsen/logrus" + "github.com/docker/docker/api/types" + "github.com/docker/docker/client" "github.com/gianarb/orbiter/api" "github.com/gianarb/orbiter/autoscaler" "github.com/gianarb/orbiter/core" + "time" ) type DaemonCmd struct { @@ -54,19 +58,47 @@ func (c *DaemonCmd) Run(args []string) int { } } else { logrus.Info("Starting in auto-detection mode.") - err = core.Autodetect(&coreEngine) + /* err = core.Autodetect(&coreEngine) if err != nil { logrus.WithField("error", err).Info(err) os.Exit(0) - } + }*/ } + + // Timer ticker + timer1 := time.NewTicker(1000 * time.Millisecond) + + // Watchdog go func() { sigchan := make(chan os.Signal, 10) signal.Notify(sigchan, os.Interrupt) <-sigchan + timer1.Stop() logrus.Info("Stopping and cleaning. Bye!") os.Exit(0) }() + + // Background service check + go func() { + counter := 0 + ctx := context.Background() + dockerClient, _ := client.NewEnvClient() + for { + <-timer1.C + + services, _ := dockerClient.ServiceList(ctx, types.ServiceListOptions{}) + if len(services) != counter { + logrus.Debugf("Service list changed %d -> %d", counter, len(services)) + err = core.Autodetect(&coreEngine) + if err != nil { + logrus.WithField("error", err).Info(err) + } + counter = len(services) + } + } + }() + + // Add routing router := api.GetRouter(coreEngine, c.EventChannel) logrus.Infof("API Server run on port %s", port) http.ListenAndServe(port, router) diff --git a/core/autodetect.go b/core/autodetect.go index cdac19c..8d3b58a 100644 --- a/core/autodetect.go +++ b/core/autodetect.go @@ -52,7 +52,7 @@ func autoDetectSwarmMode(c *Core) { if err != nil { continue } - c.Autoscalers[fmt.Sprintf("autodetect_swarm/%s", service.Spec.Annotations.Name)] = s + c.Autoscalers[fmt.Sprintf("autoswarm/%s", service.Spec.Annotations.Name)] = s } } @@ -64,7 +64,7 @@ func getAutoscalerByService(p autoscaler.Provider, an swarm.Annotations) (autosc up := convertStringLabelToInt("orbiter.up", an.Labels) down := convertStringLabelToInt("orbiter.down", an.Labels) as := autoscaler.NewAutoscaler(p, an.Name, up, down) - logrus.Debugf("autodetect_swarm/%s added to orbiter. UP %d, DOWN %d", an.Name, up, down) + logrus.Debugf("Registering /handle/autoswarm/%s to orbiter. (UP %d, DOWN %d)", an.Name, up, down) return as, nil }