-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
78 lines (56 loc) · 2.12 KB
/
server.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
from fastapi import FastAPI
from pydantic import BaseModel
import pika
import msgpack
# RabbitMQ connection parameters
RABBITMQ_HOST = 'localhost'
RUN_EXCHANGE = 'run_exchange'
SUBMIT_EXCHANGE = 'submit_exchange'
RUN_ROUTING_KEY = 'run_queue_key'
SUBMIT_ROUTING_KEY = 'submit_queue_key'
# RabbitMQ connection parameters
connection_params = pika.ConnectionParameters(RABBITMQ_HOST)
connection = pika.BlockingConnection(connection_params)
channel = connection.channel()
# Declare exchanges
channel.exchange_declare(exchange=RUN_EXCHANGE, exchange_type='direct')
channel.exchange_declare(exchange=SUBMIT_EXCHANGE, exchange_type='direct')
# Declare queues
channel.queue_declare(queue='run_queue')
channel.queue_declare(queue='submit_queue')
# Bind queues to exchanges with specific routing keys
channel.queue_bind(exchange=RUN_EXCHANGE, queue='run_queue', routing_key=RUN_ROUTING_KEY)
channel.queue_bind(exchange=SUBMIT_EXCHANGE, queue='submit_queue', routing_key=SUBMIT_ROUTING_KEY)
# Publish messages
def publish_message(exchange, routing_key, body):
message = msgpack.packb(body)
channel.basic_publish(exchange=exchange, routing_key=routing_key, body=message)
print(f" [x] Sent message to {exchange} with routing key '{routing_key}'")
# Submit request pydantic model
class SubmitRequest(BaseModel):
language: str
time_limit: int
memory_limit: int
src_code: str
std_in: str = " "
expected_out: str
callback_url: str
# Run request pydantic model
class RunRequest(BaseModel):
language: str
time_limit: int
memory_limit: int
src_code: str
std_in: str = " "
callback_url: str
app = FastAPI()
# Submit route
@app.post('/submit')
async def submit(submit_request: SubmitRequest):
publish_message(SUBMIT_EXCHANGE, SUBMIT_ROUTING_KEY, body=submit_request.dict())
return {"msg": "submit task enqueued, result will be available at the callback server"}
# Run route
@app.post('/run')
async def run(run_request: RunRequest):
publish_message(RUN_EXCHANGE, RUN_ROUTING_KEY, body=run_request.dict())
return {"msg": "run task enqueued, result will be available at the callback server"}