remove lock

This commit is contained in:
emmatveev 2024-04-30 11:28:13 +03:00
parent 7dc6df0182
commit 0f16bff9ac
2 changed files with 14 additions and 21 deletions

30
bot.py
View File

@ -42,14 +42,13 @@ 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'] else:
else: another_chat_id = current_dialog['chat_id_1']
another_chat_id = current_dialog['chat_id_1'] self.send_message('🤖 Диалог окончен, жду тебя снова!')
self.send_message('🤖 Диалог окончен, жду тебя снова!') self.start_new_dialog([another_chat_id])
self.start_new_dialog([another_chat_id])
return return
if self.state == 'pause': if self.state == 'pause':
self.send_message('🤖 Сейчас твой аккаунт не активен. Активируй его с помощью команды /start') self.send_message('🤖 Сейчас твой аккаунт не активен. Активируй его с помощью команды /start')
@ -58,14 +57,13 @@ 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']]) return
return else:
else: self.start_new_dialog([self.chat_id])
self.start_new_dialog([self.chat_id]) return
return
def handle_state_search(self): def handle_state_search(self):
self.send_message('🤖 Поиски собеседника продолжаются') self.send_message('🤖 Поиски собеседника продолжаются')

View File

@ -23,11 +23,6 @@ class RedisClient:
with self.cli as cli: with self.cli as cli:
cli.set(f"ruletka_{key}", value) cli.set(f"ruletka_{key}", value)
@contextlib.contextmanager
def lock(self, key):
with self.cli.lock(f"ruletka_{key}"):
yield
redis_client = RedisClient( redis_client = RedisClient(
settings.REDIS_HOST, settings.REDIS_HOST,