refactor StartupCheck into class, lock if already run for multithreading

This commit is contained in:
simon 2022-03-22 10:35:36 +07:00
parent 1f1dfcb54f
commit fd4f15ab66
No known key found for this signature in database
GPG Key ID: 2C15AA5E89985DD4

View File

@ -8,14 +8,38 @@ from home.src.ta.config import AppConfig as ArchivistConfig
from home.src.ta.ta_redis import RedisArchivist from home.src.ta.ta_redis import RedisArchivist
def sync_redis_state(): class StartupCheck:
"""checks to run at application startup"""
def __init__(self):
self.config_handler = ArchivistConfig()
self.redis_con = RedisArchivist()
self.has_run = self.get_has_run()
def run(self):
"""run all startup checks"""
print("run startup checks")
self.release_lock()
index_check()
self.sync_redis_state()
self.make_folders()
self.set_has_run()
def get_has_run(self):
"""validate if check has already executed"""
return self.redis_con.get_message("startup_check")
def set_has_run(self):
"""startup checks run"""
message = {"status": True}
self.redis_con.set_message("startup_check", message, expire=120)
def sync_redis_state(self):
"""make sure redis gets new config.json values""" """make sure redis gets new config.json values"""
print("sync redis") print("sync redis")
config_handler = ArchivistConfig() self.config_handler.load_new_defaults()
config_handler.load_new_defaults()
def make_folders(self):
def make_folders():
"""make needed cache folders here so docker doesn't mess it up""" """make needed cache folders here so docker doesn't mess it up"""
folders = [ folders = [
"download", "download",
@ -25,8 +49,7 @@ def make_folders():
"import", "import",
"backup", "backup",
] ]
config = ArchivistConfig().config cache_dir = self.config_handler.config["application"]["cache_dir"]
cache_dir = config["application"]["cache_dir"]
for folder in folders: for folder in folders:
folder_path = os.path.join(cache_dir, folder) folder_path = os.path.join(cache_dir, folder)
try: try:
@ -34,10 +57,10 @@ def make_folders():
except FileExistsError: except FileExistsError:
continue continue
def release_lock(self):
def release_lock(): """make sure there are no leftover locks set in redis"""
"""make sure there are no leftover locks set in redis on container start"""
all_locks = [ all_locks = [
"startup_check",
"manual_import", "manual_import",
"downloading", "downloading",
"dl_queue", "dl_queue",
@ -45,7 +68,7 @@ def release_lock():
"rescan", "rescan",
] ]
for lock in all_locks: for lock in all_locks:
response = RedisArchivist().del_message(lock) response = self.redis_con.del_message(lock)
if response: if response:
print("deleted leftover key from redis: " + lock) print("deleted leftover key from redis: " + lock)
@ -57,7 +80,9 @@ class HomeConfig(AppConfig):
name = "home" name = "home"
def ready(self): def ready(self):
release_lock() startup = StartupCheck()
index_check() if startup.has_run["status"]:
sync_redis_state() print("startup checks run in other thread")
make_folders() return
startup.run()