func (s *Scheduler) HandleHostEvent(e *discoverd.Event) { log := s.logger.New("fn", "HandleHostEvent", "event.type", e.Kind) log.Info("handling host event") switch e.Kind { case discoverd.EventKindUp: s.handleNewHost(e.Instance.Meta["id"]) case discoverd.EventKindUpdate: id := e.Instance.Meta["id"] _, isShutdown := e.Instance.Meta["shutdown"] // if we haven't seen this host before, handle it as new // (provided it is not shutdown) host, ok := s.hosts[id] if !ok { if !isShutdown { s.handleNewHost(id) } return } // if the host is shutdown, just mark it as shutdown and return // rather than explicitly unfollowing to avoid a race where // SyncHosts could run before we get the down event, thus // re-following a host we know is shutdown (the host will be // unfollowed when we get the eventual down event) if isShutdown { log.Info("marking host as shutdown", "host.id", host.ID) host.shutdown = true return } // if the host's tags have changed, rectify all formations so // that any running jobs with mismatched tags are stopped, and // also try to start pending jobs in case tags now match tags := cluster.HostTagsFromMeta(e.Instance.Meta) if !host.TagsEqual(tags) { log.Info("host tags changed", "host.id", id, "from", host.Tags, "to", tags) host.Tags = tags s.rectifyAll() s.maybeStartPendingTagJobs(host) } case discoverd.EventKindDown: id := e.Instance.Meta["id"] log = log.New("host.id", id) host, ok := s.hosts[id] if !ok { log.Warn("ignoring host down event, unknown host") return } if host.shutdown { s.unfollowHost(host) } else { s.markHostAsUnhealthy(host) } } }
func (s *Scheduler) HandleHostEvent(e *discoverd.Event) { log := logger.New("fn", "HandleHostEvent", "event.type", e.Kind) log.Info("handling host event") var err error defer func() { s.sendEvent(EventTypeHostEvent, err, nil) }() switch e.Kind { case discoverd.EventKindUp: s.handleNewHost(e.Instance.Meta["id"]) case discoverd.EventKindUpdate: id := e.Instance.Meta["id"] // if we haven't seen this host before, handle it as new host, ok := s.hosts[id] if !ok { s.handleNewHost(id) return } // if the host's tags have changed, rectify all formations so // that any running jobs with mismatched tags are stopped, and // also try to start pending jobs in case tags now match tags := cluster.HostTagsFromMeta(e.Instance.Meta) if !host.TagsEqual(tags) { log.Info("host tags changed", "host.id", id, "from", host.Tags, "to", tags) host.Tags = tags s.rectifyAll() s.maybeStartPendingTagJobs(host) } case discoverd.EventKindDown: id := e.Instance.Meta["id"] log = log.New("host.id", id) host, ok := s.hosts[id] if !ok { log.Warn("ignoring host down event, unknown host") return } s.markHostAsUnhealthy(host) } }