This commit is contained in:
emmatveev 2024-04-29 15:05:05 +03:00
parent c6f5c44d00
commit 7dc6df0182
5 changed files with 59 additions and 14 deletions

View File

@ -14,6 +14,8 @@ services:
MINIO_SECRET_KEY: $MINIO_SECRET_KEY_DEV MINIO_SECRET_KEY: $MINIO_SECRET_KEY_DEV
PLATFORM_SECURITY_TOKEN: $PLATFORM_SECURITY_TOKEN PLATFORM_SECURITY_TOKEN: $PLATFORM_SECURITY_TOKEN
STAGE: "development" STAGE: "development"
REDIS_HOST: "redis.develop.sprinthub.ru"
REDIS_PASSWORD: $REDIS_PASSWORD_DEV
networks: networks:
- net - net
deploy: deploy:

View File

@ -17,6 +17,8 @@ services:
MINIO_SECRET_KEY: $MINIO_SECRET_KEY_PROD MINIO_SECRET_KEY: $MINIO_SECRET_KEY_PROD
PLATFORM_SECURITY_TOKEN: $PLATFORM_SECURITY_TOKEN PLATFORM_SECURITY_TOKEN: $PLATFORM_SECURITY_TOKEN
STAGE: "production" STAGE: "production"
REDIS_HOST: "redis.sprinthub.ru"
REDIS_PASSWORD: $REDIS_PASSWORD_PROD
deploy: deploy:
mode: replicated mode: replicated
restart_policy: restart_policy:

3
bot.py
View File

@ -8,6 +8,7 @@ from telebot.types import Message, ReplyKeyboardRemove
from tools.minio import minio_client as minio from tools.minio import minio_client as minio
from tools.mongo import mongo from tools.mongo import mongo
from tools.sprint_platform import platform from tools.sprint_platform import platform
from tools.redis import redis_client as redis
bot = telebot.TeleBot(os.getenv("TELEGRAM_TOKEN")) bot = telebot.TeleBot(os.getenv("TELEGRAM_TOKEN"))
@ -41,6 +42,7 @@ class Core:
self.set_state('pause') self.set_state('pause')
if self.state == 'dialog': if self.state == 'dialog':
current_dialog = mongo.get_current_dialog(self.chat_id) current_dialog = mongo.get_current_dialog(self.chat_id)
with redis.lock('search'):
mongo.finish_dialog(current_dialog['_id']) mongo.finish_dialog(current_dialog['_id'])
if self.chat_id == current_dialog['chat_id_1']: if self.chat_id == current_dialog['chat_id_1']:
another_chat_id = current_dialog['chat_id_2'] another_chat_id = current_dialog['chat_id_2']
@ -56,6 +58,7 @@ class Core:
self.send_message('🤖 Поиск собеседника окончен, жду тебя снова!') self.send_message('🤖 Поиск собеседника окончен, жду тебя снова!')
return return
if self.message_text == '/next' or self.message_text == '/start': if self.message_text == '/next' or self.message_text == '/start':
with redis.lock('search'):
if self.state == 'dialog': if self.state == 'dialog':
dialog = mongo.get_current_dialog(self.chat_id) dialog = mongo.get_current_dialog(self.chat_id)
self.start_new_dialog([dialog['chat_id_1'], dialog['chat_id_2']]) self.start_new_dialog([dialog['chat_id_1'], dialog['chat_id_2']])

View File

@ -8,3 +8,6 @@ MINIO_HOST = os.getenv("MINIO_HOST", "localhost") + ":9000"
MINIO_ACCESS_KEY = os.getenv("MINIO_ACCESS_KEY", "serviceminioadmin") MINIO_ACCESS_KEY = os.getenv("MINIO_ACCESS_KEY", "serviceminioadmin")
MINIO_SECRET_KEY = os.getenv("MINIO_SECRET_KEY", "minioadmin") MINIO_SECRET_KEY = os.getenv("MINIO_SECRET_KEY", "minioadmin")
MINIO_BUCKET_NAME = 'ruletka' MINIO_BUCKET_NAME = 'ruletka'
REDIS_HOST = os.getenv("REDIS_HOST", "localhost")
REDIS_PASSWORD = os.getenv("REDIS_PASSWORD", None)

35
tools/redis.py Normal file
View File

@ -0,0 +1,35 @@
import contextlib
import redis
import settings
class RedisClient:
def __init__(self, host, password=None):
kwargs = {
"host": host,
}
if password:
kwargs['password'] = password
self.cli = redis.Redis(**kwargs)
def get(self, key):
with self.cli as cli:
return cli.get(f"ruletka_{key}")
def set(self, key, value):
with self.cli as cli:
cli.set(f"ruletka_{key}", value)
@contextlib.contextmanager
def lock(self, key):
with self.cli.lock(f"ruletka_{key}"):
yield
redis_client = RedisClient(
settings.REDIS_HOST,
settings.REDIS_PASSWORD
)