Пример #1
0
    def _get_current_presence_state_from_memcache(self):
        client_id = self.key.id()
        client_memcache_key = CLIENT_PRESENCE_MEMCACHE_PREFIX + client_id
        serialized_dict = memcache.get(client_memcache_key)
        presence_state_name = None

        if serialized_dict:
            client_memcache_dict = pickle.loads(serialized_dict)
            # logging.debug('Pulled presence states %s from memcache key: %s' % (client_memcache_dict['presence_state_name'],
            # client_memcache_key))

            # memcache is active, therefore we make sure that the client presence status has been updated recently,
            # otherwise the client will be considered PRESENCE_OFFLINE
            if client_memcache_dict['stored_datetime'] + \
                    datetime.timedelta(seconds=HEARTBEAT_INTERVAL_TIMEOUT_SECONDS) < datetime.datetime.utcnow():

                logging.debug(
                    'Client %s is set to PRESENCE_OFFLINE due to timeout (memcache check)'
                    % client_id)
                presence_state_name = 'PRESENCE_OFFLINE'
            else:
                presence_state_name = client_memcache_dict[
                    'presence_state_name']

        return presence_state_name
Пример #2
0
    def get_dict_of_client_objects(self, recompute_members_from_scratch):
        """
         Get a list of objects corresponding to each client that is in the chat room.

         Parameters:
         recompute_members_from_scratch -- if this is true, then we will not attempt to pull the dictionary from memcache.
             We should set this to True in cases where we know that the list of room members has changed, and needs to
             be updated from the database. 
         """

        dict_of_client_objects_memcache_key = DICT_OF_CLIENT_OBJECTS_MEMCACHE_PREFIX + str(self.key.id())

        if not recompute_members_from_scratch:
            serialized_dict_of_client_objects = memcache.get(dict_of_client_objects_memcache_key)
        else:
            serialized_dict_of_client_objects = None


        # If we successfully pulled the dictionary out of memcache, then we return the value we pulled
        if serialized_dict_of_client_objects:
            # logging.debug('Pulled serialized_dict_of_client_objects from memcache')
            dict_of_client_objects = pickle.loads(serialized_dict_of_client_objects)
            return dict_of_client_objects

        else:
            # logging.debug('Computing dict_of_client_objects')
            dict_of_client_objects = {}
            for client_id in self.room_members_client_ids:

                try:
                    client_obj = clients.ClientModel.get_by_id(id=client_id)
                    # logging.debug('Getting presence state for client_obj %s' % client_obj)
                    presence_state_name = client_obj.get_current_presence_state()
                except:
                    # We were not able to get a client object corresponding to the client_id, therefore
                    # it may have been removed from the database already. In this case, make sure that the
                    # client is also removed from the room by setting them to offline.
                    presence_state_name = 'PRESENCE_OFFLINE'

                # If client is OFFLINE, then don't include them in dict_of_client_objects *and* also remove the client
                # from the room (if they later start their heartbeat, then they will be added back to the room)
                if presence_state_name == 'PRESENCE_OFFLINE':
                    logging.warning('*** Removing client_id %s from room %s due to PRESENCE_OFFLINE.' % (client_id, self.key.id()))
                    self.txn_remove_client_from_room(client_id)

                # Client is not PRESENCE_OFFLINE, include them in dict_of_client_objects
                else:
                    # Send relevant data to the client, which includes the client_id, username, and presence state.
                    user_obj = client_obj.user_obj_key.get()
                    dict_of_client_objects[client_id] =  {
                        'usernameNormalized': user_obj.username_normalized,
                        'usernameAsWritten': user_obj.username_as_written,
                        'presenceStateName': presence_state_name
                        }

            # Store the dictionary to memcache
            serialized_dict_of_client_objects = pickle.dumps(dict_of_client_objects, constants.pickle_protocol)
            memcache.set(dict_of_client_objects_memcache_key, serialized_dict_of_client_objects, DICT_OF_CLIENT_OBJECTS_MEMCACHE_EXPIRY_IN_SECONDS)

            return dict_of_client_objects
Пример #3
0
    def _get_current_presence_state_from_memcache(self):
        client_id = self.key.id()
        client_memcache_key = CLIENT_PRESENCE_MEMCACHE_PREFIX + client_id
        serialized_dict = memcache.get(client_memcache_key)
        presence_state_name = None

        if serialized_dict:
            client_memcache_dict = pickle.loads(serialized_dict)
            # logging.debug('Pulled presence states %s from memcache key: %s' % (client_memcache_dict['presence_state_name'],
            # client_memcache_key))

            # memcache is active, therefore we make sure that the client presence status has been updated recently,
            # otherwise the client will be considered PRESENCE_OFFLINE
            if client_memcache_dict['stored_datetime'] + \
                    datetime.timedelta(seconds=HEARTBEAT_INTERVAL_TIMEOUT_SECONDS) < datetime.datetime.utcnow():

                logging.debug('Client %s is set to PRESENCE_OFFLINE due to timeout (memcache check)' % client_id)
                presence_state_name = 'PRESENCE_OFFLINE'
            else:
                presence_state_name = client_memcache_dict['presence_state_name']


        return presence_state_name