diff --git a/consumer.py b/consumer.py index 2ea307c..095facf 100644 --- a/consumer.py +++ b/consumer.py @@ -77,7 +77,6 @@ def callback(ch, method, properties, body, logger, libre_locker): thread.start() logging.log(logging.INFO, f"Active threads: {active_count()}.") # print(f"Active threads: {active_count()}.") - except Exception as exc: if hasattr(exc, "message"): logger.error(f"{sys.exc_info()[0]}: {exc.message}") @@ -90,15 +89,18 @@ def callback(ch, method, properties, body, logger, libre_locker): def server_run(): logger = configure_file_logger("consumer") + channel = None try: folder_path = os.path.dirname(os.path.abspath(__file__)) - config_path = Path(os.path.join(folder_path, "config/queue_config.json")) + config_path = Path(os.path.join( + folder_path, "config/queue_config.json")) with open(config_path, "r") as f: conf_param = json.load(f) - host = conf_param.get("host") or pika.ConnectionParameters().DEFAULT_HOST - port = conf_param.get("port") or pika.ConnectionParameters().DEFAULT_PORT - channel = None + host = conf_param.get( + "host") or pika.ConnectionParameters().DEFAULT_HOST + port = conf_param.get( + "port") or pika.ConnectionParameters().DEFAULT_PORT credentials = pika.PlainCredentials( username=conf_param["username"], password=conf_param["password"]) parameters = pika.ConnectionParameters( @@ -113,7 +115,6 @@ def server_run(): logger.log(logging.ERROR, f"Queue {conf_param['queue']} is not declared.") raise exc - locker = Event() locker.set() channel.basic_consume(queue=conf_param["queue"], diff --git a/src/access.py b/src/access.py index 4367c33..ba8ddb6 100644 --- a/src/access.py +++ b/src/access.py @@ -35,22 +35,6 @@ class Access: self.get_token() self.refreshing.set() - def sleep(timeout: float, retry=3): - def decorator(function): - """Decorator sleeping timeout sec and makes 3 retries""" - def wrapper(*args, **kwargs): - retries = 0 - while retries < retry: - try: - value = function(*args, **kwargs) - if value is not None: - return value - except: - time.sleep(timeout) - retries += 1 - return wrapper - return decorator - def set_credentials(self, url): folder_path = os.path.dirname( os.path.dirname(os.path.abspath(__file__))) @@ -60,8 +44,8 @@ class Access: self.refreshing.clear() self.url = url - self.username = params['username'] - self.password = params['password'] + self.username = params["username"] + self.password = params["password"] self.refreshing.set() def format_header(self): @@ -123,14 +107,14 @@ class Access: else: raise Exception(f'{response.status_code}') - def get_book(self, book_id): - """Function downloads the book from site""" + def get_file(self, file_path): + """Function downloads the file[book, preset] from site""" if self.is_time_for_refreshing(): self.refresh_token() self.refreshing.wait() response = requests.get( - f'{self.url}/doc-convert/{book_id}/file', headers=self.headers, + file_path, headers=self.headers, # auth=('kiryl.miatselitsa', 'iK4yXCvdyHFEEOvG2v3F') ) @@ -139,11 +123,26 @@ class Access: elif response.status_code == 200: content = response.content else: - raise Exception(f'Error in getting doc from url: {self.url}/doc-convert/{book_id}/file, ' + raise Exception(f'Error in getting preset from url: {file_path}, ' f'status code:{response.status_code}') - return content + def sleep(timeout: float, retry=3): + def decorator(function): + """Decorator sleeping timeout sec and makes 3 retries""" + def wrapper(*args, **kwargs): + retries = 0 + while retries < retry: + try: + value = function(*args, **kwargs) + if value is not None: + return value + except: + time.sleep(timeout) + retries += 1 + return wrapper + return decorator + @sleep(3) def send_image(self, img_path, doc_id, img_content: bytes = None): """Function sends images to site"""