forked from LiveCarta/BookConverter
Small changes (to work with preset)
This commit is contained in:
13
consumer.py
13
consumer.py
@@ -77,7 +77,6 @@ def callback(ch, method, properties, body, logger, libre_locker):
|
|||||||
thread.start()
|
thread.start()
|
||||||
logging.log(logging.INFO, f"Active threads: {active_count()}.")
|
logging.log(logging.INFO, f"Active threads: {active_count()}.")
|
||||||
# print(f"Active threads: {active_count()}.")
|
# print(f"Active threads: {active_count()}.")
|
||||||
|
|
||||||
except Exception as exc:
|
except Exception as exc:
|
||||||
if hasattr(exc, "message"):
|
if hasattr(exc, "message"):
|
||||||
logger.error(f"{sys.exc_info()[0]}: {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():
|
def server_run():
|
||||||
logger = configure_file_logger("consumer")
|
logger = configure_file_logger("consumer")
|
||||||
|
channel = None
|
||||||
try:
|
try:
|
||||||
folder_path = os.path.dirname(os.path.abspath(__file__))
|
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:
|
with open(config_path, "r") as f:
|
||||||
conf_param = json.load(f)
|
conf_param = json.load(f)
|
||||||
|
|
||||||
host = conf_param.get("host") or pika.ConnectionParameters().DEFAULT_HOST
|
host = conf_param.get(
|
||||||
port = conf_param.get("port") or pika.ConnectionParameters().DEFAULT_PORT
|
"host") or pika.ConnectionParameters().DEFAULT_HOST
|
||||||
channel = None
|
port = conf_param.get(
|
||||||
|
"port") or pika.ConnectionParameters().DEFAULT_PORT
|
||||||
credentials = pika.PlainCredentials(
|
credentials = pika.PlainCredentials(
|
||||||
username=conf_param["username"], password=conf_param["password"])
|
username=conf_param["username"], password=conf_param["password"])
|
||||||
parameters = pika.ConnectionParameters(
|
parameters = pika.ConnectionParameters(
|
||||||
@@ -113,7 +115,6 @@ def server_run():
|
|||||||
logger.log(logging.ERROR,
|
logger.log(logging.ERROR,
|
||||||
f"Queue {conf_param['queue']} is not declared.")
|
f"Queue {conf_param['queue']} is not declared.")
|
||||||
raise exc
|
raise exc
|
||||||
|
|
||||||
locker = Event()
|
locker = Event()
|
||||||
locker.set()
|
locker.set()
|
||||||
channel.basic_consume(queue=conf_param["queue"],
|
channel.basic_consume(queue=conf_param["queue"],
|
||||||
|
|||||||
@@ -35,22 +35,6 @@ class Access:
|
|||||||
self.get_token()
|
self.get_token()
|
||||||
self.refreshing.set()
|
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):
|
def set_credentials(self, url):
|
||||||
folder_path = os.path.dirname(
|
folder_path = os.path.dirname(
|
||||||
os.path.dirname(os.path.abspath(__file__)))
|
os.path.dirname(os.path.abspath(__file__)))
|
||||||
@@ -60,8 +44,8 @@ class Access:
|
|||||||
|
|
||||||
self.refreshing.clear()
|
self.refreshing.clear()
|
||||||
self.url = url
|
self.url = url
|
||||||
self.username = params['username']
|
self.username = params["username"]
|
||||||
self.password = params['password']
|
self.password = params["password"]
|
||||||
self.refreshing.set()
|
self.refreshing.set()
|
||||||
|
|
||||||
def format_header(self):
|
def format_header(self):
|
||||||
@@ -123,14 +107,14 @@ class Access:
|
|||||||
else:
|
else:
|
||||||
raise Exception(f'{response.status_code}')
|
raise Exception(f'{response.status_code}')
|
||||||
|
|
||||||
def get_book(self, book_id):
|
def get_file(self, file_path):
|
||||||
"""Function downloads the book from site"""
|
"""Function downloads the file[book, preset] from site"""
|
||||||
if self.is_time_for_refreshing():
|
if self.is_time_for_refreshing():
|
||||||
self.refresh_token()
|
self.refresh_token()
|
||||||
|
|
||||||
self.refreshing.wait()
|
self.refreshing.wait()
|
||||||
response = requests.get(
|
response = requests.get(
|
||||||
f'{self.url}/doc-convert/{book_id}/file', headers=self.headers,
|
file_path, headers=self.headers,
|
||||||
# auth=('kiryl.miatselitsa', 'iK4yXCvdyHFEEOvG2v3F')
|
# auth=('kiryl.miatselitsa', 'iK4yXCvdyHFEEOvG2v3F')
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -139,11 +123,26 @@ class Access:
|
|||||||
elif response.status_code == 200:
|
elif response.status_code == 200:
|
||||||
content = response.content
|
content = response.content
|
||||||
else:
|
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}')
|
f'status code:{response.status_code}')
|
||||||
|
|
||||||
return content
|
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)
|
@sleep(3)
|
||||||
def send_image(self, img_path, doc_id, img_content: bytes = None):
|
def send_image(self, img_path, doc_id, img_content: bytes = None):
|
||||||
"""Function sends images to site"""
|
"""Function sends images to site"""
|
||||||
|
|||||||
Reference in New Issue
Block a user