From be23f5dcd93b3a0a6e9529d91bec038f271978a9 Mon Sep 17 00:00:00 2001
From: Loxbie <ole@freirad.at>
Date: Thu, 12 Sep 2024 14:42:55 +0200
Subject: [PATCH] Fix: allow the preload to time out if the state is None

When the PlayState is None we assume that something went wrong during preloading. If it is None for more than 15 seconds we log an error and stop the preloading. Maybe this logic should be moved into the PlayState itself, which then could set the status to TIMEOUT.
---
 src/aura_engine/scheduling/scheduler.py | 18 +++++++++++++++++-
 1 file changed, 17 insertions(+), 1 deletion(-)

diff --git a/src/aura_engine/scheduling/scheduler.py b/src/aura_engine/scheduling/scheduler.py
index 4a46b22..8d2a361 100644
--- a/src/aura_engine/scheduling/scheduler.py
+++ b/src/aura_engine/scheduling/scheduler.py
@@ -200,8 +200,9 @@ class PlayCommand(EngineExecutor):
         self.logger.info(SU.cyan(f"=== play('{items_str}') ==="))
 
         last_item: PlaylistItem = items[-1]
-        # FIXME: this never breaks and keeps displaying the message in come faulty edge cases,
+        # FIXME: this never breaks and keeps displaying the message in some faulty edge cases,
         # even after the timeslot ends
+        timeout = 0
         while not last_item.play.is_ready():
             now = SU.timestamp()
             if now - last_log > log_interval:
@@ -210,6 +211,21 @@ class PlayCommand(EngineExecutor):
             if last_item.play.state == PlayState.PlayStateType.TIMEOUT:
                 self.logger.warn(SU.red("PLAY: Preloading timed out."))
                 break
+            # FIXME: could this be handled inside PlayState which would then set its type to
+            # TIMEOUT itself?
+            if last_item.play.state is None:
+                timeout += 1
+            else:
+                timeout = 0
+
+            if last_item.play.state is None and timeout >= 15:
+                self.logger.error(
+                    SU.red(
+                        "PLAY: Aborting preload after 15 seconds since the play state is still None."
+                    )
+                )
+                break
+            self.logger.debug(f"PLAY: STATE({last_item.play.state})")
             time.sleep(1)
         self.engine.player.play(items[0], engine.Player.TransitionType.FADE)
         timetable_renderer: utils.TimetableRenderer = self.engine.scheduler.timetable_renderer
-- 
GitLab