Example #1
0
func executeRequests(u utils.Utils, requests *MapServiceRequest, data *MapUrlServiceCount) {
	utils.Verbosef(u, "services.executeRequests(%s)", requests)
	if len(*requests) < 1 {
		return
	}

	var wg sync.WaitGroup
	var cacheWg sync.WaitGroup
	cacheCh := make(chan cache, 1)
	caches := make(sliceCache, 0)
	var historyWg sync.WaitGroup
	historyTime := time.Now()
	historyCh := make(chan utils.HistoryRecord, 1)
	histories := make([]utils.HistoryRecord, 0)

	wg.Add(len(*requests))
	for _, req := range *requests {
		go func(req request) {
			defer wg.Done()

			req.Worker(u, &req)
			if req.Error != nil {
				u.Errorf("services.%s: %v", req.Service, req.Error)
			}

			for url, res := range req.Results {
				oldCount, _ := (*data)[url][req.Service]
				(*data)[url][req.Service] = res.Count

				if res.Error != nil {
					u.Errorf("services.%s: %s error %v response %s", req.Service, url, res.Error, res.Response)
				} else {
					if res.Count > COUNT_INITIAL_VALUE && res.Count > oldCount {
						cacheWg.Add(1)
						cacheCh <- cache{Service: req.Service, Url: url, Count: res.Count}

						historyWg.Add(1)
						historyCh <- utils.HistoryRecord{Service: req.Service, Url: url, Count: res.Count, Time: historyTime}
					}
				}
			}
		}(req)
	}

	go func() {
		for cache := range cacheCh {
			caches = append(caches, cache)
			cacheWg.Done()
		}
	}()

	go func() {
		for history := range historyCh {
			histories = append(histories, history)
			historyWg.Done()
		}
	}()

	wg.Wait()
	cacheWg.Wait()
	historyWg.Wait()

	setCaches(u, &caches)
	if err := u.HistorySave(&histories); err != nil {
		u.Errorf("u.HistorySave error %v", err)
	}
}