def get_producer_at_time(sid, at_time): to_ret = None local_time = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(at_time)) time_ahead = int((at_time - timestamp()) / 60) sched_id = db.c.fetch_var( "SELECT sched_id " "FROM r4_schedule " "WHERE sid = %s AND sched_start <= %s AND sched_end > %s " "ORDER BY sched_id DESC " "LIMIT 1", (sid, at_time + 20, at_time)) try: to_ret = events.event.BaseProducer.load_producer_by_id(sched_id) if to_ret: to_ret.start_producer() except Exception as e: log.warn( "get_producer", "Failed to obtain producer at time %s (%sm ahead)." % (local_time, time_ahead)) log.exception( "get_producer", "Failed to get an appropriate producer at time %s (%sm ahead)." % (local_time, time_ahead), e) if not to_ret: log.debug( "get_producer", "No producer at time %s (%sm ahead), defaulting to election." % (local_time, time_ahead)) return election.ElectionProducer(sid) if not to_ret.has_next_event(): log.warn( "get_producer", "Producer ID %s (type %s, %s) has no events." % (to_ret.id, to_ret.type, to_ret.name)) return election.ElectionProducer(sid) return to_ret
def get_event_in_progress(sid): producer = get_current_producer(sid) evt = producer.load_event_in_progress() if not evt: producer = election.ElectionProducer(sid) evt = producer.load_event_in_progress() return evt
def _get_or_create_election(sid, target_length=None): max_sched_id, max_elec_id, num_elections, next_end_time = _get_schedule_stats( sid) ep = election.ElectionProducer(sid) return ep.load_next_event(target_length=target_length, min_elec_id=max_elec_id)
def get_producer_at_time(sid, at_time): to_ret = None sched_id = db.c.fetch_var( "SELECT sched_id " "FROM r4_schedule " "WHERE sid = %s AND sched_start <= %s AND sched_end > %s " "ORDER BY sched_id DESC " "LIMIT 1", (sid, at_time + 20, at_time)) try: to_ret = events.event.BaseProducer.load_producer_by_id(sched_id) except Exception as e: log.warn("get_producer", "Failed to obtain producer.") log.exception("get_producer", "Failed to get an appropriate producer.", e) if not to_ret: return election.ElectionProducer(sid) if not to_ret.has_next_event(): return election.ElectionProducer(sid) return to_ret
def manage_next(sid): max_sched_id, max_elec_id, num_elections, max_future_time = _get_schedule_stats(sid) now_producer = get_producer_at_time(sid, time.time()) next_producer = get_producer_at_time(sid, max_future_time) nextnext_producer_start = db.c.fetch_var("SELECT sched_start FROM r4_schedule WHERE sid = %s AND sched_used = FALSE AND sched_start > %s AND sched_timed = TRUE", (sid, max_future_time)) time_to_future_producer = None if nextnext_producer_start: time_to_future_producer = nextnext_producer_start - max_future_time else: time_to_future_producer = 86400 while len(upnext[sid]) < min(now_producer.plan_ahead_limit, next_producer.plan_ahead_limit): target_length = None if time < 20: log.debug("timing", "SID %s <20 seconds to upnext event, not using timing." % sid) if time_to_future_producer < 40: target_length = time_to_future_producer next_producer = rainwave.events.shortest_election.ShortestElectionProducer(sid) log.debug("timing", "SID %s <40 seconds to upnext event, using shortest elections." % sid) elif time_to_future_producer < (playlist.get_average_song_length(sid) * 1.3): target_length = time_to_future_producer log.debug("timing", "SID %s close to event, timing to %s seconds long." % (sid, target_length)) elif time_to_future_producer < (playlist.get_average_song_length(sid) * 2.2): target_length = playlist.get_average_song_length(sid) log.debug("timing", "SID %s has an upcoming event, timing to %s seconds long." % (sid, target_length)) next_event = next_producer.load_next_event(target_length, max_elec_id) if not next_event: log.info("manage_next", "Producer ID %s type %s did not produce an event." % (next_producer.id, next_producer.type)) next_producer = election.ElectionProducer(sid) next_event = next_producer.load_next_event(target_length, max_elec_id) upnext[sid].append(next_event) if next_event.is_election: num_elections += 1 if next_event.is_election and next_event.id > max_elec_id: max_elec_id = next_event.id max_future_time += upnext[sid][-1].length() time_to_future_producer -= upnext[sid][-1].length() next_producer = get_producer_at_time(sid, max_future_time) future_time = None if current[sid].start: future_time = current[sid].start + current[sid].length() else: future_time = int(time.time() + current[sid].length()) for evt in upnext[sid]: evt.start = future_time future_time += evt.length()