def run(self): self.__resultNotifier.waitForSearchResult(self.__search_id, self.cbOnQueryResults) self.__waitCondition.acquire() while True: if len(self.__results) <= self.__cursor : self.__waitCondition.wait() else: break self.__waitCondition.release() self.__resultNotifier.removeSearchResult(self.__search_id) try: self.__cb(CursorHandler.getPageForSearchResults(self.__results, self.__cursor), self.__cursor) except Exception as e : pass
def execute(**kwargs): fbid = kwargs.get('fbid') cursor = kwargs.get('cursor') access_token = kwargs.get('access_token') callback = kwargs.get('callback') logging.info("QUERY REQUEST %s %s ", fbid,cursor) if user == None : # Create/save user if does exist wdal = WriteDAL() user = User(fbid) user.setIndexTime(datetime.datetime.now().isoformat()) user.setCreated(datetime.datetime.now().isoformat()) user.setIndexing(1) user.setAccessToken(access_token) wdal.persistUser(user) #Prep user account (create index location, etc) user.prepFirstUse() #handles getting search hits when we get them and returning with partial results qw = QueryWaiter(callback, cursor, search_id) qw.start() #create capsule capsule = StateCapsule(cursor_handler=CursorHandler(cursor=cursor,searchid=search_id,callback=callback), query=query, user=user) CapsuleManager().addCapsule(capsule) #start fetch pipeline fp = FetchPipeline(capsule) #AsyncQueryHandler.logger.debug('FULL INDEXING CYCLE STARTED') fp.start() else : #this is a returning user #check to see if this user has already searched for this id (memcache) search_result = rdal.getSearchID(search_id) if search_result != None : #This is an existing query from the user that we still have in the cache #return the next page of results r = CursorHandler.getPageForSearchResults(search_result, cursor) if user.getIndexing() == 0: #this signifies we really have no more results to return callback(r, cursor, 1) else : #handles getting search hits when we get them and returning with partial results qw = QueryWaiter(callback, cursor, search_id) qw.start() #also need to kick off a query pipeline here so we start getting results asap else : qw = QueryWaiter(callback, cursor, search_id) qw.start() #brand new query for an existing user capsule = StateCapsule(cursor_handler=CursorHandler(cursor=cursor,searchid=search_id,callback=callback), query=query, user=user) CapsuleManager().addCapsule(capsule) qr = QueryPipeline(capsule) qr.start() #check the index time. if the latest time we've queried this user's data is > THRESHOLD , kick off fetching + indexing from a certain point in time