Commit 6d01b0dd authored by Gottfried Gaisbauer's avatar Gottfried Gaisbauer
Browse files

schedule now loaded from db when fetch fails

parent cd9bc5fb
......@@ -207,6 +207,15 @@ class Schedule(DB.Model, AuraDatabaseModel):
def select_show_on_datetime(datetime):
return DB.session.query(Schedule).filter(Schedule.schedule_start == datetime).first()
@staticmethod
def select_act_programme():
#DB.session.query(Schedule).filter
# fetching all from today to ..
today = datetime.date.today()
all_entries = DB.session.query(Schedule).filter(Schedule.schedule_start >= today).order_by(Schedule.schedule_start).all()
return all_entries
# ------------------------------------------------------------------------------------------ #
class Playlist(DB.Model, AuraDatabaseModel):
__tablename__ = 'playlist'
......
......@@ -130,6 +130,11 @@ class AuraCalendarService(threading.Thread):
try:
fetched_schedule_data = self.calendar_fetcher.fetch()
# if nothing is fetched, return
if fetched_schedule_data is None:
self.queue.put("fetching_aborted Nothing fetched")
return
ret_schedule = []
# for schedule in self.fetched_schedule_data:
# if "start" not in schedule:
......@@ -209,6 +214,7 @@ class AuraCalendarService(threading.Thread):
def store_playlist(self, schedule_db, playlist_id, fetched_playlist, fallbackplaylist_type=0):
playlist_db = Playlist.select_playlist_for_schedule(schedule_db.schedule_start, playlist_id)
havetoadd = False
if not playlist_db:
playlist_db = Playlist()
havetoadd = True
......
......@@ -31,8 +31,12 @@ class CalendarFetcher:
self.__fetch_schedule_data__()
except urllib.error.HTTPError as e:
self.logger.critical("Cannot fetch from " + self.url["calendar"] + "! Reason: " + str(e))
self.fetched_schedule_data = None
return None
except (urllib.error.URLError, IOError, ValueError) as e:
self.logger.critical("Cannot connect to " + self.url["calendar"] + "! Reason: " + str(e))
self.fetched_schedule_data = None
return None
# fetch playlist and fallbacks to the schedules from TANK
try:
......@@ -40,8 +44,12 @@ class CalendarFetcher:
self.__fetch_schedule_playlists__()
except urllib.error.HTTPError as e:
self.logger.critical("Cannot fetch from " + self.url["importer"] + "! Reason: " + str(e))
self.fetched_schedule_data = None
return None
except (urllib.error.URLError, IOError, ValueError) as e:
self.logger.critical("Cannot connect to " + self.url["importer"] + "! Reason: " + str(e))
self.fetched_schedule_data = None
return None
return_data = []
# gather returndata
......@@ -62,6 +70,8 @@ class CalendarFetcher:
return_data.append(schedule)
except TypeError as e:
self.logger.error("Nothing fetched...")
self.fetched_schedule_data = None
return None
return return_data
......@@ -199,7 +209,6 @@ class CalendarFetcher:
now = datetime.now()
for index,curr in enumerate(schedule_from_pv[:-1]):
print(curr["start"])
date_start = datetime.strptime(curr["start"], "%Y-%m-%dT%H:%M:%S")
date_next_start = datetime.strptime(schedule_from_pv[index+1]["start"], "%Y-%m-%dT%H:%M:%S")
......
......@@ -75,7 +75,11 @@ class AuraScheduler(ExceptionLogger, threading.Thread):
message_timer = []
job_result = {}
# stores the conn to liquidsoap
liquidsoapcommunicator = None
# stores the last time when a fetch from pv/tank gone right
last_successful_fetch = None
schedule_entries = None
active_entry = None
exit_event = None
......@@ -84,9 +88,6 @@ class AuraScheduler(ExceptionLogger, threading.Thread):
logger = None
config = None
tried_fetching = 0
fetch_max = 3
def __init__(self, config):
"""
Constructor
......@@ -191,6 +192,10 @@ class AuraScheduler(ExceptionLogger, threading.Thread):
def load_programme_from_db(self, silent=False):
self.programme = Schedule.select_act_programme()
if self.programme is None or len(self.programme) == 0:
self.logger.critical("Could not load programme from database. We are in big trouble my friend!")
return
planned_entries = []
for schedule in self.programme:
......@@ -345,13 +350,6 @@ class AuraScheduler(ExceptionLogger, threading.Thread):
# ------------------------------------------------------------------------------------------ #
def fetch_new_programme(self):
self.logger.info("trying to fetch new programme")
if self.tried_fetching == self.fetch_max:
msg = "Cannot connect to PV or Tank! No Programme loaded!"
self.logger.error(msg)
self.tried_fetching = 0
return msg
self.tried_fetching += 1
acs = AuraCalendarService(self.config)
queue = acs.get_queue()
......@@ -362,23 +360,32 @@ class AuraScheduler(ExceptionLogger, threading.Thread):
# wait for the end
response = queue.get()
# reset
lsf = self.last_successful_fetch
self.last_successful_fetch = None
if response is None:
self.logger.critical("Got an EMPTY response from AuraCalendarService: " + str(response))
self.logger.warning("Trying to load programme from database, because i got an EMPTY (None) response from AuraCalendarService.")
elif type(response) is list:
self.logger.critical("not loading from db")
self.programme = response
# self.load_programme_from_db()
if self.programme is not None and len(self.programme) > 0:
self.tried_fetching = 0
if len(self.programme) == 0 and self.tried_fetching == self.fetch_max:
self.logger.critical("Programme loaded from database has no entries!")
self.last_successful_fetch = datetime.datetime.now()
if len(self.programme) == 0:
self.logger.critical("Programme fetched from pv/tank has no entries!")
# return self.get_act_programme_as_string()
elif response.startswith("fetching_aborted"):
self.logger.warning("Fetching was being aborted from AuraCalendarService! Reason: " + response)
self.logger.warning("Trying to load programme from database, because fetching was being aborted from AuraCalendarService! Reason: " + response[16:])
else:
self.logger.warning("Got an unknown response from AuraCalendarService: " + response)
self.logger.warning("Trying to load programme from database, because i got an unknown response from AuraCalendarService: " + response)
# if somehow the programme could not be fetched => try to load it from database
if self.last_successful_fetch is None:
self.last_successful_fetch = lsf
self.load_programme_from_db()
# ------------------------------------------------------------------------------------------ #
def set_next_file_for(self, playlistname):
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment