messaging
This commit is contained in:
parent
cb8efd8f0b
commit
d8cfa99eb1
@ -1,8 +1,6 @@
|
|||||||
import json
|
|
||||||
from enum import Enum, auto
|
|
||||||
from typing import Union
|
|
||||||
|
|
||||||
import pika
|
import pika
|
||||||
|
from django.core.management import BaseCommand
|
||||||
|
from pika.adapters.utils.connection_workflow import AMQPConnectorException
|
||||||
|
|
||||||
from Sprint import settings
|
from Sprint import settings
|
||||||
|
|
||||||
@ -22,43 +20,24 @@ def send_testing(solution):
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
class Queue(str, Enum):
|
class MessagingSupport(BaseCommand):
|
||||||
test = auto()
|
queue_name = None
|
||||||
notification = auto()
|
|
||||||
|
|
||||||
|
def consume(self, ch, method, properties, body):
|
||||||
|
raise NotImplementedError
|
||||||
|
|
||||||
class QueueAccessor:
|
def handle(self, *args, **options):
|
||||||
|
if self.queue_name is None:
|
||||||
def publish(self, queue: Union[Queue, str], message: Union[bytes, dict]):
|
raise NotImplementedError("Queue name must be declared")
|
||||||
if isinstance(message, dict):
|
print("start listening " + self.queue_name)
|
||||||
message = json.dumps(message).encode("UTF-8")
|
while True:
|
||||||
if isinstance(queue, str):
|
try:
|
||||||
queue = Queue(queue)
|
connection = pika.BlockingConnection(
|
||||||
with pika.BlockingConnection(
|
pika.ConnectionParameters(host=settings.RABBIT_HOST)
|
||||||
pika.ConnectionParameters(host=settings.RABBIT_HOST, port=settings.RABBIT_PORT)
|
)
|
||||||
) as connection:
|
|
||||||
channel = connection.channel()
|
|
||||||
channel.queue_declare(queue=queue.name)
|
|
||||||
channel.basic_publish(
|
|
||||||
exchange="",
|
|
||||||
routing_key=queue.name,
|
|
||||||
body=message,
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
def message_handler(queue: Union[Queue, str]):
|
|
||||||
if isinstance(queue, str):
|
|
||||||
queue = Queue(queue)
|
|
||||||
|
|
||||||
def decorator(func):
|
|
||||||
def new_func(*args, **kwargs):
|
|
||||||
print("Enter listener for queue", queue)
|
|
||||||
with pika.BlockingConnection(
|
|
||||||
pika.ConnectionParameters(host=settings.RABBIT_HOST)
|
|
||||||
) as connection:
|
|
||||||
channel = connection.channel()
|
channel = connection.channel()
|
||||||
channel.queue_declare(queue=queue.name)
|
channel.queue_declare(queue=self.queue_name)
|
||||||
channel.basic_consume(queue=queue.name, on_message_callback=func, auto_ack=True)
|
channel.basic_consume(queue=self.queue_name, on_message_callback=self.consume, auto_ack=True)
|
||||||
channel.start_consuming()
|
channel.start_consuming()
|
||||||
return new_func
|
except AMQPConnectorException:
|
||||||
return decorator
|
print("connection to rabbit failed: reconnecting")
|
||||||
|
9
daemons/management/commands/file_generator.py
Normal file
9
daemons/management/commands/file_generator.py
Normal 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):
|
||||||
|
...
|
@ -1,44 +1,26 @@
|
|||||||
from os.path import join, exists
|
from os.path import join, exists
|
||||||
from shutil import rmtree
|
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 Main.models import Solution
|
||||||
from Sprint import settings
|
from SprintLib.queue import MessagingSupport
|
||||||
from SprintLib.testers import *
|
from SprintLib.testers import *
|
||||||
|
|
||||||
|
|
||||||
class Command(BaseCommand):
|
class Command(MessagingSupport):
|
||||||
help = "Tests solution"
|
help = "Tests solution"
|
||||||
|
queue_name = "test"
|
||||||
|
|
||||||
def handle(self, *args, **options):
|
def consume(self, ch, method, properties, body):
|
||||||
print("Enter worker")
|
id = int(str(body, encoding="utf-8"))
|
||||||
while True:
|
print(f"Received id {id}")
|
||||||
try:
|
solution = Solution.objects.get(id=id)
|
||||||
connection = pika.BlockingConnection(
|
try:
|
||||||
pika.ConnectionParameters(host=settings.RABBIT_HOST)
|
eval(solution.language.work_name + "Tester")(solution).execute()
|
||||||
)
|
except Exception as e:
|
||||||
channel = connection.channel()
|
print(e)
|
||||||
channel.queue_declare(queue="test")
|
solution.result = "TE"
|
||||||
|
solution.save()
|
||||||
def callback(ch, method, properties, body):
|
finally:
|
||||||
id = int(str(body, encoding="utf-8"))
|
path = join("solutions", str(id))
|
||||||
print(f"Received id {id}")
|
if exists(path):
|
||||||
solution = Solution.objects.get(id=id)
|
rmtree(path)
|
||||||
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")
|
|
||||||
|
Loading…
Reference in New Issue
Block a user