BAPSicle/server.py

342 lines
12 KiB
Python
Raw Permalink Normal View History

2020-11-01 02:35:14 +00:00
"""
BAPSicle Server
Next-gen audio playout server for University Radio York playout,
based on WebStudio interface.
Flask Server
Authors:
Matthew Stratford
Michael Grace
Date:
October, November 2020
"""
from datetime import datetime
from file_manager import FileManager
2020-10-23 20:10:32 +00:00
import multiprocessing
from multiprocessing.queues import Queue
import multiprocessing.managers as m
import time
2021-04-18 17:04:31 +00:00
from typing import Any, Optional
2020-10-23 20:10:32 +00:00
import json
from setproctitle import setproctitle
2021-09-04 14:16:44 +00:00
import psutil
2021-10-12 19:45:29 +00:00
from helpers.os_environment import isLinux, isMacOS
2020-10-25 01:23:24 +00:00
if not isMacOS():
# Rip, this doesn't like threading on MacOS.
import pyttsx3
import package
2020-11-03 23:25:17 +00:00
from typing import Dict, List
2020-11-09 00:10:36 +00:00
from helpers.state_manager import StateManager
from helpers.logging_manager import LoggingManager
from websocket_server import WebsocketServer
from web_server import WebServer
from player_handler import PlayerHandler
from controllers.mattchbox_usb import MattchBox
from helpers.the_terminator import Terminator
import player
2020-11-03 23:25:17 +00:00
PROCESS_KILL_TIMEOUT_S = 5
setproctitle("server.py")
""" Proxy Manager to proxy Class Objects into multiprocessing processes, instead of making a copy. """
class ProxyManager(m.BaseManager):
pass # Pass is really enough. Nothing needs to be done here.
2021-04-08 19:53:51 +00:00
class BAPSicleServer:
2021-04-17 20:47:46 +00:00
default_state = {
"server_version": "unknown",
"server_build": "unknown",
2021-04-27 20:48:17 +00:00
"server_branch": "unknown",
"server_beta": True,
2021-04-17 20:47:46 +00:00
"server_name": "URY BAPSicle",
"host": "localhost",
"port": 13500,
"ws_port": 13501,
"num_channels": 3,
"serial_port": None,
"ser_connected": False,
"myradio_api_key": None,
"myradio_base_url": "https://ury.org.uk/myradio",
"myradio_api_url": "https://ury.org.uk/api",
"myradio_api_tracklist_source": "",
"running_state": "running",
"tracklist_mode": "off",
"normalisation_mode": "off",
2021-04-17 20:47:46 +00:00
}
player_to_q: List[Queue] = []
player_from_q: Queue
ui_to_q: Queue
websocket_to_q: Queue
controller_to_q: Queue
file_to_q: Queue
2021-04-17 20:47:46 +00:00
player: List[multiprocessing.Process] = []
2021-04-18 17:04:31 +00:00
websockets_server: Optional[multiprocessing.Process] = None
controller_handler: Optional[multiprocessing.Process] = None
player_handler: Optional[multiprocessing.Process] = None
file_manager: Optional[multiprocessing.Process] = None
2021-04-18 17:04:31 +00:00
webserver: Optional[multiprocessing.Process] = None
2021-04-17 20:47:46 +00:00
2020-10-24 20:31:52 +00:00
def __init__(self):
while True:
self.startServer()
2021-04-17 20:47:46 +00:00
self.check_processes()
2021-04-18 17:04:31 +00:00
self.stopServer()
if self.state.get()["running_state"] != "restarting":
break
2021-04-18 17:04:31 +00:00
def check_processes(self):
terminator = Terminator()
2021-04-18 17:04:31 +00:00
log_function = self.logger.log.info
2021-09-11 15:49:08 +00:00
while (
2021-09-11 16:18:35 +00:00
not terminator.terminate and self.state.get()[
"running_state"] == "running"
2021-09-11 15:49:08 +00:00
):
2021-04-18 17:04:31 +00:00
for channel in range(self.state.get()["num_channels"]):
2021-09-11 16:48:57 +00:00
# Use pid_exists to confirm process is actually still running.
# Python may not report is_alive() correctly (especially over system sleeps etc.)
2021-09-04 14:16:44 +00:00
# https://medium.com/pipedrive-engineering/encountering-some-python-trickery-683bd5f66750
2021-09-11 15:49:08 +00:00
if (
not self.player[channel]
or not self.player[channel].is_alive()
or not psutil.pid_exists(self.player[channel].pid)
):
2021-09-11 16:18:35 +00:00
log_function(
"Player {} not running, (re)starting.".format(channel))
2021-04-18 17:04:31 +00:00
self.player[channel] = multiprocessing.Process(
target=player.Player,
2021-09-11 15:49:08 +00:00
args=(
channel,
self.player_to_q[channel],
self.player_from_q,
2021-09-11 15:49:08 +00:00
self.state,
),
2021-04-18 17:04:31 +00:00
)
self.player[channel].start()
2021-09-11 15:49:08 +00:00
if (
not self.player_handler
or not self.player_handler.is_alive()
or not psutil.pid_exists(self.player_handler.pid)
):
2021-04-18 17:04:31 +00:00
log_function("Player Handler not running, (re)starting.")
self.player_handler = multiprocessing.Process(
target=PlayerHandler,
2021-09-11 15:49:08 +00:00
args=(
self.player_from_q,
self.websocket_to_q,
self.ui_to_q,
self.controller_to_q,
self.file_to_q,
),
2021-04-18 17:04:31 +00:00
)
self.player_handler.start()
2021-09-11 15:49:08 +00:00
if (
not self.file_manager
or not self.file_manager.is_alive()
or not psutil.pid_exists(self.file_manager.pid)
):
log_function("File Manager not running, (re)starting.")
self.file_manager = multiprocessing.Process(
target=FileManager,
args=(self.file_to_q, self.state),
)
self.file_manager.start()
2021-09-11 15:49:08 +00:00
if (
not self.websockets_server
or not self.websockets_server.is_alive()
or not psutil.pid_exists(self.websockets_server.pid)
):
2021-04-18 17:04:31 +00:00
log_function("Websocket Server not running, (re)starting.")
self.websockets_server = multiprocessing.Process(
2021-09-11 15:49:08 +00:00
target=WebsocketServer,
args=(self.player_to_q, self.websocket_to_q, self.state),
2021-04-18 17:04:31 +00:00
)
self.websockets_server.start()
2021-09-11 15:49:08 +00:00
if (
not self.webserver
or not self.webserver.is_alive()
or not psutil.pid_exists(self.webserver.pid)
):
2021-04-18 17:04:31 +00:00
log_function("Webserver not running, (re)starting.")
self.webserver = multiprocessing.Process(
2021-09-11 16:18:35 +00:00
target=WebServer, args=(
self.player_to_q, self.ui_to_q, self.state)
2021-04-18 17:04:31 +00:00
)
self.webserver.start()
2021-09-11 15:49:08 +00:00
if (
not self.controller_handler
or not self.controller_handler.is_alive()
or not psutil.pid_exists(self.controller_handler.pid)
):
2021-04-18 17:04:31 +00:00
log_function("Controller Handler not running, (re)starting.")
self.controller_handler = multiprocessing.Process(
2021-09-11 15:49:08 +00:00
target=MattchBox,
args=(self.player_to_q, self.controller_to_q, self.state),
2021-04-18 17:04:31 +00:00
)
self.controller_handler.start()
# After first starting processes, switch logger to error, since any future starts will have been failures.
log_function = self.logger.log.error
time.sleep(1)
2021-04-17 20:47:46 +00:00
def startServer(self):
2021-10-12 19:45:29 +00:00
# On MacOS, the default causes something to keep creating new processes.
# On Linux, this is needed to make pulseaudio initiate properly.
if isMacOS() or isLinux():
2021-04-17 20:47:46 +00:00
multiprocessing.set_start_method("spawn", True)
process_title = "BAPSicle - startServer"
2021-04-17 20:47:46 +00:00
setproctitle(process_title)
2021-04-18 20:17:41 +00:00
multiprocessing.current_process().name = process_title
2021-04-17 20:47:46 +00:00
self.logger = LoggingManager("BAPSicleServer")
# Since we're passing the StateManager across processes, it must be made a manager.
# PLEASE NOTE: You can't read attributes directly, use state.get()["var"] and state.update("var", "val")
ProxyManager.register("StateManager", StateManager)
manager = ProxyManager()
manager.start()
2021-09-11 15:49:08 +00:00
self.state: StateManager = manager.StateManager(
"BAPSicleServer", self.logger, self.default_state
)
self.state.update("running_state", "running")
self.state.update("start_time", datetime.now().timestamp())
print("Launching BAPSicle...")
# TODO: Check these match, if not, trigger any upgrade noticies / welcome
self.state.update("server_version", package.VERSION)
self.state.update("server_build", package.BUILD)
2021-04-27 20:48:17 +00:00
self.state.update("server_branch", package.BRANCH)
self.state.update("server_beta", package.BETA)
2021-04-17 20:47:46 +00:00
channel_count = self.state.get()["num_channels"]
2021-04-18 17:04:31 +00:00
self.player = [None] * channel_count
for channel in range(self.state.get()["num_channels"]):
2021-04-17 20:47:46 +00:00
self.player_to_q.append(multiprocessing.Queue())
self.player_from_q = multiprocessing.Queue()
self.ui_to_q = multiprocessing.Queue()
self.websocket_to_q = multiprocessing.Queue()
self.controller_to_q = multiprocessing.Queue()
self.file_to_q = multiprocessing.Queue()
2021-04-17 20:47:46 +00:00
2021-09-11 15:49:08 +00:00
print(
"Welcome to BAPSicle Server version: {}, build: {}.".format(
package.VERSION, package.BUILD
)
)
print(
"The Server UI is available at http://{}:{}".format(
self.state.get()["host"], self.state.get()["port"]
)
)
2021-04-17 20:47:46 +00:00
# TODO Move this to player or installer.
if False:
if not isMacOS():
# Temporary RIP.
# Welcome Speech
text_to_speach = pyttsx3.init()
text_to_speach.save_to_file(
"""Thank-you for installing BAPSicle - the play-out server from the broadcasting and presenting suite.
By default, this server is accepting connections on port 13500
The version of the server service is {}
Please refer to the documentation included with this application for further assistance.""".format(
2021-05-02 18:22:33 +00:00
package.VERSION
2021-04-17 20:47:46 +00:00
),
"dev/welcome.mp3",
)
text_to_speach.runAndWait()
new_item: Dict[str, Any] = {
"channel_weight": 0,
"filename": "dev/welcome.mp3",
"title": "Welcome to BAPSicle",
"artist": "University Radio York",
}
self.player_to_q[0].put("ADD:" + json.dumps(new_item))
self.player_to_q[0].put("LOAD:0")
self.player_to_q[0].put("PLAY")
def stopServer(self):
print("Stopping BASPicle Server.")
2021-04-17 20:47:46 +00:00
print("Stopping Websocket Server")
self.websocket_to_q.put("0:WEBSOCKET:QUIT")
2021-04-17 20:47:46 +00:00
if self.websockets_server:
self.websockets_server.join(timeout=PROCESS_KILL_TIMEOUT_S)
2021-04-18 20:17:41 +00:00
del self.websockets_server
2021-04-17 20:47:46 +00:00
print("Stopping File Manager")
if self.file_manager:
self.file_manager.terminate()
self.file_manager.join(timeout=PROCESS_KILL_TIMEOUT_S)
del self.file_manager
2021-04-17 20:47:46 +00:00
print("Stopping Controllers")
if self.controller_handler:
self.controller_handler.terminate()
self.controller_handler.join(timeout=PROCESS_KILL_TIMEOUT_S)
del self.controller_handler
2021-04-18 20:17:41 +00:00
# Stop the Webserver late as we can to allow Presenter to pull images for the disconnection page if it needs to.
print("Stopping Web Server")
if self.webserver:
self.webserver.terminate()
self.webserver.join(timeout=PROCESS_KILL_TIMEOUT_S)
2021-04-18 20:17:41 +00:00
del self.webserver
2021-04-17 20:47:46 +00:00
print("Stopping Player Handler")
2021-04-17 20:47:46 +00:00
if self.player_handler:
self.player_handler.terminate()
self.player_handler.join(timeout=PROCESS_KILL_TIMEOUT_S)
2021-04-18 20:17:41 +00:00
del self.player_handler
2021-04-17 20:47:46 +00:00
2021-09-11 16:18:35 +00:00
# Now we've stopped everything else, now is the time to stop the players.
# This is to keep playing for as long as possible during a restart.
print("Stopping Players")
for q in self.player_to_q:
q.put("ALL:QUIT")
for player in self.player:
player.join(timeout=PROCESS_KILL_TIMEOUT_S)
2022-03-12 16:45:05 +00:00
del self.player
print("Stopped all processes.")
2020-10-24 20:31:52 +00:00
if __name__ == "__main__":
raise Exception("BAPSicle is a service. Please run it like one.")