sprint/Main/management/commands/receive.py
Egor Matveev d2d427fd6e codestyle
2021-12-02 18:24:51 +03:00

40 lines
1.2 KiB
Python

from time import sleep
import pika
from django.core.management.base import BaseCommand
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")
connection = pika.BlockingConnection(
pika.ConnectionParameters(host=settings.RABBIT_HOST)
)
channel = connection.channel()
channel.queue_declare(queue="test")
def callback(ch, method, properties, body):
try:
id = int(str(body, encoding="utf-8"))
print(f"Received id {id}")
while True:
try:
solution = Solution.objects.get(id=id)
break
except:
sleep(0.5)
eval(solution.language.work_name + "Tester")(solution).execute()
except Exception as e:
print(e)
solution.result = "TE"
solution.save()
channel.basic_consume(queue="test", on_message_callback=callback, auto_ack=True)
channel.start_consuming()