From d1d1a39a30be646e9350255c9b3aba0d3f1c7720 Mon Sep 17 00:00:00 2001 From: Thomas Williams Date: Mon, 8 Jul 2024 14:23:16 +0100 Subject: [PATCH 1/2] Resolves issue #5 for now --- main.py | 10 +++++++++- rabbitmq.py | 13 ++++++++++--- requirements.txt | 1 + 3 files changed, 20 insertions(+), 4 deletions(-) diff --git a/main.py b/main.py index 0e3af54..9d97566 100755 --- a/main.py +++ b/main.py @@ -45,7 +45,14 @@ def loadUrl(url): headers = { 'User-Agent': 'Monutil monitor' } - response = requests.get(url, timeout=config.urlTimeout, headers=headers) + @retry(stop_max_attempt_number=3) + def submitRequest(url): + + response = requests.get(url, timeout=config.urlTimeout, headers=headers) + return response + + response = submitRequest(url) + return response def prepareUrl(src, baseUrl): @@ -233,6 +240,7 @@ if __name__ == "__main__": import threading import signal import socket + from retrying import retry from functools import partial from concurrent.futures import ThreadPoolExecutor, as_completed from bs4 import BeautifulSoup diff --git a/rabbitmq.py b/rabbitmq.py index 792c36c..c01990c 100644 --- a/rabbitmq.py +++ b/rabbitmq.py @@ -28,6 +28,7 @@ import base64 import config import log import datetime +from retrying import retry class rabbitMQClient: @@ -42,6 +43,13 @@ class rabbitMQClient: def publish(self, message): + @retry(stop_max_attempt_number=3) + def publishMessage(conn): + + ch = conn.channel() + + ch.basic_publish(exchange='', routing_key=self.routingKey, body=message, properties=pika.BasicProperties(delivery_mode=pika.spec.PERSISTENT_DELIVERY_MODE)) + message = base64.b64encode(message.encode('utf-8')) context = ssl.create_default_context( @@ -55,10 +63,9 @@ class rabbitMQClient: with pika.BlockingConnection(connParams) as conn: - ch = conn.channel() - - ch.basic_publish(exchange='', routing_key=self.routingKey, body=message, properties=pika.BasicProperties(delivery_mode=pika.spec.PERSISTENT_DELIVERY_MODE)) + publishMessage(conn) + @retry(stop_max_attempt_number=3) def retrieve(self): def callback(ch, method, properties, body): diff --git a/requirements.txt b/requirements.txt index a760dfd..387f555 100644 --- a/requirements.txt +++ b/requirements.txt @@ -2,3 +2,4 @@ psutil requests bs4 datetime +retrying From f2249c4f6cfe1d07d991d369102f00ef002347df Mon Sep 17 00:00:00 2001 From: Thomas Williams Date: Mon, 8 Jul 2024 14:34:13 +0100 Subject: [PATCH 2/2] Resolves issue #31 --- main.py | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/main.py b/main.py index 9d97566..75a8208 100755 --- a/main.py +++ b/main.py @@ -35,7 +35,29 @@ def get_pip_command(): def install_dependencies(): pip_command = get_pip_command() - subprocess.check_call([pip_command, 'install', '-r', 'requirements.txt', '--user']) + + with open('requirements.txt', 'r') as req_file: + + requirements = req_file.read().splitlines() + + try: + + with open('installedrequirements.txt', 'r') as installed_file: + installed_requirements = installed_file.read().splitlines() + + except FileNotFoundError: + + installed_requirements = [] + + dependencies_to_install = [req for req in requirements if req not in installed_requirements] + + if dependencies_to_install: + + subprocess.check_call([pip_command, 'install'] + dependencies_to_install + ['--user']) + + with open('installedrequirements.txt', 'a') as installed_file: + for dependency in dependencies_to_install: + installed_file.write(dependency + '\n') def signal_handler(sig, frame): print('SIGINT/SIGTERM aknowledged. Stopping script gracefully, please wait...')