def remove_from_sessions(self, channel_id): # Get session session = Session.get_by_key_name(channel_id) extended_session = None if(session): session.ended = datetime.utcnow() session.put() logging.info("Session ended in datastore") # Init listener listener = session.listener # Init extended session extended_session = Session.get_extended_session(session, listener) new_sessions = [] for s in self.sessions: if s["key_name"] != channel_id: new_sessions.append(s) memcache.set(self._memcache_station_sessions_id, new_sessions) logging.info("Session removed from memcache") # Online status becomes false if listener = host + no other host listening if(listener and listener.key().name() == Session.host.get_value_for_datastore(session).name()): logging.info("Admin leaves") still_some_admins = False for s in new_sessions: if(s["listener_key_name"] == str(listener.key().name())): still_some_admins = True break if not still_some_admins: self.station.online = False self.station.put() logging.info("Station updated in datastore") memcache.set(self._memcache_station_id, self.station) logging.info("Station updated in memcache") else: logging.info("Still some admins online") return extended_session
def sessions(self): if not hasattr(self, "_sessions"): self._sessions = memcache.get(self._memcache_station_sessions_id) if self._sessions is None: logging.info("Sessions not in memcache") q = Session.all() q.filter("host", self.station.key()) q.filter("ended", None) q.filter("created >", datetime.utcnow() - timedelta(0,7200)) sessions = q.fetch(100) extended_sessions = Session.get_extended_sessions(sessions) memcache.set(self._memcache_station_sessions_id, extended_sessions) logging.info("Sessions put in memcache") self._sessions = extended_sessions else: logging.info("Sessions already in memcache") return self._sessions
def post(self): shortname = self.request.get("shortname") station_proxy = StationApi(shortname) station = station_proxy.station output = {} if(station): # Increment visits counter station_proxy.increment_visits_counter() # Channel ID and token generation time_now = str(timegm(gmtime())) random_integer = str(randrange(1000)) new_channel_id = shortname + "." + time_now + random_integer new_channel_token = channel.create_channel(new_channel_id) listener_key = None if(self.user_proxy): listener_key = self.user_proxy.user.profile.key() # Put new session in datastore new_session = Session( key_name = new_channel_id, channel_token = new_channel_token, listener = listener_key, host = station.key(), ) new_session.put() logging.info("New session saved in datastore") output = { "channel_id": new_channel_id, "channel_token": new_channel_token, } self.response.out.write(json.dumps(output)) else: self.error(404)
def add_to_sessions(self, channel_id): # Get session session = Session.get_by_key_name(channel_id) extended_session = None if(session): # After a reconnection the session may have ended. Correct it. if session.ended is not None: session.ended = None session.put() logging.info("Session had ended (probable reconnection). Corrected session put.") # Init listener listener = session.listener # Init extended session extended_session = Session.get_extended_session(session, listener) new_sessions = self.sessions new_sessions.append(extended_session) memcache.set(self._memcache_station_sessions_id, new_sessions) logging.info("Session added in memcache") # Online status becomes true if listener = host if(listener and listener.key().name() == Session.host.get_value_for_datastore(session).name()): logging.info("Admin joins") self.station.online = True self.station.active = datetime.utcnow() self.station.put() logging.info("Station updated in datastore") memcache.set(self._memcache_station_id, self.station) logging.info("Station updated in memcache") return extended_session