def _listen(self, task_id): # task_ids start at zero, so we gobble up ports starting at the base port and work up port_no = int(config.get("api_base_port")) + task_id # Log according to configured directory and port # we're operating on log_file = "%s/rw_api_%s.log" % (config.get("api_log_dir"), port_no) if config.test_mode and os.path.exists(log_file): os.remove(log_file) log.init(log_file, config.get("log_level")) log.debug("start", "Server booting, port %s." % port_no) db.open() cache.open() for sid in config.station_ids: cache.update_local_cache_for_sid(sid) # If we're not in developer, remove development-related URLs if not config.get("developer_mode"): i = 0 while (i < len(request_classes)): if request_classes[i][0].find("/test/") != -1: request_classes.pop(i) i = i - 1 i = i + 1 # Make sure all other errors get handled in an API-friendly way request_classes.append((r".*", api.web.Error404Handler)) # Initialize the help (rather than it scan all URL handlers every time someone hits it) api.help.sectionize_requests() # Initialize playlist variables playlist.prepare_cooldown_algorithm(sid) # Fire ze missiles! app = tornado.web.Application( request_classes, debug=(config.test_mode or config.get("developer_mode")), template_path=os.path.join(os.path.dirname(__file__), "../templates"), static_path=os.path.join(os.path.dirname(__file__), "../static"), autoescape=None) http_server = tornado.httpserver.HTTPServer(app, xheaders=True) http_server.listen(port_no) if config.get("api_user") and config.get("api_group"): chuser.change_user(config.get("api_user"), config.get("api_group")) for request in request_classes: log.debug("start", " Handler: %s" % str(request)) log.info("start", "API server bootstrapped and ready to go.") self.ioloop = tornado.ioloop.IOLoop.instance() try: self.ioloop.start() finally: self.ioloop.stop() http_server.stop() db.close() log.info("stop", "Server has been shutdown.") log.close()
def test_check_song_for_conflict(self): db.c.update("DELETE FROM r4_listeners") db.c.update("DELETE FROM r4_request_store") e = Election.create(1) self.assertEqual(False, e._check_song_for_conflict(self.song1)) u = User(2) u.authorize(sid=1, ip_address=None, api_key=None, bypass=True) self.assertEqual(1, u.put_in_request_line(1)) # TODO: Use proper request/user methods here instead of DB call db.c.update( "UPDATE r4_request_line SET line_top_song_id = %s, line_expiry_tune_in = %s WHERE user_id = %s", (self.song1.id, int(time.time()) + 9999, u.id)) db.c.update( "INSERT INTO r4_listeners (sid, user_id, listener_icecast_id) VALUES (1, %s, 1)", (u.id, )) db.c.update( "INSERT INTO r4_request_store (user_id, song_id, sid) VALUES (%s, %s, 1)", (u.id, self.song1.id)) request.update_cache(1) request.update_expire_times() cache.update_local_cache_for_sid(1) self.assertEqual(True, e._check_song_for_conflict(self.song1)) self.assertEqual(True, e._check_song_for_conflict(self.song5)) self.assertEqual(event.ElecSongTypes.conflict, self.song5.data['entry_type']) self.assertEqual(event.ElecSongTypes.request, self.song1.data['entry_type'])
def _listen(self, task_id): # task_ids start at zero, so we gobble up ports starting at the base port and work up port_no = int(config.get("api_base_port")) + task_id # Log according to configured directory and port # we're operating on log_file = "%s/rw_api_%s.log" % (config.get("api_log_dir"), port_no) if config.test_mode and os.path.exists(log_file): os.remove(log_file) log.init(log_file, config.get("log_level")) log.debug("start", "Server booting, port %s." % port_no) db.open() cache.open() for sid in config.station_ids: cache.update_local_cache_for_sid(sid) # If we're not in developer, remove development-related URLs if not config.get("developer_mode"): i = 0 while i < len(request_classes): if request_classes[i][0].find("/test/") != -1: request_classes.pop(i) i = i - 1 i = i + 1 # Make sure all other errors get handled in an API-friendly way request_classes.append((r".*", api.web.Error404Handler)) # Initialize the help (rather than it scan all URL handlers every time someone hits it) api.help.sectionize_requests() # Initialize playlist variables playlist.prepare_cooldown_algorithm(sid) # Fire ze missiles! app = tornado.web.Application( request_classes, debug=(config.test_mode or config.get("developer_mode")), template_path=os.path.join(os.path.dirname(__file__), "../templates"), static_path=os.path.join(os.path.dirname(__file__), "../static"), autoescape=None, ) http_server = tornado.httpserver.HTTPServer(app, xheaders=True) http_server.listen(port_no) if config.get("api_user") and config.get("api_group"): chuser.change_user(config.get("api_user"), config.get("api_group")) for request in request_classes: log.debug("start", " Handler: %s" % str(request)) log.info("start", "API server bootstrapped and ready to go.") self.ioloop = tornado.ioloop.IOLoop.instance() try: self.ioloop.start() finally: self.ioloop.stop() http_server.stop() db.close() log.info("stop", "Server has been shutdown.") log.close()
def _on_zmq(messages): global votes_by global last_vote_by for message in messages: try: message = json.loads(message) except Exception as e: log.exception("zeromq", "Error decoding ZeroMQ message.", e) return if not "action" in message or not message["action"]: log.critical("zeromq", "No action received from ZeroMQ.") try: if message["action"] == "result_sync": sessions[message["sid"]].send_to_user( message["user_id"], message["uuid_exclusion"], message["data"] ) elif message["action"] == "live_voting": sessions[message["sid"]].send_to_all( message["uuid_exclusion"], message["data"] ) delay_live_vote_removal(message["sid"]) elif message["action"] == "delayed_live_voting": if not delayed_live_vote_timers[message["sid"]]: delay_live_vote(message) delayed_live_vote[message["sid"]] = message elif message["action"] == "update_all": delay_live_vote_removal(message["sid"]) rainwave.playlist.update_num_songs() rainwave.playlist.prepare_cooldown_algorithm(message["sid"]) cache.update_local_cache_for_sid(message["sid"]) sessions[message["sid"]].update_all(message["sid"]) votes_by = {} last_vote_by = {} elif message["action"] == "update_ip": for sid in sessions: sessions[sid].update_ip_address(message["ip"]) elif message["action"] == "update_listen_key": for sid in sessions: sessions[sid].update_listen_key(message["listen_key"]) elif message["action"] == "update_user": for sid in sessions: sessions[sid].update_user(message["user_id"]) elif message["action"] == "update_dj": sessions[message["sid"]].update_dj() elif message["action"] == "ping": log.debug("zeromq", "Pong") elif message["action"] == "vote_by": votes_by[message["by"]] = ( votes_by[message["by"]] + 1 if message["by"] in votes_by else 1 ) last_vote_by[message["by"]] = timestamp() except Exception as e: log.exception( "zeromq", "Error handling Zero MQ action '%s'" % message["action"], e ) return
def on_finish(self): if not self._rw_update_clients: return cache.update_local_cache_for_sid(self.sid) for session in sessions[self.sid]: session.update(True) sessions[self.sid] = []
def on_finish(self): if not self.request_ok: log.debug("sync_update_all", "sync_update_all request was not OK.") return log.debug("sync_update_all", "Updating all sessions for sid %s" % self.sid) cache.update_local_cache_for_sid(self.sid) if self.sid in sessions: for session in sessions[self.sid]: session.update(True) sessions[self.sid] = []
def on_finish(self): global sessions if not self.get_status() == 200: log.debug("sync_update_all", "sync_update_all request was not OK.") return log.debug("sync_update_all", "Updating all sessions for sid %s" % self.sid) rainwave.playlist.update_num_songs() rainwave.playlist.prepare_cooldown_algorithm(self.sid) cache.update_local_cache_for_sid(self.sid) sessions[self.sid].update_all(self.sid) super(SyncUpdateAll, self).on_finish()
def _on_zmq(messages): global votes_by global last_vote_by for message in messages: try: message = json.loads(message) except Exception as e: log.exception("zeromq", "Error decoding ZeroMQ message.", e) return if not 'action' in message or not message['action']: log.critical("zeromq", "No action received from ZeroMQ.") try: if message['action'] == "result_sync": sessions[message['sid']].send_to_user(message['user_id'], message['uuid_exclusion'], message['data']) elif message['action'] == "live_voting": sessions[message['sid']].send_to_all(message['uuid_exclusion'], message['data']) delay_live_vote_removal(message['sid']) elif message['action'] == "delayed_live_voting": if not delayed_live_vote_timers[message['sid']]: delay_live_vote(message) delayed_live_vote[message['sid']] = message elif message['action'] == "update_all": delay_live_vote_removal(message['sid']) rainwave.playlist.update_num_songs() rainwave.playlist.prepare_cooldown_algorithm(message['sid']) cache.update_local_cache_for_sid(message['sid']) sessions[message['sid']].update_all(message['sid']) votes_by = {} last_vote_by = {} elif message['action'] == "update_ip": for sid in sessions: sessions[sid].update_ip_address(message['ip']) elif message['action'] == "update_listen_key": for sid in sessions: sessions[sid].update_listen_key(message['listen_key']) elif message['action'] == "update_user": for sid in sessions: sessions[sid].update_user(message['user_id']) elif message['action'] == "update_dj": sessions[message['sid']].update_dj() elif message['action'] == "ping": log.debug("zeromq", "Pong") elif message['action'] == "vote_by": votes_by[message['by']] = votes_by[message['by']] + 1 if message['by'] in votes_by else 1 last_vote_by[message['by']] = timestamp() except Exception as e: log.exception("zeromq", "Error handling Zero MQ action '%s'" % message['action'], e) return
def on_finish(self): if not self.get_status() == 200: log.debug("sync_update_all", "sync_update_all request was not OK.") return log.debug("sync_update_all", "Updating all sessions for sid %s" % self.sid) cache.update_local_cache_for_sid(self.sid) session_count = 0 if self.sid in sessions: for session in sessions[self.sid]: session_count += 1 session.update() log.debug("sync_update_all", "Updated %s sessions for sid %s." % (session_count, self.sid)) sessions[self.sid] = []
def test_check_song_for_conflict(self): db.c.update("DELETE FROM r4_listeners") db.c.update("DELETE FROM r4_request_store") e = Election.create(1) self.assertEqual(False, e._check_song_for_conflict(self.song1)) u = User(2) u.authorize(1, None, None, True) self.assertEqual(1, u.put_in_request_line(1)) # TODO: Use proper request/user methods here instead of DB call db.c.update("UPDATE r4_request_line SET line_top_song_id = %s, line_expiry_tune_in = %s WHERE user_id = %s", (self.song1.id, time.time()+9999, u.id)) db.c.update("INSERT INTO r4_listeners (sid, user_id, listener_icecast_id) VALUES (1, %s, 1)", (u.id,)) db.c.update("INSERT INTO r4_request_store (user_id, song_id, sid) VALUES (%s, %s, 1)", (u.id, self.song1.id)) request.update_cache(1) cache.update_local_cache_for_sid(1) self.assertEqual(True, e._check_song_for_conflict(self.song1)) self.assertEqual(False, e._check_song_for_conflict(self.song5)) self.assertEqual(event.ElecSongTypes.conflict, self.song5.data['entry_type']) self.assertEqual(event.ElecSongTypes.request, self.song1.data['entry_type'])
def _listen(self, task_id): # task_ids start at zero, so we gobble up ports starting at the base port and work up port_no = int(config.get("api_base_port")) + task_id # Log according to configured directory and port # we're operating on log_file = "%s/rw_api_%s.log" % (config.get("api_log_dir"), port_no) if config.test_mode and os.path.exists(log_file): os.remove(log_file) log.init(log_file, config.get("log_level")) log.debug("start", "Server booting, port %s." % port_no) db.open() cache.open() for sid in config.station_ids: cache.update_local_cache_for_sid(sid) # Fire ze missiles! app = tornado.web.Application(request_classes, debug=config.get("debug_mode"), template_path=os.path.join(os.path.dirname(__file__), "../templates")) http_server = tornado.httpserver.HTTPServer(app, xheaders = True) http_server.listen(port_no) if config.get("api_user") and config.get("api_group"): chuser.change_user(config.get("api_user"), config.get("api_group")) for request in request_classes: log.debug("start", " Handler: %s" % str(request)) log.info("start", "Server bootstrapped and ready to go.") self.ioloop = tornado.ioloop.IOLoop.instance() try: self.ioloop.start() finally: self.ioloop.stop() http_server.stop() db.close() log.info("stop", "Server has been shutdown.") log.close()
def _listen(self, task_id): zeromq.init_pub() zeromq.init_sub() import api_requests.sync api_requests.sync.init() # task_ids start at zero, so we gobble up ports starting at the base port and work up port_no = int(config.get("api_base_port")) + task_id pid = os.getpid() pid_file = open("%s/api_%s.pid" % (config.get_directory("pid_dir"), port_no), 'w') pid_file.write(str(pid)) pid_file.close() # Log according to configured directory and port # we're operating on log_file = "%s/rw_api_%s.log" % (config.get_directory("log_dir"), port_no) if config.test_mode and os.path.exists(log_file): os.remove(log_file) log.init(log_file, config.get("log_level")) log.debug("start", "Server booting, port %s." % port_no) db.connect() cache.connect() memory_trace.setup(port_no) api.locale.load_translations() api.locale.compile_static_language_files() if config.get("web_developer_mode"): for station_id in config.station_ids: playlist.prepare_cooldown_algorithm(station_id) # automatically loads every station ID and fills things in if there's no data schedule.load() for station_id in config.station_ids: schedule.update_memcache(station_id) rainwave.request.update_line(station_id) rainwave.request.update_expire_times() cache.set_station(station_id, "backend_ok", True) cache.set_station(station_id, "backend_message", "OK") cache.set_station(station_id, "get_next_socket_timeout", False) for sid in config.station_ids: cache.update_local_cache_for_sid(sid) playlist.prepare_cooldown_algorithm(sid) playlist.update_num_songs() # If we're not in developer, remove development-related URLs if not config.get("developer_mode"): i = 0 while (i < len(request_classes)): if request_classes[i][0].find("/test/") != -1: request_classes.pop(i) i = i - 1 i = i + 1 # Make sure all other errors get handled in an API-friendly way request_classes.append((r"/api/.*", api.web.Error404Handler)) request_classes.append((r"/api4/.*", api.web.Error404Handler)) request_classes.append((r".*", api.web.HTMLError404Handler)) # Initialize the help (rather than it scan all URL handlers every time someone hits it) api.help.sectionize_requests() # Fire ze missiles! global app app = tornado.web.Application(request_classes, debug=(config.test_mode or config.get("developer_mode")), template_path=os.path.join(os.path.dirname(__file__), "../templates"), static_path=os.path.join(os.path.dirname(__file__), "../static"), autoescape=None) http_server = tornado.httpserver.HTTPServer(app, xheaders = True) http_server.listen(port_no) if config.get("api_user") and config.get("api_group"): chuser.change_user(config.get("api_user"), config.get("api_group")) for request in request_classes: log.debug("start", " Handler: %s" % str(request)) log.info("start", "API server on port %s ready to go." % port_no) self.ioloop = tornado.ioloop.IOLoop.instance() try: self.ioloop.start() finally: self.ioloop.stop() http_server.stop() db.close() log.info("stop", "Server has been shutdown.") log.close()
def _listen(self, task_id): import api_requests.sync api_requests.sync.init() # task_ids start at zero, so we gobble up ports starting at the base port and work up port_no = int(config.get("api_base_port")) + task_id pid = os.getpid() pid_file = open( "%s/api_%s.pid" % (config.get_directory("pid_dir"), port_no), 'w') pid_file.write(str(pid)) pid_file.close() # Log according to configured directory and port # we're operating on log_file = "%s/rw_api_%s.log" % (config.get_directory("log_dir"), port_no) if config.test_mode and os.path.exists(log_file): os.remove(log_file) log.init(log_file, config.get("log_level")) log.debug("start", "Server booting, port %s." % port_no) db.connect() cache.connect() memory_trace.setup(port_no) if config.get("web_developer_mode"): for station_id in config.station_ids: playlist.prepare_cooldown_algorithm(station_id) # automatically loads every station ID and fills things in if there's no data schedule.load() for station_id in config.station_ids: schedule.update_memcache(station_id) rainwave.request.update_line(station_id) rainwave.request.update_expire_times() cache.set_station(station_id, "backend_ok", True) cache.set_station(station_id, "backend_message", "OK") cache.set_station(station_id, "get_next_socket_timeout", False) for sid in config.station_ids: cache.update_local_cache_for_sid(sid) playlist.prepare_cooldown_algorithm(sid) playlist.update_num_songs() # If we're not in developer, remove development-related URLs if not config.get("developer_mode"): i = 0 while (i < len(request_classes)): if request_classes[i][0].find("/test/") != -1: request_classes.pop(i) i = i - 1 i = i + 1 # Make sure all other errors get handled in an API-friendly way request_classes.append((r"/api/.*", api.web.Error404Handler)) request_classes.append((r"/api4/.*", api.web.Error404Handler)) request_classes.append((r".*", api.web.HTMLError404Handler)) # Initialize the help (rather than it scan all URL handlers every time someone hits it) api.help.sectionize_requests() # Fire ze missiles! app = tornado.web.Application( request_classes, debug=(config.test_mode or config.get("developer_mode")), template_path=os.path.join(os.path.dirname(__file__), "../templates"), static_path=os.path.join(os.path.dirname(__file__), "../static"), autoescape=None) http_server = tornado.httpserver.HTTPServer(app, xheaders=True) http_server.listen(port_no) if config.get("api_user") and config.get("api_group"): chuser.change_user(config.get("api_user"), config.get("api_group")) if task_id == 0: buildtools.bake_css() buildtools.bake_js() buildtools.bake_beta_js() for request in request_classes: log.debug("start", " Handler: %s" % str(request)) log.info("start", "API server on port %s ready to go." % port_no) self.ioloop = tornado.ioloop.IOLoop.instance() try: self.ioloop.start() finally: self.ioloop.stop() http_server.stop() db.close() log.info("stop", "Server has been shutdown.") log.close()
def _listen(self, task_id): zeromq.init_pub() zeromq.init_sub() import api_requests.sync api_requests.sync.init() # task_ids start at zero, so we gobble up ports starting at the base port and work up port_no = int(config.get("api_base_port")) + task_id # Log according to configured directory and port # we're operating on log_file = "%s/rw_api_%s.log" % (config.get_directory("log_dir"), port_no) log.init(log_file, config.get("log_level")) log.debug("start", "Server booting, port %s." % port_no) db.connect(auto_retry=False, retry_only_this_time=True) cache.connect() memory_trace.setup(port_no) api.locale.load_translations() api.locale.compile_static_language_files() if config.get("developer_mode"): for station_id in config.station_ids: playlist.prepare_cooldown_algorithm(station_id) # automatically loads every station ID and fills things in if there's no data schedule.load() for station_id in config.station_ids: schedule.update_memcache(station_id) rainwave.request.update_line(station_id) rainwave.request.update_expire_times() cache.set_station(station_id, "backend_ok", True) cache.set_station(station_id, "backend_message", "OK") cache.set_station(station_id, "get_next_socket_timeout", False) for sid in config.station_ids: cache.update_local_cache_for_sid(sid) playlist.prepare_cooldown_algorithm(sid) playlist.update_num_songs() # If we're not in developer, remove development-related URLs if not config.get("developer_mode"): i = 0 while i < len(request_classes): if request_classes[i][0].find("/test/") != -1: request_classes.pop(i) i = i - 1 i = i + 1 # Make sure all other errors get handled in an API-friendly way request_classes.append((r"/api/.*", api.web.Error404Handler)) request_classes.append((r"/api4/.*", api.web.Error404Handler)) request_classes.append((r".*", api.web.HTMLError404Handler)) # Initialize the help (rather than it scan all URL handlers every time someone hits it) api.help.sectionize_requests() # Fire ze missiles! global app debug = config.get("developer_mode") app = tornado.web.Application( request_classes, debug=debug, template_path=os.path.join(os.path.dirname(__file__), "../templates"), static_path=os.path.join(os.path.dirname(__file__), "../static"), autoescape=None, autoreload=debug, serve_traceback=debug, ) http_server = tornado.httpserver.HTTPServer(app, xheaders=True) http_server.listen(port_no) for request in request_classes: log.debug("start", " Handler: %s" % str(request)) log.info("start", "API server on port %s ready to go." % port_no) self.ioloop = tornado.ioloop.IOLoop.instance() db_keepalive = tornado.ioloop.PeriodicCallback(db.connection_keepalive, 10000) db_keepalive.start() try: self.ioloop.start() finally: self.ioloop.stop() http_server.stop() db.close() log.info("stop", "Server has been shutdown.") log.close()