Refactor Docker configuration and improve database initialization
- Updated `.dockerignore` to streamline ignored files and directories, focusing on essential components. - Removed obsolete `Dockerfile.bot` to simplify the build process. - Enhanced `run_helper.py` with a new `init_db` function to initialize the SQLite database if it doesn't exist, improving setup reliability. - Removed the `/status` endpoint from `server_prometheus.py` to clean up unused functionality and improve code clarity.
This commit is contained in:
@@ -2,6 +2,7 @@ import asyncio
|
||||
import os
|
||||
import sys
|
||||
import signal
|
||||
import sqlite3
|
||||
|
||||
# Ensure project root is on sys.path for module resolution
|
||||
CURRENT_DIR = os.path.dirname(os.path.abspath(__file__))
|
||||
@@ -13,41 +14,10 @@ from helper_bot.utils.base_dependency_factory import get_global_instance
|
||||
from helper_bot.utils.auto_unban_scheduler import get_auto_unban_scheduler
|
||||
from logs.custom_logger import logger
|
||||
|
||||
# Импортируем PID менеджер из инфраструктуры (если доступен)
|
||||
import sys
|
||||
import os
|
||||
|
||||
def get_pid_manager():
|
||||
"""Получение PID менеджера из инфраструктуры проекта"""
|
||||
try:
|
||||
# Пытаемся импортировать из инфраструктуры проекта
|
||||
infra_path = os.path.join(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))), 'infra', 'monitoring')
|
||||
if infra_path not in sys.path:
|
||||
sys.path.insert(0, infra_path)
|
||||
|
||||
from pid_manager import get_bot_pid_manager
|
||||
return get_bot_pid_manager
|
||||
|
||||
except ImportError:
|
||||
# В изолированном запуске PID менеджер не нужен
|
||||
logger.info("PID менеджер недоступен (изолированный запуск), PID файл не создается")
|
||||
return None
|
||||
|
||||
# Получаем функцию создания PID менеджера
|
||||
get_bot_pid_manager = get_pid_manager()
|
||||
|
||||
|
||||
async def main():
|
||||
"""Основная функция запуска"""
|
||||
# Создаем PID менеджер для отслеживания процесса (если доступен)
|
||||
pid_manager = None
|
||||
if get_bot_pid_manager:
|
||||
pid_manager = get_bot_pid_manager("helper_bot")
|
||||
if not pid_manager.create_pid_file():
|
||||
logger.error("Не удалось создать PID файл, завершаем работу")
|
||||
return
|
||||
else:
|
||||
logger.info("PID менеджер недоступен, запуск без PID файла")
|
||||
|
||||
bdf = get_global_instance()
|
||||
|
||||
@@ -111,9 +81,6 @@ async def main():
|
||||
# Отменяем задачу бота
|
||||
bot_task.cancel()
|
||||
|
||||
# Очищаем PID файл (если PID менеджер доступен)
|
||||
if pid_manager:
|
||||
pid_manager.cleanup_pid_file()
|
||||
|
||||
# Ждем завершения задачи бота и получаем результат main bot
|
||||
try:
|
||||
@@ -145,9 +112,22 @@ async def main():
|
||||
|
||||
logger.info("Бот корректно остановлен")
|
||||
|
||||
def init_db():
|
||||
db_path = '/app/database/tg-bot-database.db'
|
||||
schema_path = '/app/database/schema.sql'
|
||||
|
||||
if not os.path.exists(db_path):
|
||||
print("Initializing database...")
|
||||
with open(schema_path, 'r') as f:
|
||||
schema = f.read()
|
||||
|
||||
with sqlite3.connect(db_path) as conn:
|
||||
conn.executescript(schema)
|
||||
print("Database initialized successfully")
|
||||
|
||||
if __name__ == '__main__':
|
||||
try:
|
||||
init_db()
|
||||
asyncio.run(main())
|
||||
except AttributeError:
|
||||
# Fallback for Python 3.6-3.7
|
||||
|
||||
Reference in New Issue
Block a user