From 5292c409ca1038bc6c207927064959c20f1cdca2 Mon Sep 17 00:00:00 2001 From: Malachy Byrne Date: Thu, 13 Apr 2023 00:50:14 +0100 Subject: [PATCH] all queues now use same logger --- funding_agency.py | 15 ++++++++++----- log.py | 2 -- project_server.py | 2 +- researcher.py | 7 ++++--- university.py | 15 +++++++++------ 5 files changed, 24 insertions(+), 17 deletions(-) diff --git a/funding_agency.py b/funding_agency.py index fbcc396..f1a4a32 100644 --- a/funding_agency.py +++ b/funding_agency.py @@ -1,13 +1,13 @@ import pika +from functions import log + connection = pika.BlockingConnection(pika.ConnectionParameters('localhost', 5672, '/', pika.PlainCredentials("user", "password"))) channel = connection.channel() channel.queue_declare(queue='research_proposals') -print("Waiting for proposals...") - def process_proposal(ch, method, properties, body): proposal = body.decode() acronym, title, description, amount = proposal.split(',') @@ -19,14 +19,19 @@ def process_proposal(ch, method, properties, body): body="approved") project_info=f"{acronym},{title},{description},{amount}" channel.basic_publish(exchange='', routing_key='approved_projects', body=project_info) - print(f"{acronym} approved") + log(f"{acronym} approved") else: channel.basic_publish(exchange='', routing_key=properties.reply_to, properties=pika.BasicProperties(correlation_id=properties.correlation_id), body="rejected") - print(f"{acronym} rejected") + log(f"{acronym} rejected") channel.basic_consume(queue='research_proposals', on_message_callback=process_proposal, auto_ack=True) -channel.start_consuming() \ No newline at end of file +try: + channel.start_consuming() +except KeyboardInterrupt: + channel.stop_consuming() + connection.close() + diff --git a/log.py b/log.py index 338f516..b41e5da 100644 --- a/log.py +++ b/log.py @@ -15,10 +15,8 @@ def log(ch, method, properties, body): channel.basic_consume(queue='log', on_message_callback=log, auto_ack=True) try: - print("Opening logs") channel.start_consuming() except KeyboardInterrupt: channel.stop_consuming() channel.close() - print("Closing logs") diff --git a/project_server.py b/project_server.py index 35497ca..54dd80e 100644 --- a/project_server.py +++ b/project_server.py @@ -23,7 +23,7 @@ def get_project(ch, method, properties, body): routing_key=properties.reply_to, properties=pika.BasicProperties(correlation_id=properties.correlation_id), body=project) - print(f"got project {project_id}") + log(f"got project {project_id}") def save_project(ch, method, properties, body): diff --git a/researcher.py b/researcher.py index a251f29..25be422 100644 --- a/researcher.py +++ b/researcher.py @@ -1,7 +1,8 @@ import pika + import uuid - +from functions import log class ResearchProposal(object): @@ -45,6 +46,6 @@ proposals = [ for proposal in proposals: proposal_client = ResearchProposal() - print(f"{proposal['acronym']} sends proposal") + log(f"{proposal['acronym']} sends proposal") response = proposal_client.call(proposal) - print(f"{proposal['acronym']} got {response}") \ No newline at end of file + log(f"{proposal['acronym']} got {response}") diff --git a/university.py b/university.py index 0b11c25..a4739b5 100644 --- a/university.py +++ b/university.py @@ -1,6 +1,9 @@ -import os import pika +import os + +from functions import log + if not os.path.exists("approved_projects"): os.makedirs("approved_projects") @@ -12,20 +15,20 @@ 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}') + + log(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...') + log('University waiting for approved projects...') channel.start_consuming() except KeyboardInterrupt: channel.stop_consuming()