load playlist in separate thread to prevent blocking on startup
This commit is contained in:
parent
5d2de1471f
commit
b9bdf31944
@ -3,6 +3,7 @@ import traceback
|
||||
import time
|
||||
import random
|
||||
import asyncio
|
||||
import threading
|
||||
from .constructors import (
|
||||
ValidRadioNextRequest,
|
||||
ValidRadioReshuffleRequest,
|
||||
@ -27,6 +28,7 @@ class Radio(FastAPI):
|
||||
self.util = my_util
|
||||
self.constants = constants
|
||||
self.radio_util = radio_util.RadioUtil(self.constants)
|
||||
self.playlist_loaded: bool = False
|
||||
|
||||
self.endpoints: dict = {
|
||||
"radio/np": self.radio_now_playing,
|
||||
@ -52,9 +54,16 @@ class Radio(FastAPI):
|
||||
methods=["GET"],
|
||||
include_in_schema=True,
|
||||
)
|
||||
|
||||
app.add_event_handler("startup", self.on_start)
|
||||
|
||||
async def on_start(self) -> None:
|
||||
logging.info("radio: Initializing")
|
||||
thread = threading.Thread(target=asyncio.run, args=(self.radio_util.load_playlist(),))
|
||||
thread.start()
|
||||
# await self.radio_util.load_playlist()
|
||||
await self.radio_util._ls_skip()
|
||||
|
||||
asyncio.get_event_loop().run_until_complete(self.radio_util.load_playlist())
|
||||
asyncio.get_event_loop().run_until_complete(self.radio_util._ls_skip())
|
||||
|
||||
async def radio_skip(
|
||||
self, data: ValidRadioNextRequest, request: Request
|
||||
@ -256,8 +265,9 @@ class Radio(FastAPI):
|
||||
not isinstance(self.radio_util.active_playlist, list)
|
||||
or not self.radio_util.active_playlist
|
||||
):
|
||||
await self.radio_util.load_playlist()
|
||||
await self.radio_util._ls_skip()
|
||||
if self.radio_util.playlist_loaded:
|
||||
self.radio_util.playlist_loaded = False
|
||||
await self.on_start()
|
||||
return JSONResponse(
|
||||
status_code=500,
|
||||
content={
|
||||
@ -268,8 +278,7 @@ class Radio(FastAPI):
|
||||
next = self.radio_util.active_playlist.pop(0)
|
||||
if not isinstance(next, dict):
|
||||
logging.critical("next is of type: %s, reloading playlist...", type(next))
|
||||
await self.radio_util.load_playlist()
|
||||
await self.radio_util._ls_skip()
|
||||
await self.on_start()
|
||||
return JSONResponse(
|
||||
status_code=500,
|
||||
content={
|
||||
|
@ -57,6 +57,7 @@ class RadioUtil:
|
||||
"electronic",
|
||||
]
|
||||
self.active_playlist: list[dict] = []
|
||||
self.playlist_loaded: bool = False
|
||||
self.now_playing: dict = {
|
||||
"artist": "N/A",
|
||||
"song": "N/A",
|
||||
@ -378,6 +379,7 @@ class RadioUtil:
|
||||
"%s items remain for playback after filtering",
|
||||
len(self.active_playlist),
|
||||
)
|
||||
self.playlist_loaded = True
|
||||
except Exception as e:
|
||||
logging.info("Playlist load failed: %s", str(e))
|
||||
traceback.print_exc()
|
||||
|
Loading…
x
Reference in New Issue
Block a user