14360: Use consts for tag keys.
[arvados.git] / lib / dispatchcloud / worker / pool.go
index 85e0801424135901b8e5c2a096b23efb994f99bf..f2a53acbaa746b5e365d681e98a99ac2456ac764 100644 (file)
@@ -18,6 +18,11 @@ import (
        "github.com/prometheus/client_golang/prometheus"
 )
 
+const (
+       tagKeyInstanceType = "InstanceType"
+       tagKeyHold         = "Hold"
+)
+
 // A View shows a worker's current state and recent activity.
 type View struct {
        Instance             string
@@ -45,6 +50,8 @@ type Executor interface {
        //
        // SetTarget must not block on concurrent Execute calls.
        SetTarget(cloud.ExecutorTarget)
+
+       Close()
 }
 
 const (
@@ -198,7 +205,7 @@ func (wp *Pool) Create(it arvados.InstanceType) error {
        wp.setupOnce.Do(wp.setup)
        wp.mtx.Lock()
        defer wp.mtx.Unlock()
-       tags := cloud.InstanceTags{"InstanceType": it.Name}
+       tags := cloud.InstanceTags{tagKeyInstanceType: it.Name}
        wp.creating[it]++
        go func() {
                inst, err := wp.instanceSet.Create(it, wp.imageID, tags, nil)
@@ -237,7 +244,7 @@ func (wp *Pool) updateWorker(inst cloud.Instance, it arvados.InstanceType, initi
                }
                return
        }
-       if initialState == StateUnknown && inst.Tags()["hold"] != "" {
+       if initialState == StateUnknown && inst.Tags()[tagKeyHold] != "" {
                initialState = StateHold
        }
        wp.logger.WithFields(logrus.Fields{
@@ -669,7 +676,7 @@ func (wp *Pool) sync(threshold time.Time, instances []cloud.Instance) {
        wp.logger.WithField("Instances", len(instances)).Debug("sync instances")
 
        for _, inst := range instances {
-               itTag := inst.Tags()["InstanceType"]
+               itTag := inst.Tags()[tagKeyInstanceType]
                it, ok := wp.instanceTypes[itTag]
                if !ok {
                        wp.logger.WithField("Instance", inst).Errorf("unknown InstanceType tag %q --- ignoring", itTag)
@@ -688,6 +695,7 @@ func (wp *Pool) sync(threshold time.Time, instances []cloud.Instance) {
                })
                logger.Info("instance disappeared in cloud")
                delete(wp.workers, id)
+               go wkr.executor.Close()
                go wp.notify()
        }