Compare commits
30 Commits
queues
...
b23ab7b97f
| Author | SHA1 | Date | |
|---|---|---|---|
| b23ab7b97f | |||
| 0046701dbb | |||
| 09857b47b7 | |||
| e7109cc254 | |||
| 99ceb4c716 | |||
| dc7c222f50 | |||
| 3d195df1a7 | |||
| 2d051a1881 | |||
| 0e836b1d92 | |||
| e6e3762768 | |||
| ff9fe98a3e | |||
| 8a4a6ac217 | |||
| 2cc67efebb | |||
| 3d379bc6d7 | |||
| 50d408554e | |||
| 443b943bee | |||
| 1d543461e9 | |||
| 37ac151e68 | |||
| 589eb70f69 | |||
| 53e929fe5a | |||
| 5c35987d15 | |||
| 09f2a267b2 | |||
| 5042df4874 | |||
| 7fd853f300 | |||
| 6a0471dc15 | |||
| cc290465a1 | |||
| 1d0abfed03 | |||
| e17c5ab7fe | |||
| ff7f0ffa91 | |||
| 71a12ef77b |
@@ -2,23 +2,6 @@ version: "3.4"
|
||||
|
||||
|
||||
services:
|
||||
|
||||
poll:
|
||||
image: mathwave/sprint-repo:pizda-bot
|
||||
command: poll
|
||||
environment:
|
||||
TELEGRAM_TOKEN: $TELEGRAM_TOKEN_DEV
|
||||
STAGE: "development"
|
||||
networks:
|
||||
- queues
|
||||
deploy:
|
||||
mode: replicated
|
||||
restart_policy:
|
||||
condition: any
|
||||
update_config:
|
||||
parallelism: 1
|
||||
order: start-first
|
||||
|
||||
worker:
|
||||
image: mathwave/sprint-repo:pizda-bot
|
||||
command: worker
|
||||
@@ -26,25 +9,9 @@ services:
|
||||
MONGO_HOST: "mongo.develop.sprinthub.ru"
|
||||
MONGO_PASSWORD: $MONGO_PASSWORD_DEV
|
||||
STAGE: "development"
|
||||
PLATFORM_SECURITY_TOKEN: $PLATFORM_SECURITY_TOKEN
|
||||
networks:
|
||||
- queues
|
||||
deploy:
|
||||
mode: replicated
|
||||
restart_policy:
|
||||
condition: any
|
||||
update_config:
|
||||
parallelism: 1
|
||||
order: start-first
|
||||
|
||||
mailbox:
|
||||
image: mathwave/sprint-repo:pizda-bot
|
||||
command: mailbox
|
||||
environment:
|
||||
TELEGRAM_TOKEN: $TELEGRAM_TOKEN_DEV
|
||||
STAGE: "development"
|
||||
networks:
|
||||
- queues
|
||||
- queues-development
|
||||
- configurator
|
||||
deploy:
|
||||
mode: replicated
|
||||
restart_policy:
|
||||
@@ -63,7 +30,7 @@ services:
|
||||
PLATFORM_SECURITY_TOKEN: $PLATFORM_SECURITY_TOKEN
|
||||
STAGE: "development"
|
||||
networks:
|
||||
- common-infra-nginx
|
||||
- common-infra-nginx-development
|
||||
deploy:
|
||||
mode: replicated
|
||||
restart_policy:
|
||||
@@ -73,7 +40,9 @@ services:
|
||||
order: start-first
|
||||
|
||||
networks:
|
||||
common-infra-nginx:
|
||||
common-infra-nginx-development:
|
||||
external: true
|
||||
queues:
|
||||
queues-development:
|
||||
external: true
|
||||
configurator:
|
||||
external: true
|
||||
|
||||
@@ -29,6 +29,7 @@ services:
|
||||
PLATFORM_SECURITY_TOKEN: $PLATFORM_SECURITY_TOKEN
|
||||
networks:
|
||||
- queues
|
||||
- configurator
|
||||
deploy:
|
||||
mode: replicated
|
||||
restart_policy:
|
||||
@@ -77,3 +78,5 @@ networks:
|
||||
external: true
|
||||
queues:
|
||||
external: true
|
||||
configurator:
|
||||
external: true
|
||||
|
||||
@@ -28,7 +28,7 @@ jobs:
|
||||
run: docker push mathwave/sprint-repo:pizda-bot
|
||||
deploy-dev:
|
||||
name: Deploy dev
|
||||
runs-on: [dev]
|
||||
runs-on: [prod]
|
||||
needs: push
|
||||
steps:
|
||||
- name: login
|
||||
@@ -42,4 +42,4 @@ jobs:
|
||||
TELEGRAM_TOKEN_DEV: ${{ secrets.TELEGRAM_TOKEN_DEV }}
|
||||
MONGO_PASSWORD_DEV: ${{ secrets.MONGO_PASSWORD_DEV }}
|
||||
PLATFORM_SECURITY_TOKEN: ${{ secrets.PLATFORM_SECURITY_TOKEN }}
|
||||
run: docker stack deploy --with-registry-auth -c ./.deploy/deploy-dev.yaml pizda-bot
|
||||
run: docker stack deploy --with-registry-auth -c ./.deploy/deploy-dev.yaml pizda-bot-development
|
||||
|
||||
@@ -1,27 +0,0 @@
|
||||
import telebot
|
||||
import os
|
||||
|
||||
from daemons import base
|
||||
from utils import queues
|
||||
|
||||
|
||||
class Daemon(base.BaseDaemon, queues.TasksHandlerMixin):
|
||||
def __init__(self):
|
||||
super().__init__()
|
||||
self.bot = telebot.TeleBot(os.getenv("TELEGRAM_TOKEN"))
|
||||
|
||||
@property
|
||||
def queue_name(self):
|
||||
return 'pizda_bot_mailbox'
|
||||
|
||||
def execute(self):
|
||||
self.poll()
|
||||
|
||||
def process(self, payload):
|
||||
body = {
|
||||
'chat_id': payload['chat_id'],
|
||||
'text': payload['message'],
|
||||
}
|
||||
if payload['action'] == 'reply':
|
||||
body['reply_to_message_id'] = payload['reply_to']
|
||||
self.bot.send_message(**body)
|
||||
@@ -1,16 +0,0 @@
|
||||
import os
|
||||
import json
|
||||
import telebot
|
||||
|
||||
from daemons import base
|
||||
from telebot import types
|
||||
from utils import queues
|
||||
|
||||
|
||||
class Daemon(base.BaseDaemon):
|
||||
def execute(self):
|
||||
bot = telebot.TeleBot(os.getenv("TELEGRAM_TOKEN"))
|
||||
@bot.message_handler()
|
||||
def do_action(message: types.Message):
|
||||
queues.set_task('pizda_bot_worker', message.json, 1)
|
||||
bot.polling()
|
||||
@@ -49,23 +49,29 @@ class Daemon(base.BaseDaemon, queues.TasksHandlerMixin):
|
||||
|
||||
def reply(self, text: str, chat_id: int, message_id: int):
|
||||
queues.set_task(
|
||||
'pizda_bot_mailbox',
|
||||
'botalka_mailbox',
|
||||
{
|
||||
'action': 'reply',
|
||||
'message': text,
|
||||
'reply_to': message_id,
|
||||
'chat_id': chat_id
|
||||
'project': 'pizda-bot',
|
||||
'name': 'telegram-bot',
|
||||
'body': {
|
||||
'message': text,
|
||||
'reply_to_message_id': message_id,
|
||||
'chat_id': chat_id,
|
||||
}
|
||||
},
|
||||
1,
|
||||
)
|
||||
|
||||
def send(self, text: str, chat_id: int):
|
||||
queues.set_task(
|
||||
'pizda_bot_mailbox',
|
||||
'botalka_mailbox',
|
||||
{
|
||||
'action': 'send',
|
||||
'message': text,
|
||||
'chat_id': chat_id
|
||||
'project': 'pizda-bot',
|
||||
'name': 'telegram-bot',
|
||||
'body': {
|
||||
'message': text,
|
||||
'chat_id': chat_id,
|
||||
}
|
||||
},
|
||||
1,
|
||||
)
|
||||
|
||||
8
main.py
8
main.py
@@ -2,15 +2,9 @@ import sys
|
||||
|
||||
|
||||
arg = sys.argv[-1]
|
||||
if arg == 'poll':
|
||||
from daemons import poll
|
||||
daemon = poll.Daemon()
|
||||
elif arg == 'worker':
|
||||
if arg == 'worker':
|
||||
from daemons import worker
|
||||
daemon = worker.Daemon()
|
||||
elif arg == 'mailbox':
|
||||
from daemons import mailbox
|
||||
daemon = mailbox.Daemon()
|
||||
else:
|
||||
from api import app
|
||||
app.run(host="0.0.0.0", port=1238)
|
||||
|
||||
@@ -14,11 +14,8 @@ class PlatformClient:
|
||||
self.stage = stage
|
||||
self.configs = configs
|
||||
self.experiments = experiments
|
||||
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.endpoint = 'http://configurator/'
|
||||
self.fetch_url = urllib.parse.urljoin(self.endpoint, '/api/v1/fetch')
|
||||
self.config_storage = {}
|
||||
self.experiment_storage = {}
|
||||
self.staff_storage = {}
|
||||
@@ -43,7 +40,6 @@ class PlatformClient:
|
||||
try:
|
||||
response = get(
|
||||
url,
|
||||
headers={'X-Security-Token': self.platform_security_token},
|
||||
params=params
|
||||
)
|
||||
if response.status_code == 200:
|
||||
|
||||
@@ -18,22 +18,22 @@ class QueuesException(Exception):
|
||||
class TasksHandlerMixin:
|
||||
def poll(self):
|
||||
while True:
|
||||
response = requests.get(f'{QUEUES_URL}/api/v1/take', headers={'queue': self.queue_name})
|
||||
if response.status_code == 404:
|
||||
response = requests.get(f'{QUEUES_URL}/api/v1/take', headers={'queue': self.queue_name}).json()
|
||||
task = response.get('task')
|
||||
if not task:
|
||||
time.sleep(0.2)
|
||||
continue
|
||||
data = response.json()
|
||||
try:
|
||||
self.process(data['payload'])
|
||||
self.process(task['payload'])
|
||||
except Exception as exc:
|
||||
print(f'Error processing message id={data["id"]}, payload={data["payload"]}, exc={exc}')
|
||||
print(f'Error processing message id={task["id"]}, payload={task["payload"]}, exc={exc}')
|
||||
continue
|
||||
try:
|
||||
resp = requests.post(f'{QUEUES_URL}/api/v1/finish', json={'id': data['id']})
|
||||
resp = requests.post(f'{QUEUES_URL}/api/v1/finish', json={'id': task['id']})
|
||||
if resp.status_code != 202:
|
||||
raise QueuesException
|
||||
except:
|
||||
print(f'Failed to finish task id={data["id"]}')
|
||||
print(f'Failed to finish task id={task["id"]}')
|
||||
|
||||
@property
|
||||
def queue_name(self):
|
||||
|
||||
Reference in New Issue
Block a user