daemons
This commit is contained in:
1
daemons/__init__.py
Normal file
1
daemons/__init__.py
Normal file
@@ -0,0 +1 @@
|
||||
from .management.commands.bot import bot
|
6
daemons/apps.py
Normal file
6
daemons/apps.py
Normal file
@@ -0,0 +1,6 @@
|
||||
from django.apps import AppConfig
|
||||
|
||||
|
||||
class DaemonsConfig(AppConfig):
|
||||
default_auto_field = 'django.db.models.BigAutoField'
|
||||
name = 'daemons'
|
0
daemons/management/__init__.py
Normal file
0
daemons/management/__init__.py
Normal file
0
daemons/management/commands/__init__.py
Normal file
0
daemons/management/commands/__init__.py
Normal file
20
daemons/management/commands/apply_languages.py
Normal file
20
daemons/management/commands/apply_languages.py
Normal file
@@ -0,0 +1,20 @@
|
||||
from django.core.management.base import BaseCommand
|
||||
|
||||
from Main.models import LanguageApply, Set
|
||||
from SprintLib.language import languages
|
||||
|
||||
|
||||
class Command(BaseCommand):
|
||||
help = "starts FileStorage"
|
||||
|
||||
def handle(self, *args, **options):
|
||||
for language in languages:
|
||||
apply = LanguageApply.objects.filter(language_id=language.id, applied=True).first()
|
||||
if apply is None:
|
||||
for s in Set.objects.filter(auto_add_new_languages=True):
|
||||
if language.id not in s.languages:
|
||||
s.languages.append(language.id)
|
||||
s.save()
|
||||
obj, _ = LanguageApply.objects.get_or_create(language_id=language.id)
|
||||
obj.applied = True
|
||||
obj.save()
|
86
daemons/management/commands/bot.py
Normal file
86
daemons/management/commands/bot.py
Normal file
@@ -0,0 +1,86 @@
|
||||
import os
|
||||
|
||||
import telebot
|
||||
from django.contrib.auth.models import User
|
||||
from django.core.management.base import BaseCommand
|
||||
from telebot.types import Message
|
||||
|
||||
from Main.models import UserInfo
|
||||
|
||||
|
||||
# инстанс бота
|
||||
bot = telebot.TeleBot(os.getenv("TELEGRAM_TOKEN"))
|
||||
|
||||
|
||||
@bot.message_handler(commands=["start"])
|
||||
def do_action(message: Message):
|
||||
bot.send_message(
|
||||
message.chat.id,
|
||||
"Привет! Я тут чтобы помогать!\n/register - зарегистрироваться в сервисе\nБольше команд нет:(",
|
||||
)
|
||||
|
||||
|
||||
@bot.message_handler(commands=["register"])
|
||||
def register(message: Message):
|
||||
username = message.from_user.username
|
||||
if username == "" or message.from_user.username is None:
|
||||
bot.send_message(
|
||||
message.chat.id, "Добавть имя пользователя к своему телеграм аккаунту"
|
||||
)
|
||||
return
|
||||
ui = UserInfo.objects.filter(telegram_chat_id=message.chat.id).first()
|
||||
if ui:
|
||||
bot.send_message(message.chat.id, "Ты уже зарегистрировался")
|
||||
return
|
||||
user = User.objects.create(username=username)
|
||||
ui = UserInfo.objects.create(user=user, telegram_chat_id=message.chat.id)
|
||||
name = message.from_user.first_name
|
||||
surname = message.from_user.last_name
|
||||
if surname is None or surname == "" or name is None or name == "":
|
||||
bot.send_message(
|
||||
message.chat.id,
|
||||
"Приветствую в Sprint! Сейчас я помогу тебе создать аккаунт.\nДля начала отправь мне свою фамилию",
|
||||
)
|
||||
else:
|
||||
ui.surname = surname
|
||||
ui.name = name
|
||||
ui.verified = True
|
||||
ui.save()
|
||||
bot.send_message(
|
||||
message.chat.id,
|
||||
f"Регистрация завершена! Теперь можешь ты можешь войти в сервис под именем пользователя: {user.username}",
|
||||
)
|
||||
|
||||
|
||||
@bot.message_handler(content_types=["text"])
|
||||
def do_action(message: Message):
|
||||
user = User.objects.filter(userinfo__telegram_chat_id=message.chat.id).first()
|
||||
if not user:
|
||||
bot.send_message(
|
||||
message.chat.id,
|
||||
"Зарегистрируйся в сервисе, чтобы взаимодействовать со мной",
|
||||
)
|
||||
return
|
||||
if user.userinfo.surname is None:
|
||||
user.userinfo.surname = message.text
|
||||
user.userinfo.save()
|
||||
bot.send_message(message.chat.id, "Отлично! Теперь отправь мне свое имя")
|
||||
elif user.userinfo.name is None:
|
||||
user.userinfo.name = message.text
|
||||
user.userinfo.verified = True
|
||||
user.userinfo.save()
|
||||
bot.send_message(
|
||||
message.chat.id,
|
||||
f"Регистрация завершена! Теперь можешь ты можешь войти в сервис под именем пользователя: {user.username}",
|
||||
)
|
||||
else:
|
||||
bot.send_message(message.chat.id, "Я пока больше ничего не умею")
|
||||
|
||||
|
||||
class Command(BaseCommand):
|
||||
help = "starts bot"
|
||||
|
||||
def handle(self, *args, **options):
|
||||
print("bot is starting")
|
||||
bot.polling()
|
||||
print("bot failed")
|
21
daemons/management/commands/loop.py
Normal file
21
daemons/management/commands/loop.py
Normal file
@@ -0,0 +1,21 @@
|
||||
import datetime
|
||||
from time import sleep
|
||||
|
||||
from django.core.management.base import BaseCommand
|
||||
from django.utils import timezone
|
||||
|
||||
from Checker.models import Checker
|
||||
|
||||
|
||||
class Command(BaseCommand):
|
||||
help = "starts loop"
|
||||
|
||||
def check_checkers(self):
|
||||
for checker in Checker.objects.filter(testing_solution__isnull=False, last_request__lt=timezone.now() - datetime.timedelta(seconds=3)):
|
||||
checker.testing_solution.result = 'In queue'
|
||||
checker.testing_solution.save()
|
||||
|
||||
def handle(self, *args, **options):
|
||||
while True:
|
||||
self.check_checkers()
|
||||
sleep(5)
|
44
daemons/management/commands/receive.py
Normal file
44
daemons/management/commands/receive.py
Normal file
@@ -0,0 +1,44 @@
|
||||
from os.path import join, exists
|
||||
from shutil import rmtree
|
||||
|
||||
import pika
|
||||
from django.core.management.base import BaseCommand
|
||||
from pika.adapters.utils.connection_workflow import AMQPConnectorException
|
||||
|
||||
from Main.models import Solution
|
||||
from Sprint import settings
|
||||
from SprintLib.testers import *
|
||||
|
||||
|
||||
class Command(BaseCommand):
|
||||
help = "Tests solution"
|
||||
|
||||
def handle(self, *args, **options):
|
||||
print("Enter worker")
|
||||
while True:
|
||||
try:
|
||||
connection = pika.BlockingConnection(
|
||||
pika.ConnectionParameters(host=settings.RABBIT_HOST)
|
||||
)
|
||||
channel = connection.channel()
|
||||
channel.queue_declare(queue="test")
|
||||
|
||||
def callback(ch, method, properties, body):
|
||||
id = int(str(body, encoding="utf-8"))
|
||||
print(f"Received id {id}")
|
||||
solution = Solution.objects.get(id=id)
|
||||
try:
|
||||
eval(solution.language.work_name + "Tester")(solution).execute()
|
||||
except Exception as e:
|
||||
print(e)
|
||||
solution.result = "TE"
|
||||
solution.save()
|
||||
finally:
|
||||
path = join("solutions", str(id))
|
||||
if exists(path):
|
||||
rmtree(path)
|
||||
|
||||
channel.basic_consume(queue="test", on_message_callback=callback, auto_ack=True)
|
||||
channel.start_consuming()
|
||||
except AMQPConnectorException:
|
||||
print("connection to rabbit failed: reconnecting")
|
9
daemons/management/commands/storage.py
Normal file
9
daemons/management/commands/storage.py
Normal file
@@ -0,0 +1,9 @@
|
||||
from django.core.management.base import BaseCommand
|
||||
from FileStorage.root import runserver
|
||||
|
||||
|
||||
class Command(BaseCommand):
|
||||
help = "starts FileStorage"
|
||||
|
||||
def handle(self, *args, **options):
|
||||
runserver()
|
10
daemons/management/commands/success_deploy.py
Normal file
10
daemons/management/commands/success_deploy.py
Normal file
@@ -0,0 +1,10 @@
|
||||
from django.contrib.auth.models import User
|
||||
from django.core.management import BaseCommand
|
||||
|
||||
from daemons.management import bot
|
||||
|
||||
|
||||
class Command(BaseCommand):
|
||||
def handle(self, *args, **options):
|
||||
for user in User.objects.filter(is_superuser=True):
|
||||
bot.send_message(user.userinfo.telegram_chat_id, "Деплой прошел успешно")
|
0
daemons/migrations/__init__.py
Normal file
0
daemons/migrations/__init__.py
Normal file
Reference in New Issue
Block a user