Browse Source

Bug proxy

master
scayac 3 weeks ago
parent
commit
de459417f7
  1. 12
      backend/app/audio_player.py
  2. 1
      backend/app/main.py
  3. 3
      backend/systemd/pysonnerie-backend.service
  4. 6
      frontend/app/backend_client.py
  5. 3
      frontend/systemd/pysonnerie-frontend.service

12
backend/app/audio_player.py

@ -1,6 +1,7 @@
from __future__ import annotations from __future__ import annotations
import logging import logging
import shutil
import subprocess import subprocess
import threading import threading
from pathlib import Path from pathlib import Path
@ -86,12 +87,11 @@ class AudioPlayer:
raise ValueError("end_seconds must be greater than start_seconds") raise ValueError("end_seconds must be greater than start_seconds")
volume_factor = max(0.0, min(1.0, float(volume) / 100.0)) volume_factor = max(0.0, min(1.0, float(volume) / 100.0))
cmd = [ base_cmd = [
"play", "play",
"-q", "-q",
"-v", "-v",
str(volume_factor), str(volume_factor),
str(music_path),
] ]
effects: list[str] = [] effects: list[str] = []
@ -134,14 +134,14 @@ class AudioPlayer:
effects += ["repeat", str(int(repeat_count))] effects += ["repeat", str(int(repeat_count))]
# If sox lacks mp3 handlers, decode with ffmpeg and stream wav to sox. # If sox lacks mp3 handlers, decode with ffmpeg and stream wav to sox.
if music_path.suffix.lower() == ".mp3": if music_path.suffix.lower() == ".mp3" and shutil.which("ffmpeg"):
decoder_cmd = ["ffmpeg", "-v", "error", "-i", str(music_path), "-f", "wav", "-"] decoder_cmd = ["ffmpeg", "-v", "error", "-i", str(music_path), "-f", "wav", "-"]
self._decoder_proc = subprocess.Popen( self._decoder_proc = subprocess.Popen(
decoder_cmd, decoder_cmd,
stdout=subprocess.PIPE, stdout=subprocess.PIPE,
stderr=subprocess.DEVNULL, stderr=subprocess.DEVNULL,
) )
cmd += ["-t", "wav", "-"] + effects cmd = base_cmd + ["-t", "wav", "-"] + effects
self._proc = subprocess.Popen( self._proc = subprocess.Popen(
cmd, cmd,
stdin=self._decoder_proc.stdout, stdin=self._decoder_proc.stdout,
@ -151,7 +151,9 @@ class AudioPlayer:
if self._decoder_proc.stdout is not None: if self._decoder_proc.stdout is not None:
self._decoder_proc.stdout.close() self._decoder_proc.stdout.close()
else: else:
cmd += [str(music_path)] + effects if music_path.suffix.lower() == ".mp3":
logger.warning("ffmpeg unavailable, falling back to direct sox playback for %s", music_path)
cmd = base_cmd + [str(music_path)] + effects
self._proc = subprocess.Popen(cmd, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) self._proc = subprocess.Popen(cmd, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
def stop(self) -> None: def stop(self) -> None:

1
backend/app/main.py

@ -218,6 +218,7 @@ def force_play(trigger_id: str, repeat_count: int | None = None) -> Dict[str, st
except KeyError: except KeyError:
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Unknown trigger") raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Unknown trigger")
except (FileNotFoundError, ValueError, RuntimeError) as exc: except (FileNotFoundError, ValueError, RuntimeError) as exc:
logging.getLogger("pysonnerie.api").warning("Force play failed for %s: %s", trigger_id, exc)
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail=str(exc)) raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail=str(exc))
return {"status": "playing"} return {"status": "playing"}

3
backend/systemd/pysonnerie-backend.service

@ -7,9 +7,10 @@ Type=simple
User=www-data User=www-data
WorkingDirectory=/opt/pySonnerie/backend WorkingDirectory=/opt/pySonnerie/backend
Environment=PATH=/opt/pySonnerie/backend/.venv/bin:/usr/bin:/bin Environment=PATH=/opt/pySonnerie/backend/.venv/bin:/usr/bin:/bin
Environment=NO_PROXY=127.0.0.1,localhost,::1
Environment=no_proxy=127.0.0.1,localhost,::1
#Environment=http_proxy=http://proxy:port #Environment=http_proxy=http://proxy:port
#Environment=https_proxy=http://proxy:port #Environment=https_proxy=http://proxy:port
#Environment=no_proxy=127.0.0.1,localhost
ExecStart=/opt/pySonnerie/backend/.venv/bin/python /opt/pySonnerie/backend/run.py ExecStart=/opt/pySonnerie/backend/.venv/bin/python /opt/pySonnerie/backend/run.py
Restart=on-failure Restart=on-failure
RestartSec=3 RestartSec=3

6
frontend/app/backend_client.py

@ -40,11 +40,13 @@ class BackendClient:
self.base_url = base_url.rstrip("/") self.base_url = base_url.rstrip("/")
self.auth = (username, password) self.auth = (username, password)
self.timeout = 8 self.timeout = 8
self.session = requests.Session()
self.session.trust_env = False
def _request(self, method: str, path: str, json_payload: dict[str, Any] | None = None) -> Any: def _request(self, method: str, path: str, json_payload: dict[str, Any] | None = None) -> Any:
url = f"{self.base_url}{path}" url = f"{self.base_url}{path}"
try: try:
response = requests.request( response = self.session.request(
method=method, method=method,
url=url, url=url,
auth=self.auth, auth=self.auth,
@ -75,7 +77,7 @@ class BackendClient:
def health(self) -> dict[str, Any]: def health(self) -> dict[str, Any]:
url = f"{self.base_url}/api/health" url = f"{self.base_url}/api/health"
try: try:
response = requests.get(url, timeout=self.timeout, verify=False) response = self.session.get(url, timeout=self.timeout, verify=False)
response.raise_for_status() response.raise_for_status()
return response.json() return response.json()
except requests.RequestException as exc: except requests.RequestException as exc:

3
frontend/systemd/pysonnerie-frontend.service

@ -9,9 +9,10 @@ Group=www-data
WorkingDirectory=/opt/pySonnerie/frontend WorkingDirectory=/opt/pySonnerie/frontend
Environment=FRONTEND_BIND=0.0.0.0:5000 Environment=FRONTEND_BIND=0.0.0.0:5000
Environment=PATH=/opt/pySonnerie/frontend/.venv/bin:/usr/bin:/bin Environment=PATH=/opt/pySonnerie/frontend/.venv/bin:/usr/bin:/bin
Environment=NO_PROXY=127.0.0.1,localhost,::1
Environment=no_proxy=127.0.0.1,localhost,::1
#Environment=http_proxy=http://TON_PROXY:PORT #Environment=http_proxy=http://TON_PROXY:PORT
#Environment=https_proxy=http://TON_PROXY:PORT #Environment=https_proxy=http://TON_PROXY:PORT
#Environment=no_proxy=127.0.0.1,localhost
ExecStart=/opt/pySonnerie/frontend/.venv/bin/gunicorn --workers 2 --bind ${FRONTEND_BIND} wsgi:app ExecStart=/opt/pySonnerie/frontend/.venv/bin/gunicorn --workers 2 --bind ${FRONTEND_BIND} wsgi:app
Restart=on-failure Restart=on-failure
RestartSec=3 RestartSec=3

Loading…
Cancel
Save