Compare commits
No commits in common. "dd2bcff3d7cfb52c79fe0ad6c40efc30d420107a" and "1415c0209e380061cad82102e86308cfbe7abb68" have entirely different histories.
dd2bcff3d7
...
1415c0209e
@ -10,12 +10,6 @@ services:
|
|||||||
TELEGRAM_TOKEN: $TELEGRAM_TOKEN_DEV
|
TELEGRAM_TOKEN: $TELEGRAM_TOKEN_DEV
|
||||||
MONGO_HOST: "mongo.develop.sprinthub.ru"
|
MONGO_HOST: "mongo.develop.sprinthub.ru"
|
||||||
MONGO_PASSWORD: $MONGO_PASSWORD_DEV
|
MONGO_PASSWORD: $MONGO_PASSWORD_DEV
|
||||||
MINIO_HOST: "minio.develop.sprinthub.ru"
|
|
||||||
MINIO_SECRET_KEY: $MINIO_SECRET_KEY_DEV
|
|
||||||
PLATFORM_SECURITY_TOKEN: $PLATFORM_SECURITY_TOKEN
|
|
||||||
STAGE: "development"
|
|
||||||
REDIS_HOST: "redis.develop.sprinthub.ru"
|
|
||||||
REDIS_PASSWORD: $REDIS_PASSWORD_DEV
|
|
||||||
networks:
|
networks:
|
||||||
- net
|
- net
|
||||||
deploy:
|
deploy:
|
||||||
@ -26,30 +20,6 @@ services:
|
|||||||
parallelism: 1
|
parallelism: 1
|
||||||
order: start-first
|
order: start-first
|
||||||
|
|
||||||
roulette-nginx:
|
|
||||||
image: mathwave/sprint-repo:roulette-bot
|
|
||||||
command: api
|
|
||||||
environment:
|
|
||||||
TELEGRAM_TOKEN: $TELEGRAM_TOKEN_DEV
|
|
||||||
MONGO_HOST: "mongo.develop.sprinthub.ru"
|
|
||||||
MONGO_PASSWORD: $MONGO_PASSWORD_DEV
|
|
||||||
MINIO_HOST: "minio.develop.sprinthub.ru"
|
|
||||||
MINIO_SECRET_KEY: $MINIO_SECRET_KEY_DEV
|
|
||||||
PLATFORM_SECURITY_TOKEN: $PLATFORM_SECURITY_TOKEN
|
|
||||||
STAGE: "development"
|
|
||||||
REDIS_HOST: "redis.develop.sprinthub.ru"
|
|
||||||
REDIS_PASSWORD: $REDIS_PASSWORD_DEV
|
|
||||||
networks:
|
|
||||||
- net
|
|
||||||
- common-infra-nginx
|
|
||||||
deploy:
|
|
||||||
mode: replicated
|
|
||||||
restart_policy:
|
|
||||||
condition: any
|
|
||||||
update_config:
|
|
||||||
parallelism: 1
|
|
||||||
order: start-first
|
|
||||||
|
|
||||||
networks:
|
networks:
|
||||||
net:
|
net:
|
||||||
driver: overlay
|
driver: overlay
|
||||||
|
@ -6,29 +6,6 @@ services:
|
|||||||
bot:
|
bot:
|
||||||
image: mathwave/sprint-repo:roulette-bot
|
image: mathwave/sprint-repo:roulette-bot
|
||||||
command: bot
|
command: bot
|
||||||
networks:
|
|
||||||
- net
|
|
||||||
environment:
|
|
||||||
TELEGRAM_TOKEN: $TELEGRAM_TOKEN_PROD
|
|
||||||
MONGO_HOST: "mongo.sprinthub.ru"
|
|
||||||
MONGO_PASSWORD: $MONGO_PASSWORD_PROD
|
|
||||||
MINIO_HOST: "minio.sprinthub.ru"
|
|
||||||
MINIO_SECRET_KEY: $MINIO_SECRET_KEY_PROD
|
|
||||||
PLATFORM_SECURITY_TOKEN: $PLATFORM_SECURITY_TOKEN
|
|
||||||
STAGE: "production"
|
|
||||||
REDIS_HOST: "redis.sprinthub.ru"
|
|
||||||
REDIS_PASSWORD: $REDIS_PASSWORD_PROD
|
|
||||||
deploy:
|
|
||||||
mode: replicated
|
|
||||||
restart_policy:
|
|
||||||
condition: any
|
|
||||||
update_config:
|
|
||||||
parallelism: 1
|
|
||||||
order: start-first
|
|
||||||
|
|
||||||
roulette-nginx:
|
|
||||||
image: mathwave/sprint-repo:roulette-bot
|
|
||||||
command: api
|
|
||||||
networks:
|
networks:
|
||||||
- net
|
- net
|
||||||
- common-infra-nginx
|
- common-infra-nginx
|
||||||
@ -36,12 +13,6 @@ services:
|
|||||||
TELEGRAM_TOKEN: $TELEGRAM_TOKEN_PROD
|
TELEGRAM_TOKEN: $TELEGRAM_TOKEN_PROD
|
||||||
MONGO_HOST: "mongo.sprinthub.ru"
|
MONGO_HOST: "mongo.sprinthub.ru"
|
||||||
MONGO_PASSWORD: $MONGO_PASSWORD_PROD
|
MONGO_PASSWORD: $MONGO_PASSWORD_PROD
|
||||||
MINIO_HOST: "minio.sprinthub.ru"
|
|
||||||
MINIO_SECRET_KEY: $MINIO_SECRET_KEY_PROD
|
|
||||||
PLATFORM_SECURITY_TOKEN: $PLATFORM_SECURITY_TOKEN
|
|
||||||
STAGE: "production"
|
|
||||||
REDIS_HOST: "redis.sprinthub.ru"
|
|
||||||
REDIS_PASSWORD: $REDIS_PASSWORD_PROD
|
|
||||||
deploy:
|
deploy:
|
||||||
mode: replicated
|
mode: replicated
|
||||||
restart_policy:
|
restart_policy:
|
||||||
|
@ -1,47 +0,0 @@
|
|||||||
name: Deploy Dev
|
|
||||||
|
|
||||||
on:
|
|
||||||
pull_request:
|
|
||||||
branches:
|
|
||||||
- dev
|
|
||||||
types: [closed]
|
|
||||||
|
|
||||||
jobs:
|
|
||||||
build:
|
|
||||||
name: Build
|
|
||||||
runs-on: [ dev ]
|
|
||||||
steps:
|
|
||||||
- name: login
|
|
||||||
run: docker login -u mathwave -p ${{ secrets.DOCKERHUB_PASSWORD }}
|
|
||||||
- name: checkout
|
|
||||||
uses: actions/checkout@v4
|
|
||||||
with:
|
|
||||||
ref: dev
|
|
||||||
- name: build
|
|
||||||
run: docker build -t mathwave/sprint-repo:roulette-bot .
|
|
||||||
push:
|
|
||||||
name: Push
|
|
||||||
runs-on: [ dev ]
|
|
||||||
needs: build
|
|
||||||
steps:
|
|
||||||
- name: push
|
|
||||||
run: docker push mathwave/sprint-repo:roulette-bot
|
|
||||||
deploy-dev:
|
|
||||||
name: Deploy dev
|
|
||||||
runs-on: [dev]
|
|
||||||
needs: push
|
|
||||||
steps:
|
|
||||||
- name: login
|
|
||||||
run: docker login -u mathwave -p ${{ secrets.DOCKERHUB_PASSWORD }}
|
|
||||||
- name: checkout
|
|
||||||
uses: actions/checkout@v4
|
|
||||||
with:
|
|
||||||
ref: dev
|
|
||||||
- name: deploy
|
|
||||||
env:
|
|
||||||
TELEGRAM_TOKEN_DEV: ${{ secrets.TELEGRAM_TOKEN_DEV }}
|
|
||||||
MONGO_PASSWORD_DEV: ${{ secrets.MONGO_PASSWORD_DEV }}
|
|
||||||
PLATFORM_SECURITY_TOKEN: ${{ secrets.PLATFORM_SECURITY_TOKEN }}
|
|
||||||
MINIO_SECRET_KEY_DEV: ${{ secrets.MINIO_SECRET_KEY_DEV }}
|
|
||||||
REDIS_PASSWORD_DEV: ${{ secrets.REDIS_PASSWORD_DEV }}
|
|
||||||
run: docker stack deploy --with-registry-auth -c ./.deploy/deploy-dev.yaml roulette-bot
|
|
@ -1,47 +0,0 @@
|
|||||||
name: Deploy Prod
|
|
||||||
|
|
||||||
on:
|
|
||||||
pull_request:
|
|
||||||
branches:
|
|
||||||
- prod
|
|
||||||
types: [closed]
|
|
||||||
|
|
||||||
jobs:
|
|
||||||
build:
|
|
||||||
name: Build
|
|
||||||
runs-on: [ dev ]
|
|
||||||
steps:
|
|
||||||
- name: login
|
|
||||||
run: docker login -u mathwave -p ${{ secrets.DOCKERHUB_PASSWORD }}
|
|
||||||
- name: checkout
|
|
||||||
uses: actions/checkout@v4
|
|
||||||
with:
|
|
||||||
ref: prod
|
|
||||||
- name: build
|
|
||||||
run: docker build -t mathwave/sprint-repo:roulette-bot .
|
|
||||||
push:
|
|
||||||
name: Push
|
|
||||||
runs-on: [ dev ]
|
|
||||||
needs: build
|
|
||||||
steps:
|
|
||||||
- name: push
|
|
||||||
run: docker push mathwave/sprint-repo:roulette-bot
|
|
||||||
deploy-prod:
|
|
||||||
name: Deploy prod
|
|
||||||
runs-on: [prod]
|
|
||||||
needs: push
|
|
||||||
steps:
|
|
||||||
- name: login
|
|
||||||
run: docker login -u mathwave -p ${{ secrets.DOCKERHUB_PASSWORD }}
|
|
||||||
- name: checkout
|
|
||||||
uses: actions/checkout@v4
|
|
||||||
with:
|
|
||||||
ref: prod
|
|
||||||
- name: deploy
|
|
||||||
env:
|
|
||||||
TELEGRAM_TOKEN_PROD: ${{ secrets.TELEGRAM_TOKEN_PROD }}
|
|
||||||
MONGO_PASSWORD_PROD: ${{ secrets.MONGO_PASSWORD_PROD }}
|
|
||||||
PLATFORM_SECURITY_TOKEN: ${{ secrets.PLATFORM_SECURITY_TOKEN }}
|
|
||||||
MINIO_SECRET_KEY_PROD: ${{ secrets.MINIO_SECRET_KEY_PROD }}
|
|
||||||
REDIS_PASSWORD_PROD: ${{ secrets.REDIS_PASSWORD_PROD }}
|
|
||||||
run: docker stack deploy --with-registry-auth -c ./.deploy/deploy-prod.yaml roulette-bot
|
|
24
api.py
24
api.py
@ -1,24 +0,0 @@
|
|||||||
from bson import ObjectId
|
|
||||||
from flask import Flask, request
|
|
||||||
|
|
||||||
from tools.mongo import mongo
|
|
||||||
|
|
||||||
app = Flask("roulette")
|
|
||||||
|
|
||||||
|
|
||||||
@app.route('/dialogs')
|
|
||||||
def main():
|
|
||||||
html = "<html><head></head><body>"
|
|
||||||
for d in mongo.dialogs_collection.find({}).sort([('started_at', -1)]):
|
|
||||||
html += f'<a href="/dialog?id={d["_id"]}">{d["_id"]}</a><br>'
|
|
||||||
html += "</body></html>"
|
|
||||||
return html
|
|
||||||
|
|
||||||
|
|
||||||
@app.route('/dialog')
|
|
||||||
def dialog():
|
|
||||||
html = "<html><head></head><body>"
|
|
||||||
for message in mongo.messages_collection.find({"dialog_id": ObjectId(request.args.get('id'))}).sort([('sent_at', 1)]):
|
|
||||||
html += f'{message["sender"]}: {message["text"]}<br>'
|
|
||||||
html += "</body></html>"
|
|
||||||
return html
|
|
70
bot.py
70
bot.py
@ -1,14 +1,9 @@
|
|||||||
import os
|
import os
|
||||||
import uuid
|
|
||||||
|
|
||||||
import requests
|
|
||||||
import telebot
|
import telebot
|
||||||
from telebot.types import Message, ReplyKeyboardRemove
|
from telebot.types import Message, ReplyKeyboardRemove
|
||||||
|
|
||||||
from tools.minio import minio_client as minio
|
from mongo import mongo
|
||||||
from tools.mongo import mongo
|
|
||||||
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"))
|
||||||
|
|
||||||
@ -17,7 +12,7 @@ class Core:
|
|||||||
def __init__(self, message: Message):
|
def __init__(self, message: Message):
|
||||||
self.message = message
|
self.message = message
|
||||||
self.chat_id = message.chat.id
|
self.chat_id = message.chat.id
|
||||||
self.message_text = message.text or message.caption or ""
|
self.message_text = message.text
|
||||||
print(f'Handled message from {self.chat_id}: {self.message_text}')
|
print(f'Handled message from {self.chat_id}: {self.message_text}')
|
||||||
user = mongo.chats_collection.find_one({"chat_id": message.chat.id})
|
user = mongo.chats_collection.find_one({"chat_id": message.chat.id})
|
||||||
if user is None:
|
if user is None:
|
||||||
@ -69,8 +64,6 @@ class Core:
|
|||||||
self.send_message('🤖 Поиски собеседника продолжаются')
|
self.send_message('🤖 Поиски собеседника продолжаются')
|
||||||
|
|
||||||
def send_message(self, text, chat_id=None, reply_markup=None, remove_keyboard=True, **kwargs):
|
def send_message(self, text, chat_id=None, reply_markup=None, remove_keyboard=True, **kwargs):
|
||||||
if not text:
|
|
||||||
return
|
|
||||||
if reply_markup is None and remove_keyboard:
|
if reply_markup is None and remove_keyboard:
|
||||||
reply_markup = ReplyKeyboardRemove()
|
reply_markup = ReplyKeyboardRemove()
|
||||||
bot.send_message(chat_id or self.chat_id, text, reply_markup=reply_markup, **kwargs)
|
bot.send_message(chat_id or self.chat_id, text, reply_markup=reply_markup, **kwargs)
|
||||||
@ -86,44 +79,13 @@ class Core:
|
|||||||
|
|
||||||
def handle_state_dialog(self):
|
def handle_state_dialog(self):
|
||||||
current_dialog = mongo.get_current_dialog(self.chat_id)
|
current_dialog = mongo.get_current_dialog(self.chat_id)
|
||||||
chat_to_send = current_dialog['chat_id_2'] if current_dialog['chat_id_1'] == self.chat_id else current_dialog['chat_id_1']
|
mongo.create_message(self.message_text, current_dialog['_id'], self.chat_id)
|
||||||
saves = platform.get_config('save')
|
if current_dialog['chat_id_1'] == self.chat_id:
|
||||||
if saves['messages']:
|
self.send_message(self.message_text, current_dialog['chat_id_2'])
|
||||||
res = mongo.create_message(self.message.content_type, self.message_text, current_dialog['_id'], self.chat_id).inserted_id
|
|
||||||
else:
|
else:
|
||||||
res = uuid.uuid4()
|
self.send_message(self.message_text, current_dialog['chat_id_1'])
|
||||||
if self.message.photo:
|
|
||||||
if saves['photos']:
|
|
||||||
photo = requests.get(bot.get_file_url(self.message.photo[-1].file_id)).content
|
|
||||||
minio.put_object(f"photos/{res}", photo)
|
|
||||||
bot.send_photo(chat_to_send, self.message.photo[-1].file_id)
|
|
||||||
if self.message.sticker:
|
|
||||||
if saves['stickers']:
|
|
||||||
sticker = requests.get(bot.get_file_url(self.message.sticker.file_id)).content
|
|
||||||
minio.put_object(f"stickers/{res}", sticker)
|
|
||||||
bot.send_sticker(chat_to_send, self.message.sticker.file_id)
|
|
||||||
if self.message.voice:
|
|
||||||
if saves['voices']:
|
|
||||||
voice = requests.get(bot.get_file_url(self.message.voice.file_id)).content
|
|
||||||
minio.put_object(f"voices/{res}", voice)
|
|
||||||
bot.send_voice(chat_to_send, self.message.voice.file_id)
|
|
||||||
if self.message.video_note:
|
|
||||||
if saves['video_notes']:
|
|
||||||
video_note = requests.get(bot.get_file_url(self.message.video_note.file_id)).content
|
|
||||||
minio.put_object(f"video_notes/{res}", video_note)
|
|
||||||
bot.send_video_note(chat_to_send, self.message.video_note.file_id)
|
|
||||||
if self.message.animation:
|
|
||||||
if saves['gifs']:
|
|
||||||
video_note = requests.get(bot.get_file_url(self.message.animation.file_id)).content
|
|
||||||
minio.put_object(f"gifs/{res}", video_note)
|
|
||||||
bot.send_animation(chat_to_send, self.message.animation.file_id)
|
|
||||||
self.send_message(self.message_text, chat_to_send)
|
|
||||||
|
|
||||||
def start_new_dialog(self, chat_ids):
|
def start_new_dialog(self, chat_ids):
|
||||||
for chat in chat_ids:
|
|
||||||
current_dialog = mongo.get_current_dialog(chat)
|
|
||||||
if current_dialog:
|
|
||||||
mongo.finish_dialog(current_dialog['_id'])
|
|
||||||
self.set_state('search', chat_ids)
|
self.set_state('search', chat_ids)
|
||||||
for chat in chat_ids:
|
for chat in chat_ids:
|
||||||
self.send_message("🤖 Начинаю искать собеседника. Сообщу тебе, когда найду его.", chat)
|
self.send_message("🤖 Начинаю искать собеседника. Сообщу тебе, когда найду его.", chat)
|
||||||
@ -137,24 +99,8 @@ class Core:
|
|||||||
|
|
||||||
|
|
||||||
def run_bot():
|
def run_bot():
|
||||||
@bot.message_handler(content_types=[
|
@bot.message_handler()
|
||||||
# 'audio',
|
|
||||||
'photo',
|
|
||||||
'voice',
|
|
||||||
'video_note',
|
|
||||||
# 'document',
|
|
||||||
'text',
|
|
||||||
'animation',
|
|
||||||
# 'location',
|
|
||||||
# 'contact',
|
|
||||||
'sticker'
|
|
||||||
]
|
|
||||||
)
|
|
||||||
def do_action(message: Message):
|
def do_action(message: Message):
|
||||||
try:
|
Core(message).process()
|
||||||
Core(message).process()
|
|
||||||
except Exception as e:
|
|
||||||
print(e)
|
|
||||||
|
|
||||||
print('bot is starting')
|
|
||||||
bot.polling()
|
bot.polling()
|
||||||
|
@ -1,14 +0,0 @@
|
|||||||
{
|
|
||||||
"configs": {
|
|
||||||
"save": {
|
|
||||||
"photos": false,
|
|
||||||
"voices": false,
|
|
||||||
"messages": false,
|
|
||||||
"stickers": false,
|
|
||||||
"video_notes": false,
|
|
||||||
"gifs": false
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"experiments": {},
|
|
||||||
"platform_staff": {}
|
|
||||||
}
|
|
3
main.py
3
main.py
@ -4,8 +4,5 @@ import sys
|
|||||||
if sys.argv[-1] == "bot":
|
if sys.argv[-1] == "bot":
|
||||||
from bot import run_bot
|
from bot import run_bot
|
||||||
run_bot()
|
run_bot()
|
||||||
elif sys.argv[-1] == "api":
|
|
||||||
from api import app
|
|
||||||
app.run(host="0.0.0.0", port=1238)
|
|
||||||
else:
|
else:
|
||||||
raise NotImplementedError
|
raise NotImplementedError
|
||||||
|
@ -16,16 +16,9 @@ class Mongo:
|
|||||||
('state', 1)
|
('state', 1)
|
||||||
])
|
])
|
||||||
self.dialogs_collection.create_index([
|
self.dialogs_collection.create_index([
|
||||||
("chat_id_1", 1),
|
("chat_id", 1),
|
||||||
('finished_at', 1)
|
('finished_at', 1)
|
||||||
])
|
])
|
||||||
self.dialogs_collection.create_index([
|
|
||||||
("chat_id_2", 1),
|
|
||||||
('finished_at', 1)
|
|
||||||
])
|
|
||||||
self.messages_collection.create_index([
|
|
||||||
('dialog_id', 1)
|
|
||||||
])
|
|
||||||
|
|
||||||
def __getitem__(self, item):
|
def __getitem__(self, item):
|
||||||
return self.database.get_collection(item)
|
return self.database.get_collection(item)
|
||||||
@ -51,9 +44,8 @@ class Mongo:
|
|||||||
def get_current_dialog(self, chat_id):
|
def get_current_dialog(self, chat_id):
|
||||||
return self.dialogs_collection.find_one({'$or': [{'chat_id_1': chat_id}, {'chat_id_2': chat_id}], 'finished_at': None})
|
return self.dialogs_collection.find_one({'$or': [{'chat_id_1': chat_id}, {'chat_id_2': chat_id}], 'finished_at': None})
|
||||||
|
|
||||||
def create_message(self, message_type, text, dialog_id, sender):
|
def create_message(self, text, dialog_id, sender):
|
||||||
return self.messages_collection.insert_one({
|
self.messages_collection.insert_one({
|
||||||
'message_type': message_type,
|
|
||||||
'dialog_id': dialog_id,
|
'dialog_id': dialog_id,
|
||||||
'text': text,
|
'text': text,
|
||||||
'sender': sender,
|
'sender': sender,
|
@ -1,20 +1,15 @@
|
|||||||
async-timeout==4.0.3
|
|
||||||
certifi==2022.12.7
|
certifi==2022.12.7
|
||||||
charset-normalizer==3.0.1
|
charset-normalizer==3.0.1
|
||||||
click==8.1.3
|
click==8.1.3
|
||||||
dnspython==2.3.0
|
dnspython==2.3.0
|
||||||
Flask==2.2.3
|
Flask==2.2.3
|
||||||
idna==3.4
|
idna==3.4
|
||||||
importlib-metadata==6.7.0
|
|
||||||
itsdangerous==2.1.2
|
itsdangerous==2.1.2
|
||||||
Jinja2==3.1.2
|
Jinja2==3.1.2
|
||||||
MarkupSafe==2.1.2
|
MarkupSafe==2.1.2
|
||||||
minio==7.1.13
|
minio==7.1.13
|
||||||
pymongo==4.3.3
|
pymongo==4.3.3
|
||||||
pyTelegramBotAPI==4.1.1
|
pyTelegramBotAPI==4.1.1
|
||||||
redis==5.0.4
|
|
||||||
requests==2.28.2
|
requests==2.28.2
|
||||||
typing_extensions==4.7.1
|
|
||||||
urllib3==1.26.14
|
urllib3==1.26.14
|
||||||
Werkzeug==2.2.3
|
Werkzeug==2.2.3
|
||||||
zipp==3.15.0
|
|
||||||
|
@ -3,11 +3,3 @@ import os
|
|||||||
MONGO_USER = os.getenv("MONGO_USER", "mongo")
|
MONGO_USER = os.getenv("MONGO_USER", "mongo")
|
||||||
MONGO_PASSWORD = os.getenv("MONGO_PASSWORD", "password")
|
MONGO_PASSWORD = os.getenv("MONGO_PASSWORD", "password")
|
||||||
MONGO_HOST = os.getenv("MONGO_HOST", "localhost")
|
MONGO_HOST = os.getenv("MONGO_HOST", "localhost")
|
||||||
|
|
||||||
MINIO_HOST = os.getenv("MINIO_HOST", "localhost") + ":9000"
|
|
||||||
MINIO_ACCESS_KEY = os.getenv("MINIO_ACCESS_KEY", "serviceminioadmin")
|
|
||||||
MINIO_SECRET_KEY = os.getenv("MINIO_SECRET_KEY", "minioadmin")
|
|
||||||
MINIO_BUCKET_NAME = 'ruletka'
|
|
||||||
|
|
||||||
REDIS_HOST = os.getenv("REDIS_HOST", "localhost")
|
|
||||||
REDIS_PASSWORD = os.getenv("REDIS_PASSWORD", None)
|
|
||||||
|
@ -1,45 +0,0 @@
|
|||||||
import io
|
|
||||||
|
|
||||||
from minio import Minio
|
|
||||||
from minio.error import MinioException
|
|
||||||
|
|
||||||
import settings
|
|
||||||
|
|
||||||
|
|
||||||
class Client:
|
|
||||||
|
|
||||||
def __init__(self, host: str, access_key: str, secret_key: str, bucket_name: str):
|
|
||||||
self.bucket_name = bucket_name
|
|
||||||
self.cli = Minio(
|
|
||||||
host,
|
|
||||||
access_key=access_key,
|
|
||||||
secret_key=secret_key,
|
|
||||||
secure=False
|
|
||||||
)
|
|
||||||
try:
|
|
||||||
self.cli.make_bucket(bucket_name)
|
|
||||||
except MinioException:
|
|
||||||
pass
|
|
||||||
|
|
||||||
def put_object(self, name: str, data: bytes):
|
|
||||||
self.cli.put_object(self.bucket_name, name, io.BytesIO(data), len(data))
|
|
||||||
|
|
||||||
def get_object(self, name: str) -> bytes:
|
|
||||||
try:
|
|
||||||
return self.cli.get_object(self.bucket_name, name).data
|
|
||||||
except MinioException:
|
|
||||||
return b""
|
|
||||||
|
|
||||||
def delete_object(self, name: str):
|
|
||||||
try:
|
|
||||||
self.cli.remove_object(self.bucket_name, name)
|
|
||||||
except MinioException:
|
|
||||||
pass
|
|
||||||
|
|
||||||
|
|
||||||
minio_client = Client(
|
|
||||||
settings.MINIO_HOST,
|
|
||||||
settings.MINIO_ACCESS_KEY,
|
|
||||||
settings.MINIO_SECRET_KEY,
|
|
||||||
settings.MINIO_BUCKET_NAME
|
|
||||||
)
|
|
@ -1,30 +0,0 @@
|
|||||||
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)
|
|
||||||
|
|
||||||
|
|
||||||
redis_client = RedisClient(
|
|
||||||
settings.REDIS_HOST,
|
|
||||||
settings.REDIS_PASSWORD
|
|
||||||
)
|
|
@ -1,98 +0,0 @@
|
|||||||
import json
|
|
||||||
import os
|
|
||||||
import typing
|
|
||||||
import urllib.parse
|
|
||||||
from threading import Thread
|
|
||||||
from time import sleep
|
|
||||||
|
|
||||||
from requests import get
|
|
||||||
|
|
||||||
|
|
||||||
class PlatformClient:
|
|
||||||
def __init__(self, platform_security_token: str, app_name: str, stage: str, need_poll: bool = True):
|
|
||||||
self.platform_security_token = platform_security_token
|
|
||||||
self.app_name = app_name
|
|
||||||
self.stage = stage
|
|
||||||
self.endpoint = 'https://platform.sprinthub.ru/'
|
|
||||||
self.configs_url = urllib.parse.urljoin(self.endpoint, 'configs/get')
|
|
||||||
self.experiments_url = urllib.parse.urljoin(self.endpoint, 'experiments/get')
|
|
||||||
self.staff_url = urllib.parse.urljoin(self.endpoint, 'is_staff')
|
|
||||||
self.fetch_url = urllib.parse.urljoin(self.endpoint, 'fetch')
|
|
||||||
self.config_storage = {}
|
|
||||||
self.experiment_storage = {}
|
|
||||||
self.staff_storage = {}
|
|
||||||
self.poll_data()
|
|
||||||
if need_poll:
|
|
||||||
self.poll_data_in_thread()
|
|
||||||
|
|
||||||
def poll_data_in_thread(self):
|
|
||||||
def inner():
|
|
||||||
while True:
|
|
||||||
sleep(30)
|
|
||||||
self.fetch()
|
|
||||||
|
|
||||||
Thread(target=inner, daemon=True).start()
|
|
||||||
|
|
||||||
def poll_data(self):
|
|
||||||
self.fetch(with_exception=True)
|
|
||||||
|
|
||||||
def request_with_retries(self, url, params, with_exception=False, retries_count=3):
|
|
||||||
exception_to_throw = None
|
|
||||||
for _ in range(retries_count):
|
|
||||||
try:
|
|
||||||
response = get(
|
|
||||||
url,
|
|
||||||
headers={'X-Security-Token': self.platform_security_token},
|
|
||||||
params=params
|
|
||||||
)
|
|
||||||
if response.status_code == 200:
|
|
||||||
return response.json()
|
|
||||||
print(f'Failed to request {url}, status_code={response.status_code}')
|
|
||||||
exception_to_throw = Exception('Not 200 status')
|
|
||||||
except Exception as exc:
|
|
||||||
print(exc)
|
|
||||||
exception_to_throw = exc
|
|
||||||
sleep(1)
|
|
||||||
print(f'Failed fetching with retries: {url}, {params}')
|
|
||||||
if with_exception:
|
|
||||||
raise exception_to_throw
|
|
||||||
|
|
||||||
def fetch(self, with_exception=False):
|
|
||||||
if self.stage == 'local':
|
|
||||||
local_platform = json.loads(open('local_platform.json', 'r').read())
|
|
||||||
self.config_storage = local_platform['configs']
|
|
||||||
self.experiment_storage = local_platform['experiments']
|
|
||||||
self.staff_storage = {
|
|
||||||
key: set(value)
|
|
||||||
for key, value in local_platform['platform_staff'].items()
|
|
||||||
}
|
|
||||||
return
|
|
||||||
response_data = self.request_with_retries(self.fetch_url, {
|
|
||||||
'project': self.app_name,
|
|
||||||
'stage': self.stage,
|
|
||||||
}, with_exception)
|
|
||||||
self.config_storage = response_data['configs']
|
|
||||||
self.experiment_storage = response_data['experiments']
|
|
||||||
self.staff_storage = {
|
|
||||||
key: set(value)
|
|
||||||
for key, value in response_data['platform_staff'].items()
|
|
||||||
}
|
|
||||||
|
|
||||||
def is_staff(self, **kwargs):
|
|
||||||
for key, value in kwargs.items():
|
|
||||||
if value in self.staff_storage[key]:
|
|
||||||
return True
|
|
||||||
return False
|
|
||||||
|
|
||||||
def get_config(self, name):
|
|
||||||
return self.config_storage[name]
|
|
||||||
|
|
||||||
def get_experiment(self, name):
|
|
||||||
return self.experiment_storage[name]
|
|
||||||
|
|
||||||
|
|
||||||
platform = PlatformClient(
|
|
||||||
os.getenv('PLATFORM_SECURITY_TOKEN'),
|
|
||||||
'Ruletka',
|
|
||||||
os.getenv('STAGE', 'local')
|
|
||||||
)
|
|
Loading…
Reference in New Issue
Block a user