Exemple #1
0
// UpdateCurrent sets the current queue length.
func (nsqm *NSQMetric) UpdateCurrent() {
	var statsMessage StatsMessage

	url := "http://" + nsqStatsEndpoint + constNSQStatsAPI
	body, err := utils.GetJSON(url)
	if err != nil {
		log.Errorf("Error getting NSQ metric %v", err)
	}

	err = json.Unmarshal(body, &statsMessage)
	if err != nil {
		log.Errorf("Error %v unmarshalling from %s", err, string(body[:]))
	}

	// Loop through NSQ Channels and Metrics to find the correct value.
	for _, topic := range statsMessage.Data.Topics {
		if topic.TopicName == nsqm.topicName {
			for _, channel := range topic.Channels {
				if channel.ChannelName == nsqm.channelName {
					nsqm.currentVal = channel.Depth
				}
			}
		}
	}

	log.Debugf("Topic: %s Channel: %s Length: %d", nsqm.topicName, nsqm.channelName, nsqm.currentVal)
}
Exemple #2
0
// GetApps retrives the app definitions from the server for a given userID
func GetApps(apiAddress string, userID string) (tasks []*demand.Task, maxContainers int, err error) {
	url := "http://" + apiAddress + "/apps/" + userID

	body, err := utils.GetJSON(url)
	if err != nil {
		log.Debugf("Failed to get /apps/: %v", err)
		return nil, 0, err
	}

	tasks, maxContainers, err = appsFromResponse(body)
	return
}
// CountAllTasks tells us how many instances of each task are currently running.
func (m *MarathonScheduler) CountAllTasks(running *demand.Tasks) error {
	var (
		err         error
		appsMessage AppsMessage
	)

	running.Lock()
	defer running.Unlock()

	url := m.baseMarathonURL + "apps/"

	body, err := utils.GetJSON(url)
	if err != nil {
		log.Errorf("Error getting Marathon Apps %v", err)
		return err
	}

	err = json.Unmarshal(body, &appsMessage)
	if err != nil {
		log.Errorf("Error %v unmarshalling from %s", err, string(body[:]))
		return err
	}

	appCounts := make(map[string]int)

	// Remove leading slash from App IDs and set the instance counts.
	for _, app := range appsMessage.Apps {
		appCounts[strings.Replace(app.ID, "/", "", 1)] = app.Instances
	}

	// Set running counts. Defaults to 0 if the App does not exist.
	tasks := running.Tasks
	for _, t := range tasks {
		t.Running = appCounts[t.Name]
	}

	return err
}