Added loggers. Rename file. Update
This commit is contained in:
@@ -58,7 +58,7 @@ nvim .env
|
|||||||
5. Запустите бота:
|
5. Запустите бота:
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
python BybitBot_API.py
|
python run.py
|
||||||
```
|
```
|
||||||
|
|
||||||
## Настройка автономной работы
|
## Настройка автономной работы
|
||||||
|
@@ -2,15 +2,18 @@ import os
|
|||||||
|
|
||||||
current_directory = os.path.dirname(os.path.abspath(__file__))
|
current_directory = os.path.dirname(os.path.abspath(__file__))
|
||||||
log_directory = os.path.join(current_directory, 'loggers')
|
log_directory = os.path.join(current_directory, 'loggers')
|
||||||
|
error_log_directory = os.path.join(log_directory, 'errors')
|
||||||
os.makedirs(log_directory, exist_ok=True)
|
os.makedirs(log_directory, exist_ok=True)
|
||||||
|
os.makedirs(error_log_directory, exist_ok=True)
|
||||||
log_filename = os.path.join(log_directory, 'app.log')
|
log_filename = os.path.join(log_directory, 'app.log')
|
||||||
|
error_log_filename = os.path.join(error_log_directory, 'error.log')
|
||||||
|
|
||||||
LOGGING_CONFIG = {
|
LOGGING_CONFIG = {
|
||||||
"version": 1,
|
"version": 1,
|
||||||
"disable_existing_loggers": False,
|
"disable_existing_loggers": False,
|
||||||
"formatters": {
|
"formatters": {
|
||||||
"default": {
|
"default": {
|
||||||
"format": "%(asctime)s - %(name)s - %(levelname)s - %(message)s",
|
"format": "TELEGRAM: %(asctime)s - %(name)s - %(levelname)s - %(message)s",
|
||||||
"datefmt": "%Y-%m-%d %H:%M:%S", # Формат даты
|
"datefmt": "%Y-%m-%d %H:%M:%S", # Формат даты
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
@@ -23,90 +26,112 @@ LOGGING_CONFIG = {
|
|||||||
"backupCount": 7, # Количество сохраняемых архивов (0 - не сохранять)
|
"backupCount": 7, # Количество сохраняемых архивов (0 - не сохранять)
|
||||||
"formatter": "default",
|
"formatter": "default",
|
||||||
"encoding": "utf-8",
|
"encoding": "utf-8",
|
||||||
|
"level": "DEBUG",
|
||||||
|
},
|
||||||
|
"error_file": {
|
||||||
|
"class": "logging.handlers.TimedRotatingFileHandler",
|
||||||
|
"filename": error_log_filename,
|
||||||
|
"when": "midnight",
|
||||||
|
"interval": 1,
|
||||||
|
"backupCount": 30,
|
||||||
|
"formatter": "default",
|
||||||
|
"encoding": "utf-8",
|
||||||
|
"level": "ERROR",
|
||||||
},
|
},
|
||||||
"console": {
|
"console": {
|
||||||
"class": "logging.StreamHandler",
|
"class": "logging.StreamHandler",
|
||||||
"formatter": "default",
|
"formatter": "default",
|
||||||
|
"level": "DEBUG",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
"loggers": {
|
"loggers": {
|
||||||
"main": {
|
"run": {
|
||||||
"handlers": ["console", "timed_rotating_file"],
|
"handlers": ["console", "timed_rotating_file", "error_file"],
|
||||||
|
"level": "DEBUG",
|
||||||
|
"propagate": False,
|
||||||
|
},
|
||||||
|
"config": {
|
||||||
|
"handlers": ["console", "timed_rotating_file", "error_file"],
|
||||||
|
"level": "DEBUG",
|
||||||
|
"propagate": False,
|
||||||
|
},
|
||||||
|
"common": {
|
||||||
|
"handlers": ["console", "timed_rotating_file", "error_file"],
|
||||||
|
"level": "DEBUG",
|
||||||
|
"propagate": False,
|
||||||
|
},
|
||||||
|
"handlers_main": {
|
||||||
|
"handlers": ["console", "timed_rotating_file", "error_file"],
|
||||||
|
"level": "DEBUG",
|
||||||
|
"propagate": False,
|
||||||
|
},
|
||||||
|
"database": {
|
||||||
|
"handlers": ["console", "timed_rotating_file", "error_file"],
|
||||||
|
"level": "DEBUG",
|
||||||
|
"propagate": False,
|
||||||
|
},
|
||||||
|
"request": {
|
||||||
|
"handlers": ["console", "timed_rotating_file", "error_file"],
|
||||||
"level": "DEBUG",
|
"level": "DEBUG",
|
||||||
"propagate": False,
|
"propagate": False,
|
||||||
},
|
},
|
||||||
"add_bybit_api": {
|
"add_bybit_api": {
|
||||||
"handlers": ["console", "timed_rotating_file"],
|
"handlers": ["console", "timed_rotating_file", "error_file"],
|
||||||
"level": "DEBUG",
|
"level": "DEBUG",
|
||||||
"propagate": False,
|
"propagate": False,
|
||||||
},
|
},
|
||||||
"balance": {
|
"profile_tg": {
|
||||||
"handlers": ["console", "timed_rotating_file"],
|
"handlers": ["console", "timed_rotating_file", "error_file"],
|
||||||
"level": "DEBUG",
|
"level": "DEBUG",
|
||||||
"propagate": False,
|
"propagate": False,
|
||||||
},
|
},
|
||||||
"functions": {
|
"settings": {
|
||||||
"handlers": ["console", "timed_rotating_file"],
|
"handlers": ["console", "timed_rotating_file", "error_file"],
|
||||||
"level": "DEBUG",
|
"level": "DEBUG",
|
||||||
"propagate": False,
|
"propagate": False,
|
||||||
},
|
},
|
||||||
"futures": {
|
"additional_settings": {
|
||||||
"handlers": ["console", "timed_rotating_file"],
|
"handlers": ["console", "timed_rotating_file", "error_file"],
|
||||||
"level": "DEBUG",
|
"level": "DEBUG",
|
||||||
"propagate": False,
|
"propagate": False,
|
||||||
},
|
},
|
||||||
"get_valid_symbol": {
|
"helper_functions": {
|
||||||
"handlers": ["console", "timed_rotating_file"],
|
"handlers": ["console", "timed_rotating_file", "error_file"],
|
||||||
"level": "DEBUG",
|
"level": "DEBUG",
|
||||||
"propagate": False,
|
"propagate": False,
|
||||||
},
|
},
|
||||||
"min_qty": {
|
"risk_management": {
|
||||||
"handlers": ["console", "timed_rotating_file"],
|
"handlers": ["console", "timed_rotating_file", "error_file"],
|
||||||
"level": "DEBUG",
|
"level": "DEBUG",
|
||||||
"propagate": False,
|
"propagate": False,
|
||||||
},
|
},
|
||||||
"price_symbol": {
|
"start_trading": {
|
||||||
"handlers": ["console", "timed_rotating_file"],
|
"handlers": ["console", "timed_rotating_file", "error_file"],
|
||||||
"level": "DEBUG",
|
"level": "DEBUG",
|
||||||
"propagate": False,
|
"propagate": False,
|
||||||
},
|
},
|
||||||
"requests": {
|
"changing_the_symbol": {
|
||||||
"handlers": ["console", "timed_rotating_file"],
|
"handlers": ["console", "timed_rotating_file", "error_file"],
|
||||||
"level": "DEBUG",
|
"level": "DEBUG",
|
||||||
"propagate": False,
|
"propagate": False,
|
||||||
},
|
},
|
||||||
"handlers": {
|
"conditional_settings": {
|
||||||
"handlers": ["console", "timed_rotating_file"],
|
"handlers": ["console", "timed_rotating_file", "error_file"],
|
||||||
"level": "DEBUG",
|
"level": "DEBUG",
|
||||||
"propagate": False,
|
"propagate": False,
|
||||||
},
|
},
|
||||||
"condition_settings": {
|
"get_positions_handlers": {
|
||||||
"handlers": ["console", "timed_rotating_file"],
|
"handlers": ["console", "timed_rotating_file", "error_file"],
|
||||||
"level": "DEBUG",
|
"level": "DEBUG",
|
||||||
"propagate": False,
|
"propagate": False,
|
||||||
},
|
},
|
||||||
"main_settings": {
|
"close_orders": {
|
||||||
"handlers": ["console", "timed_rotating_file"],
|
"handlers": ["console", "timed_rotating_file", "error_file"],
|
||||||
"level": "DEBUG",
|
"level": "DEBUG",
|
||||||
"propagate": False,
|
"propagate": False,
|
||||||
},
|
},
|
||||||
"risk_management_settings": {
|
"tp_sl_handlers": {
|
||||||
"handlers": ["console", "timed_rotating_file"],
|
"handlers": ["console", "timed_rotating_file", "error_file"],
|
||||||
"level": "DEBUG",
|
|
||||||
"propagate": False,
|
|
||||||
},
|
|
||||||
"models": {
|
|
||||||
"handlers": ["console", "timed_rotating_file"],
|
|
||||||
"level": "DEBUG",
|
|
||||||
"propagate": False,
|
|
||||||
},
|
|
||||||
"bybit_ws": {
|
|
||||||
"handlers": ["console", "timed_rotating_file"],
|
|
||||||
"level": "DEBUG",
|
|
||||||
"propagate": False,
|
|
||||||
},
|
|
||||||
"tasks": {
|
|
||||||
"handlers": ["console", "timed_rotating_file"],
|
|
||||||
"level": "DEBUG",
|
"level": "DEBUG",
|
||||||
"propagate": False,
|
"propagate": False,
|
||||||
},
|
},
|
||||||
|
56
run.py
Normal file
56
run.py
Normal file
@@ -0,0 +1,56 @@
|
|||||||
|
import asyncio
|
||||||
|
import contextlib
|
||||||
|
import logging.config
|
||||||
|
|
||||||
|
from aiogram import Bot, Dispatcher
|
||||||
|
from aiogram.fsm.storage.redis import RedisStorage
|
||||||
|
|
||||||
|
from app.bybit.web_socket import WebSocketBot
|
||||||
|
from app.telegram.handlers import router
|
||||||
|
from config import BOT_TOKEN
|
||||||
|
from database import init_db
|
||||||
|
from logger_helper.logger_helper import LOGGING_CONFIG
|
||||||
|
|
||||||
|
logging.config.dictConfig(LOGGING_CONFIG)
|
||||||
|
logger = logging.getLogger("run")
|
||||||
|
|
||||||
|
|
||||||
|
async def main():
|
||||||
|
"""
|
||||||
|
The main function of launching the bot.
|
||||||
|
|
||||||
|
Performs database initialization, creation of bot and dispatcher objects,
|
||||||
|
then it triggers the long polling event.
|
||||||
|
|
||||||
|
Logs important events and errors.
|
||||||
|
"""
|
||||||
|
try:
|
||||||
|
await init_db()
|
||||||
|
bot = Bot(token=BOT_TOKEN)
|
||||||
|
storage = RedisStorage.from_url("redis://localhost:6379")
|
||||||
|
dp = Dispatcher(storage=storage)
|
||||||
|
dp.include_router(router)
|
||||||
|
web_socket = WebSocketBot(telegram_bot=bot)
|
||||||
|
await web_socket.clear_user_sockets()
|
||||||
|
ws_task = asyncio.create_task(web_socket.run_user_check_loop())
|
||||||
|
tg_task = asyncio.create_task(dp.start_polling(bot))
|
||||||
|
|
||||||
|
try:
|
||||||
|
logger.info("Bot started")
|
||||||
|
await asyncio.gather(ws_task, tg_task)
|
||||||
|
except Exception as e:
|
||||||
|
logger.error("Bot stopped with error: %s", e)
|
||||||
|
finally:
|
||||||
|
for task in (ws_task, tg_task):
|
||||||
|
task.cancel()
|
||||||
|
with contextlib.suppress(asyncio.CancelledError):
|
||||||
|
await ws_task
|
||||||
|
await tg_task
|
||||||
|
await web_socket.clear_user_sockets()
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
logger.error("Bot stopped with error: %s", e)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
asyncio.run(main())
|
Reference in New Issue
Block a user