Skip to content
Snippets Groups Projects
Commit 2d0c3061 authored by David Trattnig's avatar David Trattnig
Browse files

Fixed timeslot fetching. #41

parent 05b6b59b
No related branches found
No related tags found
No related merge requests found
...@@ -60,7 +60,6 @@ class ApiFetcher(threading.Thread): ...@@ -60,7 +60,6 @@ class ApiFetcher(threading.Thread):
self.tank_playlist_url = self.config.get("api_tank_playlist") self.tank_playlist_url = self.config.get("api_tank_playlist")
self.tank_session = self.config.get("api_tank_session") self.tank_session = self.config.get("api_tank_session")
self.tank_secret = self.config.get("api_tank_secret") self.tank_secret = self.config.get("api_tank_secret")
self.queue = queue.Queue() self.queue = queue.Queue()
self.stop_event = threading.Event() self.stop_event = threading.Event()
threading.Thread.__init__(self) threading.Thread.__init__(self)
...@@ -94,6 +93,7 @@ class ApiFetcher(threading.Thread): ...@@ -94,6 +93,7 @@ class ApiFetcher(threading.Thread):
return return
# #
# METHODS # METHODS
# #
......
...@@ -55,6 +55,7 @@ class ProgrammeService(): ...@@ -55,6 +55,7 @@ class ProgrammeService():
self.programme_store = ProgrammeStore() self.programme_store = ProgrammeStore()
def refresh(self): def refresh(self):
""" """
Fetch the latest programme from `ProgrammeStore` which stores it to the database. Fetch the latest programme from `ProgrammeStore` which stores it to the database.
...@@ -68,7 +69,7 @@ class ProgrammeService(): ...@@ -68,7 +69,7 @@ class ProgrammeService():
self.api_fetcher = ApiFetcher(self.config) self.api_fetcher = ApiFetcher(self.config)
self.api_fetcher.start() self.api_fetcher.start()
response = self.api_fetcher.get_fetched_data() response = self.api_fetcher.get_fetched_data()
# Reset last successful fetch state # Reset last successful fetch state
lsf = self.last_successful_fetch lsf = self.last_successful_fetch
self.last_successful_fetch = None self.last_successful_fetch = None
...@@ -77,18 +78,17 @@ class ProgrammeService(): ...@@ -77,18 +78,17 @@ class ProgrammeService():
msg = SU.red("Trying to load programme from Engine Database, because ApiFetcher returned an empty response.") msg = SU.red("Trying to load programme from Engine Database, because ApiFetcher returned an empty response.")
self.logger.warning(msg) self.logger.warning(msg)
elif type(response) is list: elif type(response) is list:
self.timeslots = response if len(response) > 0:
if self.timeslots is not None and len(self.timeslots) > 0: self.last_successful_fetch = datetime.now()
self.last_successful_fetch = datetime.now()
self.programme_store.store_timeslots(response) self.programme_store.store_timeslots(response)
self.logger.info(SU.green(f"Finished fetching current programme from API ({len(response)})")) self.logger.info(SU.green(f"Finished fetching current programme from API ({len(response)} timeslots)"))
if len(self.timeslots) == 0: else:
self.logger.critical("Programme fetched from Steering/Tank has no entries!") self.logger.critical("Programme fetched from Steering/Tank has no entries!")
elif response.startswith("fetching_aborted"): elif response.startswith("fetching_aborted"):
msg = SU.red("Trying to load programme from database only, because fetching was being aborted from ApiFetcher! Reason: ") msg = SU.red("Load programme from DB, because fetching was aborted by ApiFetcher! Reason: " + response[17:])
self.logger.warning(msg + response[16:]) self.logger.warning(msg)
else: 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) self.logger.warning(msg)
# Always load latest programme from the database # Always load latest programme from the database
...@@ -177,6 +177,9 @@ class ProgrammeService(): ...@@ -177,6 +177,9 @@ class ProgrammeService():
now_unix = Engine.engine_time() now_unix = Engine.engine_time()
next_timeslots = [] next_timeslots = []
if not self.timeslots:
return []
for timeslot in self.timeslots: for timeslot in self.timeslots:
if timeslot.start_unix > now_unix: if timeslot.start_unix > now_unix:
if (len(next_timeslots) < max_count) or max_count == 0: if (len(next_timeslots) < max_count) or max_count == 0:
...@@ -200,7 +203,7 @@ class ProgrammeService(): ...@@ -200,7 +203,7 @@ class ProgrammeService():
window_end = self.config.get("scheduling_window_end") 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)) 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) 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 return timeslots
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment