Пример #1
0
func updateFromSource2ForAppEngine(r *http.Request) (int64, bool, error) {
	var result int64
	ctx := appengine.NewContext(r)
	client := urlfetch.Client(ctx)
	resp, err := client.Get(source2Url)
	if err != nil {
		return 0, false, err
	}
	defer resp.Body.Close()

	dec := json.NewDecoder(resp.Body)
	if resp.StatusCode == 200 {
		var rate entities.Rate2Response
		dec.Decode(&rate)
		result = rate.TimestampUnix
		repo := repository.New(repoSize, true, r)
		if err := repo.Push(entities.Rate{
			Base: rate.Base,
			ID:   rate.TimestampUnix,
			RUB:  rate.Quotes["USDRUB"],
			JPY:  rate.Quotes["USDJPY"],
			GBP:  rate.Quotes["USDGBP"],
			USD:  rate.Quotes["USDUSD"],
			EUR:  rate.Quotes["USDEUR"],
			CNY:  rate.Quotes["USDCNY"],
			CHF:  rate.Quotes["USDCHF"]}); err != nil {
			return 0, false, fmt.Errorf("Push rate to repo error: %v", err)
		}
	} else {
		var err2Resp entities.Error2Response
		dec.Decode(&err2Resp)
		return 0, false, fmt.Errorf(err2Resp.ToString())
	}
	return result, true, nil
}
Пример #2
0
func init() {
	_repo = repository.New(repoSize, true, nil)
	_rubWork = work.NewWork(6, 5, 20, 1, work.TTLbfgs, "RUB")
	_eurWork = work.NewWork(6, 5, 20, 1, work.TTLbfgs, "EUR")
	_gbpWork = work.NewWork(6, 5, 20, 1, work.TTLbfgs, "GBP")
	_jpyWork = work.NewWork(6, 5, 20, 1, work.TTLbfgs, "JPY")
	_cnyWork = work.NewWork(6, 5, 20, 1, work.TTLbfgs, "CNY")
	_chfWork = work.NewWork(6, 5, 20, 1, work.TTLbfgs, "CHF")

	_now := time.Now()
	_next := _now.Round(time.Hour)
	if _next.Hour() == _now.Hour() {
		_next = _next.Add(time.Hour)
	}
	_next = _next.Add(time.Second * 10)
	log.Printf("Start tick: %v.", _next)
	ticker := time.NewTicker(_next.Sub(_now))
	// ticker := time.NewTicker(time.Second)
	quit := make(chan struct{})

	for {
		select {
		case <-ticker.C:
			ticker.Stop()
			timestamp, success, err := updateFromSource2()
			if err != nil {
				log.Fatal(err)
				return
			}
			now := time.Now()
			//	next := now.Round(time.Hour).Add(time.Hour).Add(time.Second * 10)
			next := time.Unix(timestamp, 0).Add(time.Hour)

			if success == false {
				next = now.Round(time.Minute).Add(time.Minute * 5)
			} else {
				// logic here
				go executeDomainLogic()
			}

			ticker = time.NewTicker(next.Sub(now))
			log.Printf("Next tick: %v;\t Repo length: %d.", next, _repo.Len())

		case <-quit:
			ticker.Stop()
			return
		}
	}
}
Пример #3
0
func executeDomainLogic(w http.ResponseWriter, r *http.Request) {
	repo := repository.New(repoSize, true, r)
	rates := repo.GetAll()

	for key, work := range works {
		if work.Limit < len(rates) {
			_, err := work.Process(rates, r)

			if err != nil {
				if r != nil {
					ctx := appengine.NewContext(r)
					logAE.Warningf(ctx, "Error: %v", fmt.Errorf("%s executeDomainLogic error: %v", key, err))
				} else {
					log.Printf("%s executeDomainLogic error: %v", key, err)
				}
			}
		}
	}

	controllers.ReloadData(r)

	w.Header().Set("Cache-Control", "no-cache")
	w.WriteHeader(http.StatusOK)
}
Пример #4
0
func initializeRepo(r *http.Request) {
	_rateRepo = repository.New(historyLimit+5, false, r)
	_rates = _rateRepo.GetAll()

	_rubResultRepo = repository.NewResultDataRepo(historyLimit, false, "RUB", r)
	_rubEffRepo = repository.NewEfficiencyRepo("L-BFGS", "RUB", 6, 20, 5, r)

	_eurResultRepo = repository.NewResultDataRepo(historyLimit, false, "EUR", r)
	_eurEffRepo = repository.NewEfficiencyRepo("L-BFGS", "EUR", 6, 20, 5, r)

	_gbpResultRepo = repository.NewResultDataRepo(historyLimit, false, "GBP", r)
	_gbpEffRepo = repository.NewEfficiencyRepo("L-BFGS", "GBP", 6, 20, 5, r)

	_chfResultRepo = repository.NewResultDataRepo(historyLimit, false, "CHF", r)
	_chfEffRepo = repository.NewEfficiencyRepo("L-BFGS", "CHF", 6, 20, 5, r)

	_cnyResultRepo = repository.NewResultDataRepo(historyLimit, false, "CNY", r)
	_cnyEffRepo = repository.NewEfficiencyRepo("L-BFGS", "CNY", 6, 20, 5, r)

	_jpyResultRepo = repository.NewResultDataRepo(historyLimit, false, "JPY", r)
	_jpyEffRepo = repository.NewEfficiencyRepo("L-BFGS", "JPY", 6, 20, 5, r)

	_initialized = true
}