feat: add logger.py and add root to livekit.yaml

This commit is contained in:
roai_linux 2026-03-29 20:24:16 +03:30
parent ffd8d174c8
commit 135d7d0ff3
4 changed files with 79 additions and 30 deletions

38
Back/core/logger.py Normal file
View File

@ -0,0 +1,38 @@
import logging
import os
from typing import Final
DEFAULT_LOG_LEVEL: Final[str] = "INFO"
DEFAULT_LOG_FORMAT: Final[str] = (
"%(asctime)s | %(levelname)s | %(name)s | %(message)s"
)
_configured = False
def _resolve_log_level() -> int:
level_name = os.getenv("LOG_LEVEL", DEFAULT_LOG_LEVEL).upper()
return getattr(logging, level_name, logging.INFO)
def setup_logging() -> None:
global _configured
if _configured:
return
root_logger = logging.getLogger()
root_logger.setLevel(_resolve_log_level())
if not root_logger.handlers:
handler = logging.StreamHandler()
handler.setFormatter(logging.Formatter(DEFAULT_LOG_FORMAT))
root_logger.addHandler(handler)
_configured = True
def get_logger(name: str) -> logging.Logger:
setup_logging()
return logging.getLogger(name)

View File

@ -39,24 +39,16 @@ services:
livekit: livekit:
image: livekit/livekit-server image: livekit/livekit-server
container_name: neda_livekit container_name: neda_livekit
ports: network_mode: "host"
- "7780:7880"
- "7781:7881"
- "51000-52000:51000-52000/udp"
- "3478:3478/udp"
- "5349:5349/tcp"
env_file: env_file:
- .env - .env
volumes: volumes:
- ./livekit.yaml:/etc/livekit/livekit.yaml - ./livekit.yaml:/etc/livekit/livekit.yaml
# - /etc/letsencrypt/live/pathfinder.wikm.ir:/etc/letsencrypt:ro # uncomment when using letsencrypt - /etc/letsencrypt:/etc/letsencrypt:ro # uncomment when using letsencrypt
command: [ "--config", "/etc/livekit/livekit.yaml", "--keys", "${LIVEKIT_API_KEY}: ${LIVEKIT_API_SECRET}" ] command: [ "--config", "/etc/livekit/livekit.yaml", "--keys", "${LIVEKIT_API_KEY}: ${LIVEKIT_API_SECRET}" ]
restart: always restart: always
networks:
- public
- internal
healthcheck: healthcheck:
test: [ "CMD", "wget", "--no-verbose", "--tries=1", "--spider", "http://localhost:7880/health" ] # بررسی سلامت LiveKit test: [ "CMD", "wget", "--no-verbose", "--tries=1", "--spider", "http://localhost:7880/health" ]
interval: 10s interval: 10s
timeout: 5s timeout: 5s
retries: 3 retries: 3
@ -109,7 +101,7 @@ networks:
driver: bridge driver: bridge
internal: internal:
driver: bridge driver: bridge
# internal: true internal: true
volumes: volumes:
postgres_data: postgres_data:

View File

@ -1,6 +1,8 @@
import uuid import uuid
from livekit import api from livekit import api
from livekit.api.twirp_client import TwirpError
from sqlalchemy.ext.asyncio import AsyncSession from sqlalchemy.ext.asyncio import AsyncSession
from core.logger import get_logger
from integrations.livekit.client import get_livekit_api from integrations.livekit.client import get_livekit_api
from db.redis import ( from db.redis import (
acquire_speaker, acquire_speaker,
@ -8,7 +10,8 @@ from db.redis import (
get_active_speaker get_active_speaker
) )
from domains.groups.repo import get_group_by_id logger = get_logger(__name__)
async def request_speak( async def request_speak(
group_id: str | uuid.UUID, group_id: str | uuid.UUID,
@ -43,13 +46,21 @@ async def current_speaker(group_id: str | uuid.UUID):
async def grant_publish_permission(room_name: str, identity: str, can_publish: bool): async def grant_publish_permission(room_name: str, identity: str, can_publish: bool):
lk_api = get_livekit_api() lk_api = get_livekit_api()
await lk_api.room.update_participant( try:
api.UpdateParticipantRequest( await lk_api.room.update_participant(
room=room_name, api.UpdateParticipantRequest(
identity=identity, room=room_name,
permission=api.ParticipantPermission( identity=identity,
can_publish=can_publish, permission=api.ParticipantPermission(
can_subscribe=True can_publish=can_publish,
can_subscribe=True
)
) )
) )
) except TwirpError as e:
if "not_found" in str(e).lower() or "exist" in str(e).lower():
logger.warning(f"Participant {identity} already left the room.")
else:
logger.error(f"Error updating participant: {e}")
except Exception as e:
logger.error(f"Unexpected error in grant_permission: {e}")

View File

@ -3,18 +3,26 @@ port: 7880
rtc: rtc:
tcp_port: 7881 tcp_port: 7881
port_range_start: 51000 port_range_start: 51000
port_range_end: 52000 port_range_end: 51100
use_external_ip: false use_external_ip: false
# node_ip: "94.183.170.121" # uncomment when using server ip node_ip: "188.213.199.211" # uncomment when using server ip
allow_tcp_fallback: true
congestion_control:
enabled: true
room:
empty_timeout: 600
departure_timeout: 600
auto_create: true
##### uncomment when using letsencrypt in server ####### ##### uncomment when using letsencrypt in server #######
# turn: turn:
# cert_file: "/etc/letsencrypt/live/pathfinder.wikm.ir/fullchain.pem" cert_file: "/etc/letsencrypt/live/pathfinder.wikm.ir/fullchain.pem"
# key_file: "/etc/letsencrypt/live/pathfinder.wikm.ir/privkey.pem" key_file: "/etc/letsencrypt/live/pathfinder.wikm.ir/privkey.pem"
# tls_port: 5349 tls_port: 5349
# udp_port: 3478 udp_port: 3478
# external_tls: false external_tls: false
# domain: "pathfinder.wikm.ir" domain: "pathfinder.wikm.ir"
####################################################### #######################################################
logging: logging: