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 time
|
||||||
import random
|
import random
|
||||||
import asyncio
|
import asyncio
|
||||||
|
import threading
|
||||||
from .constructors import (
|
from .constructors import (
|
||||||
ValidRadioNextRequest,
|
ValidRadioNextRequest,
|
||||||
ValidRadioReshuffleRequest,
|
ValidRadioReshuffleRequest,
|
||||||
@ -27,6 +28,7 @@ class Radio(FastAPI):
|
|||||||
self.util = my_util
|
self.util = my_util
|
||||||
self.constants = constants
|
self.constants = constants
|
||||||
self.radio_util = radio_util.RadioUtil(self.constants)
|
self.radio_util = radio_util.RadioUtil(self.constants)
|
||||||
|
self.playlist_loaded: bool = False
|
||||||
|
|
||||||
self.endpoints: dict = {
|
self.endpoints: dict = {
|
||||||
"radio/np": self.radio_now_playing,
|
"radio/np": self.radio_now_playing,
|
||||||
@ -53,8 +55,15 @@ class Radio(FastAPI):
|
|||||||
include_in_schema=True,
|
include_in_schema=True,
|
||||||
)
|
)
|
||||||
|
|
||||||
asyncio.get_event_loop().run_until_complete(self.radio_util.load_playlist())
|
app.add_event_handler("startup", self.on_start)
|
||||||
asyncio.get_event_loop().run_until_complete(self.radio_util._ls_skip())
|
|
||||||
|
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()
|
||||||
|
|
||||||
|
|
||||||
async def radio_skip(
|
async def radio_skip(
|
||||||
self, data: ValidRadioNextRequest, request: Request
|
self, data: ValidRadioNextRequest, request: Request
|
||||||
@ -256,8 +265,9 @@ class Radio(FastAPI):
|
|||||||
not isinstance(self.radio_util.active_playlist, list)
|
not isinstance(self.radio_util.active_playlist, list)
|
||||||
or not self.radio_util.active_playlist
|
or not self.radio_util.active_playlist
|
||||||
):
|
):
|
||||||
await self.radio_util.load_playlist()
|
if self.radio_util.playlist_loaded:
|
||||||
await self.radio_util._ls_skip()
|
self.radio_util.playlist_loaded = False
|
||||||
|
await self.on_start()
|
||||||
return JSONResponse(
|
return JSONResponse(
|
||||||
status_code=500,
|
status_code=500,
|
||||||
content={
|
content={
|
||||||
@ -268,8 +278,7 @@ class Radio(FastAPI):
|
|||||||
next = self.radio_util.active_playlist.pop(0)
|
next = self.radio_util.active_playlist.pop(0)
|
||||||
if not isinstance(next, dict):
|
if not isinstance(next, dict):
|
||||||
logging.critical("next is of type: %s, reloading playlist...", type(next))
|
logging.critical("next is of type: %s, reloading playlist...", type(next))
|
||||||
await self.radio_util.load_playlist()
|
await self.on_start()
|
||||||
await self.radio_util._ls_skip()
|
|
||||||
return JSONResponse(
|
return JSONResponse(
|
||||||
status_code=500,
|
status_code=500,
|
||||||
content={
|
content={
|
||||||
|
@ -57,6 +57,7 @@ class RadioUtil:
|
|||||||
"electronic",
|
"electronic",
|
||||||
]
|
]
|
||||||
self.active_playlist: list[dict] = []
|
self.active_playlist: list[dict] = []
|
||||||
|
self.playlist_loaded: bool = False
|
||||||
self.now_playing: dict = {
|
self.now_playing: dict = {
|
||||||
"artist": "N/A",
|
"artist": "N/A",
|
||||||
"song": "N/A",
|
"song": "N/A",
|
||||||
@ -378,6 +379,7 @@ class RadioUtil:
|
|||||||
"%s items remain for playback after filtering",
|
"%s items remain for playback after filtering",
|
||||||
len(self.active_playlist),
|
len(self.active_playlist),
|
||||||
)
|
)
|
||||||
|
self.playlist_loaded = True
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logging.info("Playlist load failed: %s", str(e))
|
logging.info("Playlist load failed: %s", str(e))
|
||||||
traceback.print_exc()
|
traceback.print_exc()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user