Refactor self_update function in checker.py to accept a logger parameter for improved logging. This change enhances the update process by providing consistent logging messages for update checks, successes, and errors, thereby improving the clarity and maintainability of the script's update functionality.

This commit is contained in:
vvzvlad 2025-01-19 11:29:48 +03:00
parent b93e5f890a
commit b89e6c20ce

View File

@ -27,7 +27,8 @@ import colorama
import logging import logging
import socket import socket
def self_update(): def self_update(logger):
logger.info("Checking for updates..")
script_path = os.path.abspath(__file__) script_path = os.path.abspath(__file__)
update_url = "https://gitea.vvzvlad.xyz/vvzvlad/ritual/raw/branch/main-22aug/checker.py" update_url = "https://gitea.vvzvlad.xyz/vvzvlad/ritual/raw/branch/main-22aug/checker.py"
try: try:
@ -40,14 +41,14 @@ def self_update():
if current_content != response.text: if current_content != response.text:
with open(script_path, 'w', encoding='utf-8') as f: with open(script_path, 'w', encoding='utf-8') as f:
f.write(response.text) f.write(response.text)
logging.info("Script updated successfully, restarting") logger.info("Script updated successfully, restarting")
os.execv(sys.executable, ['python3'] + sys.argv) os.execv(sys.executable, ['python3'] + sys.argv)
else: else:
logging.info("Script is up to date") logger.info("Script is up to date")
else: else:
logging.error(f"Failed to download update, status code: {response.status_code}") logger.error(f"Failed to download update, status code: {response.status_code}")
except Exception as e: except Exception as e:
logging.error(f"Update error: {str(e)}") logger.error(f"Update error: {str(e)}")
class GRIST: class GRIST:
def __init__(self, server, doc_id, api_key, logger): def __init__(self, server, doc_id, api_key, logger):
@ -147,7 +148,7 @@ if __name__ == "__main__":
logger.addHandler(ch) logger.addHandler(ch)
logger.info("Checker started") logger.info("Checker started")
self_update() self_update(logger)
random_sleep = random.randint(1, 60) random_sleep = random.randint(1, 60)
logger.info(f"Sleeping for {random_sleep} seconds") logger.info(f"Sleeping for {random_sleep} seconds")
time.sleep(random_sleep) time.sleep(random_sleep)