Skip to content

Commit

Permalink
Use set to only have group or hostnames once
Browse files Browse the repository at this point in the history
Only print unsync message if any devices were set to unsync
  • Loading branch information
indy-independence committed Oct 28, 2024
1 parent 6b7e9f7 commit f4c4c58
Showing 1 changed file with 10 additions and 9 deletions.
19 changes: 10 additions & 9 deletions src/cnaas_nms/db/git_worktrees.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import os
import shutil
from typing import List, Optional
from typing import Optional, Set

import git.exc
from cnaas_nms.app_settings import app_settings
Expand All @@ -19,7 +19,7 @@ class WorktreeError(Exception):
def refresh_existing_templates_worktrees(by: str, job_id: int, group_settings: dict, device_primary_groups: dict):
"""Look for existing worktrees and refresh them"""
logger = get_logger()
updated_groups: List[str] = []
updated_groups: Set[str] = set()
if os.path.isdir("/tmp/worktrees"):
for subdir in os.listdir("/tmp/worktrees"):
try:
Expand All @@ -31,10 +31,10 @@ def refresh_existing_templates_worktrees(by: str, job_id: int, group_settings: d
except Exception as e:
logger.exception(e)
shutil.rmtree("/tmp/worktrees/" + subdir, ignore_errors=True)
updated_groups.append(get_groups_using_branch(subdir, group_settings))
updated_groups.update(get_groups_using_branch(subdir, group_settings))

# find all devices that are using these branches and mark them as unsynchronized
updated_hostnames: List[str] = []
updated_hostnames: Set[str] = set()
with sqla_session() as session:
for hostname, primary_group in device_primary_groups.items():
if hostname in updated_hostnames:
Expand All @@ -44,12 +44,13 @@ def refresh_existing_templates_worktrees(by: str, job_id: int, group_settings: d
if dev:
dev.synchronized = False
add_sync_event(hostname, "refresh_templates", by, job_id)
updated_hostnames.append(hostname)
logger.debug(
"Devices marked as unsynchronized because git worktree branches were refreshed: {}".format(
", ".join(updated_hostnames)
updated_hostnames.add(hostname)
if updated_hostnames:
logger.debug(
"Devices marked as unsynchronized because git worktree branches were refreshed: {}".format(
", ".join(updated_hostnames)
)
)
)

local_repo = Repo(app_settings.TEMPLATES_LOCAL)
local_repo.git.worktree("prune")
Expand Down

0 comments on commit f4c4c58

Please sign in to comment.