From 2ee9f1ea3b8f3c39719cc049db8569a4738e379f Mon Sep 17 00:00:00 2001 From: Egor Matveev Date: Sun, 13 Mar 2022 16:12:41 +0300 Subject: [PATCH] testers --- Main/management/commands/receive.py | 47 ++++++++++++++++------------- 1 file changed, 26 insertions(+), 21 deletions(-) diff --git a/Main/management/commands/receive.py b/Main/management/commands/receive.py index a602181..b31d506 100644 --- a/Main/management/commands/receive.py +++ b/Main/management/commands/receive.py @@ -3,6 +3,7 @@ from shutil import rmtree import pika from django.core.management.base import BaseCommand +from pika.adapters.utils.connection_workflow import AMQPConnectionWorkflowFailed from Main.models import Solution from Sprint import settings @@ -14,26 +15,30 @@ class Command(BaseCommand): 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): - id = int(str(body, encoding="utf-8")) - print(f"Received id {id}") - solution = Solution.objects.get(id=id) + while True: 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) + connection = pika.BlockingConnection( + pika.ConnectionParameters(host=settings.RABBIT_HOST) + ) + channel = connection.channel() + channel.queue_declare(queue="test") - channel.basic_consume(queue="test", on_message_callback=callback, auto_ack=True) - channel.start_consuming() + 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 AMQPConnectionWorkflowFailed: + print("connection to rabbit failed: reconnecting")