Beispiel #1
0
func getAccountByID(id string) bool {
	if _, err := existingAccountByID.Get(id); err == nil {
		return true
	}

	if _, err := deletedAccountByID.Get(id); err == nil {
		return false
	}

	acc, err := helper.GetAccountById(id)
	if err == mgo.ErrNotFound {
		deletedAccountByID.Set(id, struct{}{})
		return false
	}

	// treat them as existing on random errors
	if err != nil {
		fmt.Printf("err while getting acc by id %q, %s\n", id, err.Error())
		return true
	}

	if acc.Type == "deleted" {
		deletedAccountByID.Set(id, struct{}{})
		deletedAccountByNick.Set(acc.Profile.Nickname, struct{}{})
		return false
	}

	existingAccountByID.Set(id, struct{}{})
	existingAccountByNick.Set(acc.Profile.Nickname, id)

	return true
}
Beispiel #2
0
func (a *Account) DeleteIfNotInMongo() error {
	if a.OldId == "" {
		return ErrOldIdIsNotSet
	}

	_, err := modelhelper.GetAccountById(a.OldId)
	if err != nil {
		if err == mgo.ErrNotFound {
			return a.Delete()
		}
		return err
	}

	return nil
}
Beispiel #3
0
func (f *Controller) DeleteNicksWithQuery(queryName string) error {
	index, err := f.indexes.GetIndex(IndexAccounts)
	params := map[string]interface{}{"restrictSearchableAttributes": "nick"}
	_, err = index.Search(queryName, params)
	if err != nil {
		return err
	}

	var nbHit int
	var pages int

	for pages > 0 && nbHit != 0 {
		record, err := index.Search(queryName, params)
		hist := record.Hits

		pages = record.NbPages
		nbHit = record.NbHits

		index, err := f.indexes.GetIndex(IndexAccounts)
		if err != nil {
			return err
		}

		record, err = index.Search(queryName, params)
		if err != nil {
			return err
		}

		hist = record.Hits
		for _, val := range hist {
			object := val["objectID"].(string)
			_, err := modelhelper.GetAccountById(object)
			if err != nil && err != modelhelper.ErrNotFound {
				return err
			}
			if err == modelhelper.ErrNotFound {
				if _, err = index.DeleteObject(object); err != nil {
					return nil
				}
			}
		}
	}
	return nil
}
Beispiel #4
0
func (mwc *Controller) AccountIdByOldId(oldId string) (int64, error) {
	acc, err := models.Cache.Account.ByOldId(oldId)
	if err == nil {
		return acc.Id, nil
	}

	acc1, err := modelhelper.GetAccountById(oldId)
	if err != nil {
		return 0, fmt.Errorf("Participant account %s cannot be fetched: %s", oldId, err)
	}

	a := models.NewAccount()
	a.OldId = oldId
	a.Nick = acc1.Profile.Nickname
	if err := a.FetchOrCreate(); err != nil {
		return 0, err
	}

	if err := models.Cache.Account.SetToCache(a); err != nil {
		return 0, err
	}

	return a.Id, nil
}
Beispiel #5
0
func (f *Controller) DeleteNicksWithQueryBrowseAll(queryName string) error {
	index, err := f.indexes.GetIndex(IndexAccounts)
	params := map[string]interface{}{
		"restrictSearchableAttributes": "nick",
	}
	_, err = index.Search(queryName, params)
	if err != nil {
		return err
	}

	workChan := make(chan string, 30)
	var wg sync.WaitGroup

	go func() {
		signals := make(chan os.Signal, 1)
		signal.Notify(signals)
		for {
			<-signals
			close(workChan)
		}
	}()

	worker := func(objectID string) {
		_, err := modelhelper.GetAccountById(objectID)
		if err != nil && err != modelhelper.ErrNotFound {
			fmt.Println("err.Error() for objectID-->", err.Error(), objectID)
		}
		if err == modelhelper.ErrNotFound {
			fmt.Println("deleting object", objectID)
			if _, err = index.DeleteObject(objectID); err != nil {
				fmt.Println("deleting err.Error() for objectID-->", err.Error(), objectID)
			}
		}
	}

	for i := 0; i < 20; i++ {
		wg.Add(1)
		go func(i int) {
			for id := range workChan {
				worker(id)
			}
			wg.Done()
		}(i)
	}

	params = map[string]interface{}{
		"restrictSearchableAttributes": "nick",
		"page":        3000,
		"hitsPerPage": 50,
	}

	counter := 0
	recordBrowse, err := index.BrowseAll(params)
	for {
		record, err := recordBrowse.Next()
		if err != nil {
			fmt.Println("ERROR:", err)
			return nil
		}
		counter++

		if len(record) != 0 {
			objectID := record["objectID"].(string)
			fmt.Println("counter, objectID-->", counter, objectID)
			workChan <- objectID
		}
	}

	fmt.Println("waiting for workers-->")

	wg.Wait()

	return nil

}