dont search for fallbacks wthout updating the db first

This commit is contained in:
deflax 2024-01-07 06:32:31 +02:00
parent 485cacc5e2
commit 55d731b709

View file

@ -18,7 +18,7 @@ logger_api.setLevel(log_level)
logger_job.setLevel(log_level) logger_job.setLevel(log_level)
# Variables # Variables
CORE_SYNC_PERIOD = int(os.environ.get('CORE_SYNC_PERIOD', 30)) CORE_SYNC_PERIOD = int(os.environ.get('CORE_SYNC_PERIOD', 15))
api_hostname = os.environ.get('CORE_API_HOSTNAME', 'stream.example.com') api_hostname = os.environ.get('CORE_API_HOSTNAME', 'stream.example.com')
api_username = os.environ.get('CORE_API_AUTH_USERNAME', 'admin') api_username = os.environ.get('CORE_API_AUTH_USERNAME', 'admin')
api_password = os.environ.get('CORE_API_AUTH_PASSWORD', 'pass') api_password = os.environ.get('CORE_API_AUTH_PASSWORD', 'pass')
@ -52,12 +52,12 @@ def process_running_channel(database, scheduler, stream_id, stream_name, stream_
if stream_start == "never": if stream_start == "never":
# Skip channels that are set to never start automatically # Skip channels that are set to never start automatically
return return
logger_job.info(f'{stream_id} ({stream_name}) has been registered to the database') logger_job.warning(f'{stream_id} ({stream_name}) has been registered to the database')
if stream_start == "now": if stream_start == "now":
logger_job.info("Stream should start now") logger_job.warning("Stream should start now")
scheduler.add_job(func=stream_exec, id=stream_id, args=(stream_id, stream_name, stream_prio, stream_hls_url)) scheduler.add_job(func=stream_exec, id=stream_id, args=(stream_id, stream_name, stream_prio, stream_hls_url))
else: else:
logger_job.info(f"Stream start hour is set to {stream_start}") logger_job.warning(f"Stream start hour is set to {stream_start}")
scheduler.add_job( scheduler.add_job(
func=stream_exec, trigger='cron', hour=stream_start, jitter=60, func=stream_exec, trigger='cron', hour=stream_start, jitter=60,
id=stream_id, args=(stream_id, stream_name, stream_prio, stream_hls_url) id=stream_id, args=(stream_id, stream_name, stream_prio, stream_hls_url)
@ -73,23 +73,23 @@ def remove_channel_from_database(database, scheduler, stream_id, stream_name, st
global prio global prio
global head global head
if stream_id in database: if stream_id in database:
logger_job.info(f'{stream_id} ({stream_name}) will be removed from the database. Reason: {state.exec}') logger_job.warning(f'{stream_id} ({stream_name}) will be removed from the database. Reason: {state.exec}')
# Handle the situation where we remove an stream that is currently playing
if head['id'] == stream_id:
logger_job.warning(f'{stream_id} was currently running.')
fallback = fallback_search(database)
prio = 0
logger_job.info(f'Source priority is reset to 0')
scheduler.add_job(func=stream_exec, id="fallback", args=(fallback['stream_id'], fallback['stream_name'], 0, fallback['stream_hls_url']))
database.pop(stream_id) database.pop(stream_id)
try: try:
scheduler.remove_job(stream_id) scheduler.remove_job(stream_id)
except Exception as joberror: except Exception as joberror:
logger_job.error(joberror) logger_job.error(joberror)
# Handle the situation where we remove an stream that is currently playing
if head['id'] == stream_id:
logger_job.warning(f'{stream_id} was currently running.')
fallback = fallback_search(database)
prio = 0
logger_job.warning(f'Source priority is reset to 0')
scheduler.add_job(func=stream_exec, id="fallback", args=(fallback['stream_id'], fallback['stream_name'], 0, fallback['stream_hls_url']))
# Helper function to search for a fallback stream # Helper function to search for a fallback stream
def fallback_search(database): def fallback_search(database):
logger_job.info('Searching for a fallback job.') logger_job.warning('Searching for a fallback job.')
current_hour = datetime.now().hour current_hour = datetime.now().hour
hour_set = [] hour_set = []
for key, value in database.items(): for key, value in database.items():
@ -119,15 +119,15 @@ def update_head(stream_id, stream_prio, stream_hls_url):
head = { "id": stream_id, head = { "id": stream_id,
"prio": stream_prio, "prio": stream_prio,
"head": stream_hls_url } "head": stream_hls_url }
logger_job.info(f'Head position is: {str(head)}') logger_job.warning(f'Head position is: {str(head)}')
# Tasks # Tasks
def stream_exec(stream_id, stream_name, stream_prio, stream_hls_url): def stream_exec(stream_id, stream_name, stream_prio, stream_hls_url):
global prio global prio
logger_job.info(f'Hello {stream_name}!') logger_job.warning(f'Hello {stream_name}!')
if stream_prio > prio: if stream_prio > prio:
prio = stream_prio prio = stream_prio
logger_job.info(f'Source priority is now set to: {prio}') logger_job.warning(f'Source priority is now set to: {prio}')
update_head(stream_id, stream_prio, stream_hls_url) update_head(stream_id, stream_prio, stream_hls_url)
elif stream_prio == prio: elif stream_prio == prio:
update_head(stream_id, stream_prio, stream_hls_url) update_head(stream_id, stream_prio, stream_hls_url)
@ -173,7 +173,7 @@ def core_api_sync():
# Cleanup orphaned references # Cleanup orphaned references
orphan_keys = [key for key in database if key not in new_ids] orphan_keys = [key for key in database if key not in new_ids]
for orphan_key in orphan_keys: for orphan_key in orphan_keys:
logger_job.info(f'Key {orphan_key} is an orphan. Removing.') logger_job.warning(f'Key {orphan_key} is an orphan. Removing.')
database.pop(orphan_key) database.pop(orphan_key)
scheduler.remove_job(orphan_key) scheduler.remove_job(orphan_key)