unversity stores approved projects in files
This commit is contained in:
parent
03849be985
commit
54e3154876
@ -14,12 +14,16 @@ def process_proposal(ch, method, properties, body):
|
||||
|
||||
if 200000 <= int(amount) <= 500000:
|
||||
channel.basic_publish(exchange='',
|
||||
routing_key=str(properties.reply_to),
|
||||
routing_key=properties.reply_to,
|
||||
properties=pika.BasicProperties(correlation_id=properties.correlation_id),
|
||||
body=f"{acronym} approved")
|
||||
project_info=f"{acronym},{title},{description},{amount}"
|
||||
channel.basic_publish(exchange='', routing_key='approved_projects', body=project_info)
|
||||
print(f"{acronym} approved")
|
||||
else:
|
||||
channel.basic_publish(exchange='',
|
||||
routing_key=str(properties.reply_to),
|
||||
routing_key=properties.reply_to,
|
||||
properties=pika.BasicProperties(correlation_id=properties.correlation_id),
|
||||
body=f"{acronym} rejected")
|
||||
print(f"{acronym} rejected")
|
||||
|
||||
|
||||
@ -1,10 +1,18 @@
|
||||
import pika
|
||||
import uuid
|
||||
|
||||
connection = pika.BlockingConnection(pika.ConnectionParameters('localhost', 5672, '/', pika.PlainCredentials("user", "password")))
|
||||
channel = connection.channel()
|
||||
|
||||
channel.queue_declare(queue='research_proposals')
|
||||
|
||||
|
||||
def process_response(ch, method, properties, body):
|
||||
if corr_id == properties.correlation_id:
|
||||
print(body)
|
||||
else:
|
||||
print("response failed")
|
||||
|
||||
proposals = [
|
||||
{"acronym": "PRT1", "title": "Phantom Blood", "description": "Johnathen", "amount": "300000"},
|
||||
{"acronym": "PRT2", "title": "Battle Tendency", "description": "Joseph", "amount": "400000"},
|
||||
@ -14,17 +22,20 @@ proposals = [
|
||||
{"acronym": "PRT6", "title": "Stone Ocean", "description": "Jolyne", "amount": "420000"},
|
||||
{"acronym": "PRT7", "title": "Steel Ball Run", "description": "Johnny", "amount": "100000"},
|
||||
{"acronym": "PRT8", "title": "Jojolion", "description": "Josuke", "amount": "900000"},
|
||||
{"acronym": "PRT9", "title": "Jojolands", "description": "Jodio", "amount": "420000"}
|
||||
{"acronym": "PRT9", "title": "Jojolands", "description": "Jodio", "amount": "450000"}
|
||||
]
|
||||
|
||||
corr_id = str(uuid.uuid4())
|
||||
|
||||
for proposal in proposals:
|
||||
result = channel.queue_declare(queue='', exclusive=True)
|
||||
reply_to = result.method.queue
|
||||
|
||||
response_queue = channel.queue_declare(queue='', exclusive=True).method.queue
|
||||
|
||||
channel.basic_publish(exchange='',
|
||||
routing_key='research_proposals',
|
||||
properties=pika.BasicProperties(reply_to=reply_to),
|
||||
properties=pika.BasicProperties(reply_to=response_queue, correlation_id=corr_id),
|
||||
body=f"{proposal['acronym']},{proposal['title']},{proposal['description']},{proposal['amount']}")
|
||||
print(f"Proposal {proposal['acronym']} sent")
|
||||
channel.basic_consume(queue=response_queue, on_message_callback=process_response)
|
||||
|
||||
connection.close()
|
||||
29
university.py
Normal file
29
university.py
Normal file
@ -0,0 +1,29 @@
|
||||
import os
|
||||
import pika
|
||||
|
||||
connection = pika.BlockingConnection(pika.ConnectionParameters('localhost', 5672, '/', pika.PlainCredentials("user", "password")))
|
||||
channel = connection.channel()
|
||||
|
||||
channel.queue_declare(queue='approved_projects')
|
||||
|
||||
def process_approved_project(ch, method, properties, body):
|
||||
proposal = body.decode()
|
||||
acronym, title, description, amount = proposal.split(',')
|
||||
|
||||
|
||||
filename = os.path.join(os.getcwd(), 'approved_projects', f'{acronym}.txt')
|
||||
with open(filename, 'w') as f:
|
||||
f.write(f'Acronym: {acronym}\n')
|
||||
f.write(f'Researcher: {description}\n')
|
||||
f.write(f'Budget: {amount}\n')
|
||||
|
||||
print(f'Approved project received: {body}')
|
||||
|
||||
channel.basic_consume(queue='approved_projects', on_message_callback=process_approved_project, auto_ack=True)
|
||||
|
||||
try:
|
||||
print('University waiting for approved projects...')
|
||||
channel.start_consuming()
|
||||
except KeyboardInterrupt:
|
||||
channel.stop_consuming()
|
||||
connection.close()
|
||||
Loading…
x
Reference in New Issue
Block a user