diff --git a/src/scheduling/api.py b/src/scheduling/api.py index 4046a2994b060cf83ca26c1dad81cce99203fe54..dfc99573f9128e51f359e2e74d6471d55d7aacb7 100644 --- a/src/scheduling/api.py +++ b/src/scheduling/api.py @@ -60,7 +60,6 @@ class ApiFetcher(threading.Thread): self.tank_playlist_url = self.config.get("api_tank_playlist") self.tank_session = self.config.get("api_tank_session") self.tank_secret = self.config.get("api_tank_secret") - self.queue = queue.Queue() self.stop_event = threading.Event() threading.Thread.__init__(self) @@ -94,6 +93,7 @@ class ApiFetcher(threading.Thread): return + # # METHODS # diff --git a/src/scheduling/programme.py b/src/scheduling/programme.py index 7060b85765267c7d5f5f6bcf64b6954b4202ffe1..037ea8c3495359d7f5cbc30acdf0f5f127d41a5b 100644 --- a/src/scheduling/programme.py +++ b/src/scheduling/programme.py @@ -55,6 +55,7 @@ class ProgrammeService(): self.programme_store = ProgrammeStore() + def refresh(self): """ Fetch the latest programme from `ProgrammeStore` which stores it to the database. @@ -68,7 +69,7 @@ class ProgrammeService(): self.api_fetcher = ApiFetcher(self.config) self.api_fetcher.start() response = self.api_fetcher.get_fetched_data() - + # Reset last successful fetch state lsf = self.last_successful_fetch self.last_successful_fetch = None @@ -77,18 +78,17 @@ class ProgrammeService(): msg = SU.red("Trying to load programme from Engine Database, because ApiFetcher returned an empty response.") self.logger.warning(msg) elif type(response) is list: - self.timeslots = response - if self.timeslots is not None and len(self.timeslots) > 0: - self.last_successful_fetch = datetime.now() + if len(response) > 0: + self.last_successful_fetch = datetime.now() self.programme_store.store_timeslots(response) - self.logger.info(SU.green(f"Finished fetching current programme from API ({len(response)})")) - if len(self.timeslots) == 0: + self.logger.info(SU.green(f"Finished fetching current programme from API ({len(response)} timeslots)")) + else: self.logger.critical("Programme fetched from Steering/Tank has no entries!") elif response.startswith("fetching_aborted"): - msg = SU.red("Trying to load programme from database only, because fetching was being aborted from ApiFetcher! Reason: ") - self.logger.warning(msg + response[16:]) + msg = SU.red("Load programme from DB, because fetching was aborted by ApiFetcher! Reason: " + response[17:]) + self.logger.warning(msg) else: - msg = SU.red("Trying to load programme from database only, because of an unknown response from ApiFetcher: " + response) + msg = SU.red("Load programme from DB, because of an unknown response from ApiFetcher: " + response) self.logger.warning(msg) # Always load latest programme from the database @@ -177,6 +177,9 @@ class ProgrammeService(): now_unix = Engine.engine_time() next_timeslots = [] + if not self.timeslots: + return [] + for timeslot in self.timeslots: if timeslot.start_unix > now_unix: if (len(next_timeslots) < max_count) or max_count == 0: @@ -200,7 +203,7 @@ class ProgrammeService(): window_end = self.config.get("scheduling_window_end") timeslots = list(filter(lambda s: (s.start_unix - window_end) > now_unix and (s.start_unix - window_start) < now_unix, timeslots)) len_after = len(timeslots) - self.logger.info("For now, skipped %s future timeslot(s) which are out of the scheduling window (-%ss <-> -%ss)" % ((len_before - len_after), window_start, window_end)) + self.logger.info("For now, skipped %s future timeslot(s) which are out of the scheduling window (T-%ss to T-%ss)" % ((len_before - len_after), window_start, window_end)) return timeslots