Compare commits
22 Commits
master
...
4e42f0927e
| Author | SHA1 | Date | |
|---|---|---|---|
| 4e42f0927e | |||
| 289701cbc0 | |||
| e1dbfb9e51 | |||
| b23ab7b97f | |||
| 09857b47b7 | |||
| 99ceb4c716 | |||
| 3d195df1a7 | |||
| 0e836b1d92 | |||
| ff9fe98a3e | |||
| 2cc67efebb | |||
| 50d408554e | |||
| 589eb70f69 | |||
| 5c35987d15 | |||
| 09f2a267b2 | |||
| 5042df4874 | |||
| 7fd853f300 | |||
| 6a0471dc15 | |||
| cc290465a1 | |||
| 1d0abfed03 | |||
| e17c5ab7fe | |||
| ff7f0ffa91 | |||
| 71a12ef77b |
@@ -6,13 +6,12 @@ services:
|
|||||||
image: mathwave/sprint-repo:pizda-bot
|
image: mathwave/sprint-repo:pizda-bot
|
||||||
command: worker
|
command: worker
|
||||||
environment:
|
environment:
|
||||||
MONGO_HOST: "mongo"
|
MONGO_HOST: "mongo.develop.sprinthub.ru"
|
||||||
MONGO_PASSWORD: $MONGO_PASSWORD_DEV
|
MONGO_PASSWORD: $MONGO_PASSWORD_DEV
|
||||||
STAGE: "development"
|
STAGE: "development"
|
||||||
networks:
|
networks:
|
||||||
- queues-development
|
- queues-development
|
||||||
- configurator
|
- configurator
|
||||||
- mongo-development
|
|
||||||
deploy:
|
deploy:
|
||||||
mode: replicated
|
mode: replicated
|
||||||
restart_policy:
|
restart_policy:
|
||||||
@@ -25,12 +24,11 @@ services:
|
|||||||
image: mathwave/sprint-repo:pizda-bot
|
image: mathwave/sprint-repo:pizda-bot
|
||||||
command: api
|
command: api
|
||||||
environment:
|
environment:
|
||||||
MONGO_HOST: "mongo"
|
MONGO_HOST: "mongo.develop.sprinthub.ru"
|
||||||
MONGO_PASSWORD: $MONGO_PASSWORD_DEV
|
MONGO_PASSWORD: $MONGO_PASSWORD_DEV
|
||||||
STAGE: "development"
|
STAGE: "development"
|
||||||
networks:
|
networks:
|
||||||
- common-infra-nginx-development
|
- common-infra-nginx-development
|
||||||
- mongo-development
|
|
||||||
deploy:
|
deploy:
|
||||||
mode: replicated
|
mode: replicated
|
||||||
restart_policy:
|
restart_policy:
|
||||||
@@ -46,5 +44,3 @@ networks:
|
|||||||
external: true
|
external: true
|
||||||
configurator:
|
configurator:
|
||||||
external: true
|
external: true
|
||||||
mongo-development:
|
|
||||||
external: true
|
|
||||||
|
|||||||
@@ -6,13 +6,12 @@ services:
|
|||||||
image: mathwave/sprint-repo:pizda-bot
|
image: mathwave/sprint-repo:pizda-bot
|
||||||
command: worker
|
command: worker
|
||||||
environment:
|
environment:
|
||||||
MONGO_HOST: "mongo"
|
MONGO_HOST: "mongo.sprinthub.ru"
|
||||||
MONGO_PASSWORD: $MONGO_PASSWORD_PROD
|
MONGO_PASSWORD: $MONGO_PASSWORD_PROD
|
||||||
STAGE: "production"
|
STAGE: "production"
|
||||||
networks:
|
networks:
|
||||||
- queues
|
- queues
|
||||||
- configurator
|
- configurator
|
||||||
- mongo
|
|
||||||
deploy:
|
deploy:
|
||||||
mode: replicated
|
mode: replicated
|
||||||
restart_policy:
|
restart_policy:
|
||||||
@@ -25,11 +24,10 @@ services:
|
|||||||
image: mathwave/sprint-repo:pizda-bot
|
image: mathwave/sprint-repo:pizda-bot
|
||||||
command: api
|
command: api
|
||||||
environment:
|
environment:
|
||||||
MONGO_HOST: "mongo"
|
MONGO_HOST: "mongo.sprinthub.ru"
|
||||||
MONGO_PASSWORD: $MONGO_PASSWORD_PROD
|
MONGO_PASSWORD: $MONGO_PASSWORD_PROD
|
||||||
networks:
|
networks:
|
||||||
- common-infra-nginx
|
- common-infra-nginx
|
||||||
- mongo
|
|
||||||
deploy:
|
deploy:
|
||||||
mode: replicated
|
mode: replicated
|
||||||
restart_policy:
|
restart_policy:
|
||||||
@@ -45,5 +43,3 @@ networks:
|
|||||||
external: true
|
external: true
|
||||||
configurator:
|
configurator:
|
||||||
external: true
|
external: true
|
||||||
mongo:
|
|
||||||
external: true
|
|
||||||
|
|||||||
4
.gitignore
vendored
4
.gitignore
vendored
@@ -116,6 +116,4 @@ GitHub.sublime-settings
|
|||||||
!.vscode/tasks.json
|
!.vscode/tasks.json
|
||||||
!.vscode/launch.json
|
!.vscode/launch.json
|
||||||
!.vscode/extensions.json
|
!.vscode/extensions.json
|
||||||
.history
|
.history
|
||||||
|
|
||||||
*pb2*
|
|
||||||
51
.gitlab-ci.yml
Normal file
51
.gitlab-ci.yml
Normal file
@@ -0,0 +1,51 @@
|
|||||||
|
stages:
|
||||||
|
- build
|
||||||
|
- deploy-dev
|
||||||
|
- deploy-prod
|
||||||
|
|
||||||
|
build:
|
||||||
|
stage: build
|
||||||
|
tags:
|
||||||
|
- dev
|
||||||
|
before_script:
|
||||||
|
- docker login -u mathwave -p $DOCKERHUB_PASSWORD
|
||||||
|
script:
|
||||||
|
- docker build -t mathwave/sprint-repo:pizda-bot .
|
||||||
|
|
||||||
|
push:
|
||||||
|
stage: build
|
||||||
|
tags:
|
||||||
|
- dev
|
||||||
|
before_script:
|
||||||
|
- docker login -u mathwave -p $DOCKERHUB_PASSWORD
|
||||||
|
script:
|
||||||
|
- docker push mathwave/sprint-repo:pizda-bot
|
||||||
|
|
||||||
|
.deploy:
|
||||||
|
before_script:
|
||||||
|
- docker login -u mathwave -p $DOCKERHUB_PASSWORD
|
||||||
|
|
||||||
|
deploy-dev:
|
||||||
|
extends:
|
||||||
|
- .deploy
|
||||||
|
stage: deploy-dev
|
||||||
|
tags:
|
||||||
|
- dev
|
||||||
|
rules:
|
||||||
|
- if: '$CI_COMMIT_BRANCH == "master"'
|
||||||
|
when: on_success
|
||||||
|
- when: manual
|
||||||
|
script:
|
||||||
|
- docker stack deploy --with-registry-auth -c ./.deploy/deploy-dev.yaml pizda-bot
|
||||||
|
|
||||||
|
deploy-prod:
|
||||||
|
extends:
|
||||||
|
- .deploy
|
||||||
|
stage: deploy-prod
|
||||||
|
tags:
|
||||||
|
- prod
|
||||||
|
only:
|
||||||
|
- master
|
||||||
|
when: manual
|
||||||
|
script:
|
||||||
|
- docker stack deploy --with-registry-auth -c ./.deploy/deploy-prod.yaml pizda-bot
|
||||||
@@ -2,7 +2,7 @@ from collections import defaultdict
|
|||||||
|
|
||||||
from flask import Flask
|
from flask import Flask
|
||||||
|
|
||||||
from utils.mongo import mongo
|
from mongo import mongo
|
||||||
|
|
||||||
app = Flask(__name__)
|
app = Flask(__name__)
|
||||||
|
|
||||||
@@ -1,13 +1,3 @@
|
|||||||
import os
|
class BaseDaemon:
|
||||||
|
|
||||||
|
|
||||||
stage = os.getenv("STAGE", 'local')
|
|
||||||
if stage == 'local':
|
|
||||||
QUEUES_URL = 'localhost:50051'
|
|
||||||
else:
|
|
||||||
QUEUES_URL = 'queues-grpc:50051'
|
|
||||||
|
|
||||||
|
|
||||||
class Daemon:
|
|
||||||
def execute(self):
|
def execute(self):
|
||||||
raise NotImplemented
|
raise NotImplementedError
|
||||||
|
|||||||
@@ -6,9 +6,9 @@ from utils import queues
|
|||||||
from telebot.types import Message
|
from telebot.types import Message
|
||||||
import json
|
import json
|
||||||
|
|
||||||
from utils.mongo import mongo
|
from mongo import mongo
|
||||||
from utils.sprint_platform import PlatformClient
|
from sprint_platform import PlatformClient
|
||||||
from utils.storage import set_values, get_chat_info
|
from storage import set_values, get_chat_info
|
||||||
|
|
||||||
security_token = os.getenv("PLATFORM_SECURITY_TOKEN")
|
security_token = os.getenv("PLATFORM_SECURITY_TOKEN")
|
||||||
stage = os.getenv("STAGE", 'local')
|
stage = os.getenv("STAGE", 'local')
|
||||||
@@ -35,15 +35,11 @@ def get_answers():
|
|||||||
return client.get_config('answers')
|
return client.get_config('answers')
|
||||||
|
|
||||||
|
|
||||||
def get_ignored_users():
|
|
||||||
return client.get_config('ignored_users')['users']
|
|
||||||
|
|
||||||
|
|
||||||
def get_replies():
|
def get_replies():
|
||||||
return client.get_config('replies')
|
return client.get_config('replies')
|
||||||
|
|
||||||
|
|
||||||
class Daemon(base.Daemon, queues.TasksHandlerMixin):
|
class Daemon(base.BaseDaemon, queues.TasksHandlerMixin):
|
||||||
@property
|
@property
|
||||||
def queue_name(self):
|
def queue_name(self):
|
||||||
return 'pizda_bot_worker'
|
return 'pizda_bot_worker'
|
||||||
@@ -122,9 +118,9 @@ class Daemon(base.Daemon, queues.TasksHandlerMixin):
|
|||||||
set_values(message.chat.id, state="set_probability")
|
set_values(message.chat.id, state="set_probability")
|
||||||
|
|
||||||
def process(self, payload):
|
def process(self, payload):
|
||||||
message: Message = Message.de_json(json.dumps(payload))
|
|
||||||
if not message.text:
|
if not message.text:
|
||||||
return
|
return
|
||||||
|
message: Message = Message.de_json(json.dumps(payload))
|
||||||
if message.text.startswith('/'):
|
if message.text.startswith('/'):
|
||||||
self.process_command(message)
|
self.process_command(message)
|
||||||
return
|
return
|
||||||
@@ -155,6 +151,6 @@ class Daemon(base.Daemon, queues.TasksHandlerMixin):
|
|||||||
else:
|
else:
|
||||||
return
|
return
|
||||||
ans = get_answers().get(convert_text)
|
ans = get_answers().get(convert_text)
|
||||||
if ans is not None and randrange(1, 101) <= info["probability"] and message.from_user.id not in get_ignored_users():
|
if ans is not None and randrange(1, 101) <= info["probability"]:
|
||||||
self.reply(ans, message.chat.id, message.message_id)
|
self.reply(ans, message.chat.id, message.message_id)
|
||||||
mongo.inc(message.from_user.username, message.chat.id)
|
mongo.inc(message.from_user.username, message.chat.id)
|
||||||
|
|||||||
0
launch.json
Normal file
0
launch.json
Normal file
5
local_platform.json
Normal file
5
local_platform.json
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
{
|
||||||
|
"configs": {},
|
||||||
|
"experiments": {},
|
||||||
|
"platform_staff": {}
|
||||||
|
}
|
||||||
2
main.py
2
main.py
@@ -6,7 +6,7 @@ if arg == 'worker':
|
|||||||
from daemons import worker
|
from daemons import worker
|
||||||
daemon = worker.Daemon()
|
daemon = worker.Daemon()
|
||||||
else:
|
else:
|
||||||
from daemons.api import app
|
from api import app
|
||||||
app.run(host="0.0.0.0", port=1238)
|
app.run(host="0.0.0.0", port=1238)
|
||||||
|
|
||||||
daemon.execute()
|
daemon.execute()
|
||||||
|
|||||||
@@ -1,14 +1,10 @@
|
|||||||
import pymongo
|
import pymongo
|
||||||
import os
|
import settings
|
||||||
|
|
||||||
MONGO_USER = os.getenv("MONGO_USER", "mongo")
|
|
||||||
MONGO_PASSWORD = os.getenv("MONGO_PASSWORD", "password")
|
|
||||||
MONGO_HOST = os.getenv("MONGO_HOST", "localhost")
|
|
||||||
|
|
||||||
|
|
||||||
class Mongo:
|
class Mongo:
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
url = f"mongodb://{MONGO_USER}:{MONGO_PASSWORD}@{MONGO_HOST}:27017/"
|
url = f"mongodb://{settings.MONGO_USER}:{settings.MONGO_PASSWORD}@{settings.MONGO_HOST}:27017/"
|
||||||
self.client = pymongo.MongoClient(url)
|
self.client = pymongo.MongoClient(url)
|
||||||
self.database = self.client.get_database("pizda-bot")
|
self.database = self.client.get_database("pizda-bot")
|
||||||
self.chats_collection.create_index([
|
self.chats_collection.create_index([
|
||||||
10
settings.py
Normal file
10
settings.py
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
import os
|
||||||
|
import sys
|
||||||
|
import zoneinfo
|
||||||
|
|
||||||
|
MONGO_USER = os.getenv("MONGO_USER", "mongo")
|
||||||
|
MONGO_PASSWORD = os.getenv("MONGO_PASSWORD", "password")
|
||||||
|
MONGO_HOST = os.getenv("MONGO_HOST", "localhost")
|
||||||
|
|
||||||
|
CACHE_SIZE = int(os.getenv("CACHE_SIZE", 1000))
|
||||||
|
CACHE_TTL = int(os.getenv("CACHE_TTL", 3600))
|
||||||
@@ -1,12 +1,9 @@
|
|||||||
from cachetools import TTLCache
|
from cachetools import TTLCache
|
||||||
import os
|
|
||||||
|
|
||||||
from utils.mongo import mongo
|
import settings
|
||||||
|
from mongo import mongo
|
||||||
|
|
||||||
CACHE_SIZE = int(os.getenv("CACHE_SIZE", 1000))
|
cache = TTLCache(settings.CACHE_SIZE, settings.CACHE_TTL)
|
||||||
CACHE_TTL = int(os.getenv("CACHE_TTL", 3600))
|
|
||||||
|
|
||||||
cache = TTLCache(CACHE_SIZE, CACHE_TTL)
|
|
||||||
|
|
||||||
|
|
||||||
def get_chat_info(chat_id: int) -> dict:
|
def get_chat_info(chat_id: int) -> dict:
|
||||||
@@ -2,7 +2,7 @@ import datetime
|
|||||||
import random
|
import random
|
||||||
from time import sleep
|
from time import sleep
|
||||||
|
|
||||||
from utils.mongo import mongo
|
from mongo import mongo
|
||||||
|
|
||||||
|
|
||||||
def update():
|
def update():
|
||||||
@@ -1,7 +1,5 @@
|
|||||||
from concurrent.futures import ThreadPoolExecutor
|
import json
|
||||||
import datetime
|
|
||||||
import os
|
import os
|
||||||
import zoneinfo
|
|
||||||
import requests
|
import requests
|
||||||
import time
|
import time
|
||||||
|
|
||||||
@@ -18,54 +16,24 @@ class QueuesException(Exception):
|
|||||||
|
|
||||||
|
|
||||||
class TasksHandlerMixin:
|
class TasksHandlerMixin:
|
||||||
def __init__(self, *args, **kwargs):
|
|
||||||
super().__init__(*args, **kwargs)
|
|
||||||
self.executor = ThreadPoolExecutor(max_workers=1)
|
|
||||||
|
|
||||||
def _send_metric(self, start: datetime.datetime, end: datetime.datetime, success: bool):
|
|
||||||
def send():
|
|
||||||
requests.post(f'{QUEUES_URL}/api/v1/metric', json={
|
|
||||||
'service': 'botalka',
|
|
||||||
'queue': self.queue_name,
|
|
||||||
'success': success,
|
|
||||||
'timestamp': start.strftime("%Y-%m-%dT%H:%M:%S") + "Z",
|
|
||||||
"success": success,
|
|
||||||
"execution_time_ms": (end - start).microseconds // 1000,
|
|
||||||
"environment": stage,
|
|
||||||
})
|
|
||||||
|
|
||||||
self.executor.submit(send)
|
|
||||||
|
|
||||||
def poll(self):
|
def poll(self):
|
||||||
while True:
|
while True:
|
||||||
try:
|
response = requests.get(f'{QUEUES_URL}/api/v1/take', headers={'queue': self.queue_name}).json()
|
||||||
response = requests.get(f'{QUEUES_URL}/api/v1/take', headers={'queue': self.queue_name}).json()
|
|
||||||
except requests.JSONDecodeError:
|
|
||||||
print('Unable to decode json')
|
|
||||||
time.sleep(3)
|
|
||||||
continue
|
|
||||||
task = response.get('task')
|
task = response.get('task')
|
||||||
if not task:
|
if not task:
|
||||||
time.sleep(0.2)
|
time.sleep(0.2)
|
||||||
continue
|
continue
|
||||||
start = datetime.datetime.now(zoneinfo.ZoneInfo("Europe/Moscow"))
|
|
||||||
try:
|
try:
|
||||||
print(f'process task with id {task["id"]}, attempt {task["attempt"]}')
|
|
||||||
self.process(task['payload'])
|
self.process(task['payload'])
|
||||||
success = True
|
|
||||||
except Exception as exc:
|
except Exception as exc:
|
||||||
print(f'Error processing message id={task["id"]}, payload={task["payload"]}, exc={exc}')
|
print(f'Error processing message id={task["id"]}, payload={task["payload"]}, exc={exc}')
|
||||||
success = False
|
continue
|
||||||
end = datetime.datetime.now(zoneinfo.ZoneInfo("Europe/Moscow"))
|
try:
|
||||||
if success:
|
resp = requests.post(f'{QUEUES_URL}/api/v1/finish', json={'id': task['id']})
|
||||||
try:
|
if resp.status_code != 202:
|
||||||
resp = requests.post(f'{QUEUES_URL}/api/v1/finish', json={'id': task['id']})
|
raise QueuesException
|
||||||
if resp.status_code != 202:
|
except:
|
||||||
raise QueuesException
|
print(f'Failed to finish task id={task["id"]}')
|
||||||
print(f'finish task with id {task["id"]}')
|
|
||||||
except:
|
|
||||||
print(f'Failed to finish task id={task["id"]}')
|
|
||||||
self._send_metric(start, end, success)
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def queue_name(self):
|
def queue_name(self):
|
||||||
|
|||||||
Reference in New Issue
Block a user