14324: Embed test public key instead of reading from file
[arvados.git] / lib / dispatchcloud / dispatcher.go
index 1a47e8490b48c14cade2564ad0f64a7c399bbcf1..2415094ac00290625794d8204449052ad749ad85 100644 (file)
@@ -21,9 +21,9 @@ import (
        "git.curoverse.com/arvados.git/sdk/go/arvados"
        "git.curoverse.com/arvados.git/sdk/go/auth"
        "git.curoverse.com/arvados.git/sdk/go/httpserver"
-       "github.com/Sirupsen/logrus"
        "github.com/prometheus/client_golang/prometheus"
        "github.com/prometheus/client_golang/prometheus/promhttp"
+       "github.com/sirupsen/logrus"
        "golang.org/x/crypto/ssh"
 )
 
@@ -35,6 +35,7 @@ const (
 type pool interface {
        scheduler.WorkerPool
        Instances() []worker.InstanceView
+       Stop()
 }
 
 type dispatcher struct {
@@ -51,6 +52,7 @@ type dispatcher struct {
 
        setupOnce sync.Once
        stop      chan struct{}
+       stopped   chan struct{}
 }
 
 // Start starts the dispatcher. Start can be called multiple times
@@ -79,6 +81,7 @@ func (disp *dispatcher) Close() {
        case disp.stop <- struct{}{}:
        default:
        }
+       <-disp.stopped
 }
 
 // Make a worker.Executor for the given instance.
@@ -109,6 +112,7 @@ func (disp *dispatcher) initialize() {
                }
        }
        disp.stop = make(chan struct{}, 1)
+       disp.stopped = make(chan struct{})
        disp.logger = logrus.StandardLogger()
 
        if key, err := ssh.ParsePrivateKey(disp.Cluster.Dispatch.PrivateKey); err != nil {
@@ -117,7 +121,7 @@ func (disp *dispatcher) initialize() {
                disp.sshKey = key
        }
 
-       instanceSet, err := newInstanceSet(disp.Cluster, disp.InstanceSetID)
+       instanceSet, err := newInstanceSet(disp.Cluster, disp.InstanceSetID, disp.logger)
        if err != nil {
                disp.logger.Fatalf("error initializing driver: %s", err)
        }
@@ -144,7 +148,9 @@ func (disp *dispatcher) initialize() {
 }
 
 func (disp *dispatcher) run() {
+       defer close(disp.stopped)
        defer disp.instanceSet.Stop()
+       defer disp.pool.Stop()
 
        staleLockTimeout := time.Duration(disp.Cluster.Dispatch.StaleLockTimeout)
        if staleLockTimeout == 0 {