def test_scheduler(self): # We need a completely blank slate for this db.c.update("DELETE FROM r4_schedule") db.c.update("DELETE FROM r4_elections") # A hundred fake songs to fill our range out if we're not running in test mode or running in sqlite if config.get("db_type") != "postgres" and not config.test_mode: db.c.update("DELETE FROM r4_songs") for i in range(0, 100): playlist.Song.create_fake(1) reset_schedule(1) # First test: # Create an event 5 minutes from now (the fake songs created above are all 60 seconds long) # Load the schedule, then watch the predicted start times of each election # The elections should work around the event properly schedule.load() # Second test: # Cycle through the elections and make sure the event gets played properly for i in range(0, 10): schedule.advance_station(1) schedule.post_process(1) schedule.current[1].to_dict() # Third test: # Reset the schedule, fill with elections, then create an event that is supposed to happen # between the elections already created in next. Advance the schedule. # Observe the start times. # Fourth test: # Reset the schedule, fill with elections, then add a 1up. # The 1up should play as soon as you advance the schedule.
def get(self, sid): self.success = False self.sid = None if int(sid) in config.station_ids: self.sid = int(sid) else: return try: schedule.advance_station(self.sid) except psycopg2.extensions.TransactionRollbackError as e: if not self.retried: self.retried = True log.warn( "backend", "Database transaction deadlock. Re-opening database and setting retry timeout." ) db.close() db.open() tornado.ioloop.IOLoop.instance().add_timeout( datetime.timedelta(milliseconds=350), self.get) else: raise if not config.get("liquidsoap_annotations"): self.write(schedule.get_current_file(self.sid)) else: self.write( self._get_annotated(schedule.get_current_event(self.sid))) self.success = True
def get(self, sid): self.success = False self.sid = None if int(sid) in config.station_ids: self.sid = int(sid) else: return try: schedule.advance_station(self.sid) except psycopg2.extensions.TransactionRollbackError as e: if not self.retried: self.retried = True log.warn("backend", "Database transaction deadlock. Re-opening database and setting retry timeout.") db.close() db.open() tornado.ioloop.IOLoop.instance().add_timeout(datetime.timedelta(milliseconds=350), self.get) else: raise if not config.get("liquidsoap_annotations"): self.write(schedule.get_current_file(self.sid)) else: self.write(self._get_annotated(schedule.get_current_event(self.sid))) self.success = True
def get(self, sid): self.sid = None if int(sid) in config.station_ids: self.sid = int(sid) schedule.advance_station(self.sid) if not config.get("liquidsoap_annotations"): self.write(schedule.get_current_file(self.sid)) else: self.write(self._get_annotated(schedule.get_current_event(self.sid)))
def get(self, sid): #pylint: disable=W0221 self.success = False self.sid = None if int(sid) in config.station_ids: self.sid = int(sid) else: return if cache.get_station(self.sid, "backend_paused") and cache.get_station(self.sid, "backend_pause_extend"): self.write(self._get_pause_file()) cache.set_station(self.sid, "backend_pause_extend", False) cache.set_station(self.sid, "backend_paused_playing", True) return else: cache.set_station(self.sid, "backend_pause_extend", False) cache.set_station(self.sid, "backend_paused", False) cache.set_station(self.sid, "backend_paused_playing", False) # This program must be run on 1 station for 1 instance, which would allow this operation to be safe. # Also works if 1 process is serving all stations. Pinging any instance for any station # would break the program here, though. if cache.get_station(self.sid, "get_next_socket_timeout") and sid_output[self.sid]: log.warn("backend", "Using previous output to prevent flooding.") self.write(sid_output[self.sid]) sid_output[self.sid] = None self.success = True else: try: schedule.advance_station(self.sid) except (psycopg2.OperationalError, psycopg2.InterfaceError) as e: log.warn("backend", e.diag.message_primary) db.close() db.connect() raise except psycopg2.extensions.TransactionRollbackError as e: log.warn("backend", "Database transaction deadlock. Re-opening database and setting retry timeout.") db.close() db.connect() raise to_send = None if not config.get("liquidsoap_annotations"): to_send = schedule.get_advancing_file(self.sid) else: to_send = self._get_annotated(schedule.get_advancing_event(self.sid)) sid_output[self.sid] = to_send self.success = True if not cache.get_station(self.sid, "get_next_socket_timeout"): self.write(to_send)
def get(self, sid): self.success = False self.sid = None if int(sid) in config.station_ids: self.sid = int(sid) else: return if cache.get_station(self.sid, "backend_paused"): if not cache.get_station(self.sid, "dj_heartbeat_start"): log.debug("dj", "Setting server start heatbeat.") cache.set_station(self.sid, "dj_heartbeat_start", timestamp()) self.write(self._get_pause_file()) schedule.set_upnext_crossfade(self.sid, False) cache.set_station(self.sid, "backend_paused_playing", True) sync_to_front.sync_frontend_dj(self.sid) return else: cache.set_station(self.sid, "dj_heartbeat_start", False) cache.set_station(self.sid, "backend_paused", False) cache.set_station(self.sid, "backend_paused_playing", False) try: schedule.advance_station(self.sid) except (psycopg2.OperationalError, psycopg2.InterfaceError) as e: log.warn("backend", e.diag.message_primary) db.close() db.connect() raise except psycopg2.extensions.TransactionRollbackError as e: log.warn( "backend", "Database transaction deadlock. Re-opening database and setting retry timeout.", ) db.close() db.connect() raise to_send = None if not config.get("liquidsoap_annotations"): to_send = schedule.get_advancing_file(self.sid) else: to_send = self._get_annotated( schedule.get_advancing_event(self.sid)) self.success = True if not cache.get_station(self.sid, "get_next_socket_timeout"): self.write(to_send)
def get(self, sid): #pylint: disable=W0221 self.success = False self.sid = None if int(sid) in config.station_ids: self.sid = int(sid) else: return if cache.get_station(self.sid, "backend_paused"): if not cache.get_station(self.sid, "dj_heartbeat_start"): log.debug("dj", "Setting server start heatbeat.") cache.set_station(self.sid, "dj_heartbeat_start", timestamp()) self.write(self._get_pause_file()) schedule.set_upnext_crossfade(self.sid, False) cache.set_station(self.sid, "backend_paused_playing", True) sync_to_front.sync_frontend_dj(self.sid) return else: cache.set_station(self.sid, "dj_heartbeat_start", False) cache.set_station(self.sid, "backend_paused", False) cache.set_station(self.sid, "backend_paused_playing", False) try: schedule.advance_station(self.sid) except (psycopg2.OperationalError, psycopg2.InterfaceError) as e: log.warn("backend", e.diag.message_primary) db.close() db.connect() raise except psycopg2.extensions.TransactionRollbackError as e: log.warn("backend", "Database transaction deadlock. Re-opening database and setting retry timeout.") db.close() db.connect() raise to_send = None if not config.get("liquidsoap_annotations"): to_send = schedule.get_advancing_file(self.sid) else: to_send = self._get_annotated(schedule.get_advancing_event(self.sid)) self.success = True if not cache.get_station(self.sid, "get_next_socket_timeout"): self.write(to_send)
def get(self, sid): self.success = False self.sid = None if int(sid) in config.station_ids: self.sid = int(sid) else: return # This program must be run on 1 station for 1 instance, which would allow this operation to be safe. # Also works if 1 process is serving all stations. Pinging any instance for any station # would break the program here, though. if cache.get_station(self.sid, "get_next_socket_timeout") and sid_output[self.sid]: log.warn("backend", "Using previous output to prevent flooding.") self.write(sid_output[self.sid]) sid_output[self.sid] = None self.success = True else: try: schedule.advance_station(self.sid) except (psycopg2.OperationalError, psycopg2.InterfaceError) as e: log.warn("backend", e.diag.message_primary) db.close() db.connect() raise except psycopg2.extensions.TransactionRollbackError as e: log.warn("backend", "Database transaction deadlock. Re-opening database and setting retry timeout.") db.close() db.connect() raise to_send = None if not config.get("liquidsoap_annotations"): to_send = schedule.get_advancing_file(self.sid) else: to_send = self._get_annotated(schedule.get_advancing_event(self.sid)) sid_output[self.sid] = to_send self.success = True if not cache.get_station(self.sid, "get_next_socket_timeout"): self.write(to_send)
def get(self, sid): self.sid = None if int(sid) in config.station_ids: self.sid = int(sid) schedule.advance_station(sid) self.write(schedule.get_current_file())