Log management ok
This commit is contained in:
parent
dc4651b43a
commit
2c957a64a2
|
|
@ -4,6 +4,7 @@ import re
|
||||||
import sys
|
import sys
|
||||||
import threading
|
import threading
|
||||||
import time
|
import time
|
||||||
|
from datetime import datetime
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
import requests
|
import requests
|
||||||
|
|
@ -12,8 +13,8 @@ import requests
|
||||||
from google.api_core.exceptions import Forbidden
|
from google.api_core.exceptions import Forbidden
|
||||||
from google.cloud import storage
|
from google.cloud import storage
|
||||||
from requests import JSONDecodeError
|
from requests import JSONDecodeError
|
||||||
|
|
||||||
from lib.db import Archive, db
|
from lib.db import Archive, db
|
||||||
|
from lib.db.models import Log
|
||||||
from PyQt5.QtCore import QThread
|
from PyQt5.QtCore import QThread
|
||||||
from requests.adapters import HTTPAdapter, Retry
|
from requests.adapters import HTTPAdapter, Retry
|
||||||
from urllib3.exceptions import InsecureRequestWarning
|
from urllib3.exceptions import InsecureRequestWarning
|
||||||
|
|
@ -187,11 +188,11 @@ class ArchiveSynchronizer(Component):
|
||||||
|
|
||||||
def remote_fetch(self, remote_path=None, local_path=None):
|
def remote_fetch(self, remote_path=None, local_path=None):
|
||||||
"""
|
"""
|
||||||
download a single file from the server.
|
Download a single file from the server.
|
||||||
|
|
||||||
:param remote_path: path of where to download the file from
|
:param remote_path: Path of where to download the file from
|
||||||
:param local_path: path of where to save the file to
|
:param local_path: Path of where to save the file to
|
||||||
:return: a dictionary with errors if any occur.
|
:return: A dictionary with errors if any occur.
|
||||||
"""
|
"""
|
||||||
if remote_path is None:
|
if remote_path is None:
|
||||||
raise ValueError("remote_path cannot be None")
|
raise ValueError("remote_path cannot be None")
|
||||||
|
|
@ -199,48 +200,68 @@ class ArchiveSynchronizer(Component):
|
||||||
raise ValueError("local_path cannot be None")
|
raise ValueError("local_path cannot be None")
|
||||||
|
|
||||||
call_url = f"https://dev.r5portal.it/{remote_path}"
|
call_url = f"https://dev.r5portal.it/{remote_path}"
|
||||||
|
log_info_type = "Download"
|
||||||
|
log_time = datetime.now()
|
||||||
|
log_info = f"Attempted to download from {call_url}"
|
||||||
|
|
||||||
try:
|
try:
|
||||||
if not self.simulate:
|
if not self.simulate:
|
||||||
with requests.Session() as s:
|
with requests.Session() as s:
|
||||||
self.log.info(f"Fetching file from: {call_url}")
|
self.log.info(f"Fetching file from: {call_url}")
|
||||||
|
|
||||||
# Make the HTTP GET request to fetch the file
|
|
||||||
response = s.get(call_url, timeout=5, verify=False)
|
response = s.get(call_url, timeout=5, verify=False)
|
||||||
|
|
||||||
# Log response details
|
|
||||||
self.log.info(f"HTTP Status Code: {response.status_code}")
|
self.log.info(f"HTTP Status Code: {response.status_code}")
|
||||||
#self.log.info(f"Response Headers: {response.headers}")
|
|
||||||
#self.log.info(f"Response Content: {response.content}")
|
|
||||||
|
|
||||||
# Handle HTTP errors
|
|
||||||
if response.status_code == 404:
|
if response.status_code == 404:
|
||||||
self.log.warning(f"File not found: {call_url}. Please check the URL path.")
|
log_info += " - File not found"
|
||||||
|
self.log.warning(log_info)
|
||||||
|
self.log_to_db(log_time, log_info_type, log_info)
|
||||||
return {"error": "File not found"}
|
return {"error": "File not found"}
|
||||||
elif response.status_code == 403 or response.status_code == 401:
|
elif response.status_code in [403, 401]:
|
||||||
self.log.warning(f"Access forbidden or not logged in for file: {call_url}")
|
log_info += " - Access forbidden or not logged in"
|
||||||
|
self.log.warning(log_info)
|
||||||
|
self.log_to_db(log_time, log_info_type, log_info)
|
||||||
return {"error": "Access forbidden or not logged in"}
|
return {"error": "Access forbidden or not logged in"}
|
||||||
elif response.status_code != 200:
|
elif response.status_code != 200:
|
||||||
self.log.error(f"Unexpected HTTP response status: {response.status_code} for URL: {call_url}")
|
log_info += f" - Unexpected HTTP response status: {response.status_code}"
|
||||||
|
self.log.error(log_info)
|
||||||
|
self.log_to_db(log_time, log_info_type, log_info)
|
||||||
return {"error": "Unexpected HTTP response status"}
|
return {"error": "Unexpected HTTP response status"}
|
||||||
|
|
||||||
# Ensure the directory exists
|
|
||||||
os.makedirs(local_path, exist_ok=True)
|
os.makedirs(local_path, exist_ok=True)
|
||||||
|
|
||||||
# Save the file to the local path
|
|
||||||
local_file_path = os.path.join(local_path, os.path.basename(remote_path))
|
local_file_path = os.path.join(local_path, os.path.basename(remote_path))
|
||||||
with open(local_file_path, "wb") as f:
|
with open(local_file_path, "wb") as f:
|
||||||
f.write(response.content)
|
f.write(response.content)
|
||||||
|
|
||||||
self.log.info(f"File downloaded successfully: {local_file_path}")
|
log_info += f" - File downloaded successfully: {local_file_path}"
|
||||||
|
self.log.info(log_info)
|
||||||
|
self.log_to_db(log_time, log_info_type, log_info)
|
||||||
return {"downloaded_file": local_file_path}
|
return {"downloaded_file": local_file_path}
|
||||||
|
|
||||||
except requests.ConnectionError as e:
|
except requests.ConnectionError as e:
|
||||||
self.log.error(f"Connection error occurred while fetching the file: {str(e)}")
|
log_info += f" - Connection error: {str(e)}"
|
||||||
|
self.log.error(log_info)
|
||||||
|
self.log_to_db(log_time, log_info_type, log_info)
|
||||||
return {"error": "Connection error"}
|
return {"error": "Connection error"}
|
||||||
except requests.Timeout as e:
|
except requests.Timeout as e:
|
||||||
self.log.error(f"Timeout error occurred while fetching the file: {str(e)}")
|
log_info += f" - Timeout error: {str(e)}"
|
||||||
|
self.log.error(log_info)
|
||||||
|
self.log_to_db(log_time, log_info_type, log_info)
|
||||||
return {"error": "Timeout error"}
|
return {"error": "Timeout error"}
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
self.log.error(f"An unexpected error occurred: {str(e)}")
|
log_info += f" - Unexpected error: {str(e)}"
|
||||||
|
self.log.error(log_info)
|
||||||
|
self.log_to_db(log_time, log_info_type, log_info)
|
||||||
return {"error": "Unexpected error"}
|
return {"error": "Unexpected error"}
|
||||||
|
|
||||||
|
def log_to_db(self, log_time, log_info_type, log_info):
|
||||||
|
"""Save log information to the database."""
|
||||||
|
try:
|
||||||
|
# Use the Log class instead of the log instance
|
||||||
|
new_log_entry = Log(time=log_time, info_type=log_info_type, info=log_info)
|
||||||
|
new_log_entry.save() # Save the log entry to the database
|
||||||
|
except Exception as e:
|
||||||
|
self.log.error(f"Failed to save log to database: {str(e)}")
|
||||||
|
self.log.error(traceback.format_exc())
|
||||||
|
|
|
||||||
|
|
@ -5,7 +5,7 @@ from peewee import AutoField, DateTimeField, TextField
|
||||||
|
|
||||||
from .base_model import BaseModel, db
|
from .base_model import BaseModel, db
|
||||||
|
|
||||||
log = logging.getLogger("db_log")
|
db_logger = logging.getLogger("db_log")
|
||||||
|
|
||||||
|
|
||||||
class Log(BaseModel):
|
class Log(BaseModel):
|
||||||
|
|
@ -18,7 +18,7 @@ class Log(BaseModel):
|
||||||
@db.atomic()
|
@db.atomic()
|
||||||
def log(cls, info_type, info=None):
|
def log(cls, info_type, info=None):
|
||||||
cls.create(info_type=info_type, info=info)
|
cls.create(info_type=info_type, info=info)
|
||||||
log.info(f"{info_type}: {info}")
|
db_logger.info(f"{info_type}: {info}")
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
table_name = "log"
|
table_name = "log"
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user