messaging

This commit is contained in:
Egor Matveev
2022-03-15 01:20:59 +03:00
parent cb8efd8f0b
commit d8cfa99eb1
3 changed files with 45 additions and 75 deletions

View File

@@ -0,0 +1,9 @@
from SprintLib.queue import MessagingSupport
class Command(MessagingSupport):
help = "starts file generator"
queue_name = "files"
def consume(self, ch, method, properties, body):
...

View File

@@ -1,44 +1,26 @@
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.queue import MessagingSupport
from SprintLib.testers import *
class Command(BaseCommand):
class Command(MessagingSupport):
help = "Tests solution"
queue_name = "test"
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")
def consume(self, 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)