func deleteItemsAccordingToHeartbeat(body []byte) { log.Debug("deleteItemsAccordingToHeartbeat() BEGIN:", string(body)) result := ds.Result{} itemEvent := &Event{} result.Data = itemEvent itemsdelete := []ItemDel{} itemEvent.Data = &itemsdelete if err := json.Unmarshal(body, &result); err == nil { log.Debug("items delete:", itemsdelete) for _, v := range itemsdelete { log.Debugf("delete item according to heartbeat: %v/%v\n", v.Repname, v.Itemname) err := delTagsForDelItem(v.Repname, v.Itemname) if err != nil { log.Error(err) return } err = delItem(v.Repname, v.Itemname) if err != nil { log.Error(err) return } log.Infof("Delete data item %v/%v according to heartbeat successfully.\n", v.Repname, v.Itemname) } } else { log.Warn("Unmarshal error:", err) } }
func EnvDebug(name string, required bool) string { s := os.Getenv(name) if required && s == "" { panic("env variable required, " + name) } log.Debugf("[env][%s] %s\n", name, s) return s }
func SetEnv(name string, value string) string { if e := os.Setenv(name, value); e != nil { log.Errorf("[setenv][%s] %s, error:%v\n", name, value, e) return "" } log.Debugf("[setenv][%s] %s\n", name, value) return name }
func InsertToTagadded(EventTime time.Time, Repname, Itemname, Tag string, status int) (err error) { log.Debugf("Insert into MSG_TAGADDED time:%s, repo:%s, item:%s, tag:%s, status:%d", EventTime, Repname, Itemname, Tag, status) sql := fmt.Sprintf(`INSERT INTO MSG_TAGADDED (ID, REPOSITORY, DATAITEM, TAG, STATUS, CREATE_TIME, STATUS_TIME) VALUES (null, '%s', '%s', '%s', %d, '%s',datetime('now'));`, Repname, Itemname, Tag, status, EventTime.Format("2006-01-02 15:04:05")) _, err = g_ds.Insert(sql) if err != nil { l := log.Error(err) logq.LogPutqueue(l) } return err }
func getItemCountByDpRepo(datapool, repo, isPublished string) (count int64) { sql := fmt.Sprintf(`SELECT COUNT(*) FROM DH_DP_RPDM_MAP WHERE DPID IN (SELECT DPID FROM DH_DP WHERE DPNAME = '%s' AND STATUS='A') AND REPOSITORY = '%s' AND PUBLISH = '%s' AND STATUS = 'A';`, datapool, repo, isPublished) row, err := g_ds.QueryRow(sql) if err != nil { l := log.Error(err) logq.LogPutqueue(l) return 0 } row.Scan(&count) log.Debugf("Dataitems count of repository %v: %v", repo, count) return }
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) } } } } }