Skip to content

Commit

Permalink
Merge pull request #1958 from bunkerity/dev
Browse files Browse the repository at this point in the history
Refactor failover backup functionality to make it stabler
  • Loading branch information
TheophileDiot authored Jan 29, 2025
2 parents c9f5634 + 43b2508 commit 4306015
Showing 1 changed file with 32 additions and 7 deletions.
39 changes: 32 additions & 7 deletions src/scheduler/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
from ApiCaller import ApiCaller # type: ignore

APPLYING_CHANGES = Event()
BACKING_UP_FAILOVER = Event()

RUN = True
SCHEDULER: Optional[JobScheduler] = None
SCHEDULER_LOCK = Lock()
Expand Down Expand Up @@ -481,6 +483,31 @@ def healthcheck_job():
HEALTHCHECK_EVENT.clear()


def backup_failover():
BACKING_UP_FAILOVER.set()
try:
rmtree(FAILOVER_PATH, ignore_errors=True)
FAILOVER_PATH.mkdir(parents=True, exist_ok=True)

for src, dst_name in (
(CONFIG_PATH, "config"),
(CUSTOM_CONFIGS_PATH, "custom_configs"),
(CACHE_PATH, "cache"),
):
try:
copytree(src, FAILOVER_PATH / dst_name, dirs_exist_ok=True)
except Exception as e:
LOGGER.error(f"Error copying {src} to failover path: {e}")

success, err = JOB.cache_dir(FAILOVER_PATH, job_name="failover-backup")
if not success:
LOGGER.error(f"Error while caching failover backup: {err}")
except Exception as e:
LOGGER.error(f"Failed to initialize failover backup: {e}")
finally:
BACKING_UP_FAILOVER.clear()


if __name__ == "__main__":
try:
# Don't execute if pid file exists
Expand Down Expand Up @@ -748,6 +775,10 @@ def check_plugin_changes(_type: Literal["external", "pro"] = "external"):
while True:
threads.clear()

while BACKING_UP_FAILOVER.is_set():
LOGGER.warning("Waiting for the failover backup to finish ...")
sleep(1)

if RUN_JOBS_ONCE:
# Only run jobs once
if not SCHEDULER.reload(
Expand Down Expand Up @@ -897,13 +928,7 @@ def check_plugin_changes(_type: Literal["external", "pro"] = "external"):
LOGGER.warning("No BunkerWeb instance is reachable, skipping failover ...")
else:
LOGGER.info("Successfully reloaded bunkerweb")
# Update the failover path with the working configuration
rmtree(FAILOVER_PATH, ignore_errors=True)
FAILOVER_PATH.mkdir(parents=True, exist_ok=True)
copytree(CONFIG_PATH, FAILOVER_PATH.joinpath("config"))
copytree(CUSTOM_CONFIGS_PATH, FAILOVER_PATH.joinpath("custom_configs"))
copytree(CACHE_PATH, FAILOVER_PATH.joinpath("cache"))
Thread(target=JOB.cache_dir, args=(FAILOVER_PATH,), kwargs={"job_name": "failover-backup"}).start()
Thread(target=backup_failover).start()
except BaseException as e:
LOGGER.error(f"Exception while executing failover logic : {e}")

Expand Down

0 comments on commit 4306015

Please sign in to comment.