Exemplo n.º 1
0
func (ic *InstanceChooser) add(instance *skynet.ServiceInfo) {
	for _, in := range ic.instances {
		if in.GetConfigPath() == instance.GetConfigPath() {
			return
		}
	}
	ic.instances = append(ic.instances, instance)
}
Exemplo n.º 2
0
func (ic *InstanceChooser) remove(instance *skynet.ServiceInfo) {
	for i, in := range ic.instances {
		if in.GetConfigPath() == instance.GetConfigPath() {
			ic.instances[i] = ic.instances[len(ic.instances)-1]
			ic.instances = ic.instances[:len(ic.instances)-1]
			return
		}
	}
}
Exemplo n.º 3
0
func (im *InstanceMonitor) monitorInstanceStats() {
	rev := im.doozer.GetCurrentRevision()

	watchPath := path.Join("/statistics", "**")

	for {
		ev, err := im.doozer.Wait(watchPath, rev+1)
		rev = ev.Rev

		if err != nil {
			continue
		}

		// If it's being removed no need to send notification, it's sent my monitorInstances
		if ev.IsDel() {
			continue
		} else {
			var s skynet.ServiceInfo
			var stats skynet.ServiceStatistics
			var ok bool

			servicePath := strings.Replace(ev.Path, "/statistics", "/services", 1)

			// If InstanceMonitor doesn't know about it, it was probably deleted, safe not to send notification
			if s, ok = im.instances[servicePath]; !ok {
				continue
			}

			buf := bytes.NewBuffer(ev.Body)
			err = json.Unmarshal(buf.Bytes(), &stats)

			if err != nil {
				fmt.Println("error unmarshalling service")
				continue
			}

			s.Stats = &stats

			// Let's create an update notification to send, with our new statistics
			im.notificationChan <- InstanceMonitorNotification{
				Path:       ev.Path,
				Service:    s,
				OldService: im.instances[servicePath],
				Type:       InstanceStatsUpdateNotification,
			}
		}
	}
}