Refactor logging, clean dead code, fix temp files and async tasks

- Replace f-string logging with % formatting throughout
- Fix create_temp_file: use mkstemp() instead of mkdtemp() (no leaked dirs)
- Add cleanup background thread and copy.deepcopy to AsyncTaskManager
- Fix transcribe_audio_async signature to accept transcription_service + params
- Use resample_poly instead of scipy_resample for better audio quality
- Remove unused validate_file/BinaryIO methods from FileValidator
- Add requests to requirements.txt
- Add error handling for pip install in server.sh
- Add __all__ and module docstrings to __init__.py files

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Serge Zaigraeff
2026-03-31 00:43:53 +03:00
co-authored by Claude Sonnet 4.6
parent 1e778cae86
commit ca2e41e0f5
13 changed files with 60 additions and 127 deletions
+23 -6
View File
@@ -2,6 +2,7 @@
Модуль async_tasks.py содержит функции для асинхронной обработки задач.
"""
import copy
import uuid
import time
from typing import Dict, Any, Callable, Optional
@@ -27,6 +28,7 @@ class AsyncTaskManager:
"""
self._lock = Lock()
self.tasks: Dict[str, Dict[str, Any]] = {}
self._start_cleanup_thread()
def run_task(self, func: Callable, *args, **kwargs) -> str:
"""
@@ -91,14 +93,14 @@ class AsyncTaskManager:
self.tasks[task_id]["result"] = result
self.tasks[task_id]["completed_at"] = time.time()
logger.info(f"Задача {task_id} завершена успешно")
logger.info("Задача %s завершена успешно", task_id)
except Exception as e:
with self._lock:
self.tasks[task_id]["status"] = "failed"
self.tasks[task_id]["error"] = str(e)
self.tasks[task_id]["completed_at"] = time.time()
logger.error(f"Задача {task_id} завершилась с ошибкой: {e}")
logger.error("Задача %s завершилась с ошибкой: %s", task_id, e)
def get_task_status(self, task_id: str) -> Optional[Dict[str, Any]]:
"""
@@ -112,22 +114,37 @@ class AsyncTaskManager:
"""
with self._lock:
task = self.tasks.get(task_id)
return dict(task) if task else None
return copy.deepcopy(task) if task else None
def _start_cleanup_thread(self) -> None:
"""Запуск фонового потока для периодической очистки старых задач."""
def _cleanup_loop():
while True:
time.sleep(600) # Каждые 10 минут
with self._lock:
self._cleanup_old_tasks()
thread = Thread(target=_cleanup_loop, daemon=True)
thread.start()
# Глобальный экземпляр менеджера асинхронных задач
task_manager = AsyncTaskManager()
def transcribe_audio_async(file_path: str, transcriber) -> str:
def transcribe_audio_async(file_path: str, transcription_service, params: Dict = None) -> str:
"""
Асинхронная транскрибация аудиофайла.
Args:
file_path: Путь к аудиофайлу.
transcriber: Экземпляр транскрайбера.
transcription_service: Экземпляр TranscriptionService.
params: Параметры транскрибации.
Returns:
ID задачи.
"""
return task_manager.run_task(transcriber.process_file, file_path)
params = params or {}
def _transcribe():
return transcription_service.transcribe(file_path, "async_task", params)
return task_manager.run_task(_transcribe)