load playlist in separate thread to prevent blocking on startup
This commit is contained in:
@ -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={
|
||||
|
Reference in New Issue
Block a user