Beispiel #1
0
func GetPulledRepoInfo(dpName, repoName string, offset int64, limit int) ([]ds.PulledItemInfo, error) {

	var pulledItemInfo ds.PulledItemInfo
	pulledItemInfos := make([]ds.PulledItemInfo, 0)

	sql := fmt.Sprintf(`SELECT DPID FROM DH_DP WHERE DPNAME = '%s' AND STATUS = 'A';`, dpName)
	row, err := g_ds.QueryRow(sql)
	if err != nil {
		l := log.Error(err)
		logq.LogPutqueue(l)
		return nil, err
	}

	var dpid int
	row.Scan(&dpid)

	sql = fmt.Sprintf(`SELECT DATAITEM ,ITEMDESC
		FROM DH_DP_RPDM_MAP 
		WHERE DPID = %d AND REPOSITORY = '%s' AND PUBLISH = 'N' AND STATUS = 'A' 
		ORDER BY RPDMID 
		LIMIT %v OFFSET %v;`, dpid, repoName, limit, offset)
	rows, err := g_ds.QueryRows(sql)
	if err != nil {
		l := log.Error(err)
		logq.LogPutqueue(l)
		return nil, err
	}

	result := ds.Result{}
	pages := ds.ResultPages{}
	orderInfoSlice := []ds.OrderInfo{}
	pages.Results = &orderInfoSlice
	result.Data = &pages

	for rows.Next() {
		rows.Scan(&pulledItemInfo.ItemName, &pulledItemInfo.Location)

		path := "/api/subscriptions/pull/" + repoName + "/" + pulledItemInfo.ItemName
		resp, err := commToServerGetRsp("get", path, nil)
		if err != nil {
			l := log.Error(err)
			logq.LogPutqueue(l)
			return nil, err
		}
		defer resp.Body.Close()

		if resp.StatusCode == http.StatusUnauthorized {
			pulledItemInfo.SignTime = nil
			log.Debug("resp.StatusCode == http.StatusUnauthorized")
		} else if resp.StatusCode != http.StatusOK {
			err = errors.New("request subscriptions api failed.")
			l := log.Error(err)
			logq.LogPutqueue(l)
			return nil, err
		} else {
			respbody, err := ioutil.ReadAll(resp.Body)
			if err != nil {
				l := log.Error(err)
				logq.LogPutqueue(l)
				return nil, err
			} else {
				err = json.Unmarshal(respbody, &result)
				if err != nil {
					err = errors.New("unmarshal failed.")
					l := log.Error(err)
					logq.LogPutqueue(l)
					return nil, err
				}

				if len(orderInfoSlice) > 0 {
					//Already order by signtime desc. Get the first member.
					pulledItemInfo.SignTime = &orderInfoSlice[0].Signtime
					log.Debug("pulledItemInfo.SignTime:", pulledItemInfo.SignTime)
				}
			}
		}
		pulledItemInfos = append(pulledItemInfos, pulledItemInfo)
	}

	log.Debug(pulledItemInfos)

	return pulledItemInfos, err
}
Beispiel #2
0
func GetMessages() {
	log.Info("start GetMessages from messages server")
	var sleepInterval int
	var srtInterval string
	var e error
	url := DefaultServerAPI + "/notifications?forclient=1&type=item_event&status=0"
	for AutoPull == true {

		if srtInterval = os.Getenv("DATAHUB_MSG_INTERVAL"); len(srtInterval) > 0 {
			sleepInterval, e = strconv.Atoi(srtInterval)
			if e != nil {
				l := log.Error(e)
				logq.LogPutqueue(l)
			}
		} else {
			sleepInterval = 600
		}

		time.Sleep(time.Duration(sleepInterval) * time.Second)
		log.Debug("connecting to", url)
		req, err := http.NewRequest("GET", url, nil)

		if len(loginAuthStr) > 0 {
			req.Header.Set("Authorization", loginAuthStr)
		}

		resp, err := http.DefaultClient.Do(req)
		if err != nil {
			l := log.Error(err)
			logq.LogPutqueue(l)

			continue
		}
		defer resp.Body.Close()

		body, _ := ioutil.ReadAll(resp.Body)

		if resp.StatusCode == http.StatusOK {
			log.Debugf("HeartBeat http statuscode:%v,  http body:%s", resp.StatusCode, body)

			result := ds.Result{}
			Pages := ds.ResultPages{}
			MessagesSlice := []Messages{}
			Pages.Results = &MessagesSlice
			result.Data = &Pages

			if err := json.Unmarshal(body, &result); err == nil {
				if result.Code == 0 {
					log.Debug(result)
					for _, v := range MessagesSlice {
						if v.Type == "item_event" && v.Data.Event == TAGADDED {
							InsertToTagadded(v.Data.EventTime, v.Data.Repname, v.Data.Itemname, v.Data.Tag, NOTREAD)
						}
					}
				} else {
					l := log.Error("Get Messages errror:", result.Code)
					logq.LogPutqueue(l)
				}
			} else {
				log.Error(err)
			}

		} else if resp.StatusCode == http.StatusUnauthorized {
			log.Debug("not login", http.StatusUnauthorized)
			urllogin := DefaultServerAPI + "/"
			reql, err := http.NewRequest("GET", urllogin, nil)
			if len(loginBasicAuthStr) > 0 {
				reql.Header.Set("Authorization", loginBasicAuthStr)
				log.Info("user name:", gstrUsername)
			} else {
				log.Warn("not login")
				continue
			}

			respl, err := http.DefaultClient.Do(reql)
			if err != nil {
				log.Error(err)
				continue
			}
			defer respl.Body.Close()

			result := &ds.Result{}
			log.Println("login return", respl.StatusCode)
			if respl.StatusCode == 200 {
				body, _ := ioutil.ReadAll(respl.Body)
				log.Println(string(body))

				result.Data = &tk{}
				if err = json.Unmarshal(body, result); err != nil {
					log.Error(err)
					log.Println(respl.StatusCode, string(body))
					continue
				} else {
					loginAuthStr = "Token " + result.Data.(*tk).Token
					loginLogged = true
					log.Println(loginAuthStr)
				}
			}
		}
	}
}