def on_close(self): for topic in self._subs: self.pubsub.unsubscribe(topic, self._on_pubsub) with session() as db: user = self._get_user(db) ConnectionService(db, self, user).disconnect() metrics.CONNECTED_SOCKETS.dec()
def _prepare_db(): """Cleans DB state before starting the server.""" logging.info("Cleaning database ...") with session() as db: db.query(Connection).delete() db.query(Automatch).filter( Automatch.preset != 'correspondence').delete() db.query(User).update({'is_online': False}) GameService(db).resume_all_games()
def callback(): # Sleep for a random duration so that different processes don't all run at the same time. yield gen.sleep(random.random()) while True: with session() as db: socket = SocketMixin() socket.initialize(app.pubsub) svc = service(db, socket) getattr(svc, method)() yield gen.sleep(interval.total_seconds())
def _execute_service(socket, service, method, data, user_id): with metrics.EXCEPTIONS.labels(service + '/' + method).count_exceptions(): with session() as db: user = db.query(User).get(user_id) if user_id else None service_class = _service_names.get(service) if not service_class: raise ValueError('service "{}" not found'.format(service)) if user and method != 'ping': user.last_activity_at = datetime.utcnow() return service_class(db, socket, user).execute(method, data)
def main(): define_options() options.parse_command_line() settings.LISTEN_PORT += options.port_offset create_db() if options.prepare_startup: prepare_startup() elif options.create_room: with session() as db: RoomService(db).create_default_room(options.create_room) else: if settings.DEBUG: add_reload_hook(prepare_startup) run_app()
def _execute(self, *args, **kwargs): with session() as db: self.db = db super()._execute(*args, **kwargs)
def open(self): with session() as db: user = self._get_user(db) ConnectionService(db, self, user).connect() metrics.CONNECTED_SOCKETS.inc()