-
Notifications
You must be signed in to change notification settings - Fork 1
⚡ Bolt: Parallelize folder deletion #164
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -651,7 +651,7 @@ | |||||||||||||||||||||||||||||||||||||||
| return [rule["PK"] for rule in folder_rules if rule.get("PK")] | ||||||||||||||||||||||||||||||||||||||||
| except httpx.HTTPError: | ||||||||||||||||||||||||||||||||||||||||
| return [] | ||||||||||||||||||||||||||||||||||||||||
| except Exception as e: | ||||||||||||||||||||||||||||||||||||||||
Check warningCode scanning / Pylint (reported by Codacy) Variable name "e" doesn't conform to snake_case naming style Warning
Variable name "e" doesn't conform to snake_case naming style
|
||||||||||||||||||||||||||||||||||||||||
| # We log error but don't stop the whole process; | ||||||||||||||||||||||||||||||||||||||||
| # individual folder failure shouldn't crash the sync | ||||||||||||||||||||||||||||||||||||||||
| log.warning(f"Error fetching rules for folder {folder_id}: {e}") | ||||||||||||||||||||||||||||||||||||||||
|
|
@@ -694,7 +694,7 @@ | |||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| log.info(f"Total existing rules across all folders: {len(all_rules)}") | ||||||||||||||||||||||||||||||||||||||||
| return all_rules | ||||||||||||||||||||||||||||||||||||||||
| except Exception as e: | ||||||||||||||||||||||||||||||||||||||||
Check noticeCode scanning / Pylintpython3 (reported by Codacy) Catching too general exception Exception Note
Catching too general exception Exception
|
||||||||||||||||||||||||||||||||||||||||
| log.error(f"Failed to get existing rules: {sanitize_for_log(e)}") | ||||||||||||||||||||||||||||||||||||||||
| return set() | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
|
|
@@ -825,7 +825,7 @@ | |||||||||||||||||||||||||||||||||||||||
| grp["PK"], | ||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||
| return str(grp["PK"]) | ||||||||||||||||||||||||||||||||||||||||
| except Exception as e: | ||||||||||||||||||||||||||||||||||||||||
Check noticeCode scanning / Pylint (reported by Codacy) Catching too general exception Exception Note
Catching too general exception Exception
|
||||||||||||||||||||||||||||||||||||||||
| log.warning(f"Error fetching groups on attempt {attempt}: {e}") | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| if attempt < MAX_RETRIES: | ||||||||||||||||||||||||||||||||||||||||
|
|
@@ -855,7 +855,7 @@ | |||||||||||||||||||||||||||||||||||||||
| status: int, | ||||||||||||||||||||||||||||||||||||||||
| hostnames: List[str], | ||||||||||||||||||||||||||||||||||||||||
| existing_rules: Set[str], | ||||||||||||||||||||||||||||||||||||||||
| client: httpx.Client, | ||||||||||||||||||||||||||||||||||||||||
Check warningCode scanning / Pylint (reported by Codacy) Wrong hanging indentation before block (add 4 spaces). Warning
Wrong hanging indentation before block (add 4 spaces).
|
||||||||||||||||||||||||||||||||||||||||
| ) -> bool: | ||||||||||||||||||||||||||||||||||||||||
| if not hostnames: | ||||||||||||||||||||||||||||||||||||||||
| log.info("Folder %s - no rules to push", sanitize_for_log(folder_name)) | ||||||||||||||||||||||||||||||||||||||||
|
|
@@ -1036,6 +1036,55 @@ | |||||||||||||||||||||||||||||||||||||||
| # --------------------------------------------------------------------------- # | ||||||||||||||||||||||||||||||||||||||||
| # 4. Main workflow | ||||||||||||||||||||||||||||||||||||||||
| # --------------------------------------------------------------------------- # | ||||||||||||||||||||||||||||||||||||||||
| def delete_existing_folders_parallel( | ||||||||||||||||||||||||||||||||||||||||
| client: httpx.Client, | ||||||||||||||||||||||||||||||||||||||||
| profile_id: str, | ||||||||||||||||||||||||||||||||||||||||
| folder_data_list: List[Dict[str, Any]], | ||||||||||||||||||||||||||||||||||||||||
Check warningCode scanning / Pylint (reported by Codacy) Wrong hanging indentation before block (add 4 spaces). Warning
Wrong hanging indentation before block (add 4 spaces).
|
||||||||||||||||||||||||||||||||||||||||
| existing_folders: Dict[str, str], | ||||||||||||||||||||||||||||||||||||||||
Check warningCode scanning / Pylint (reported by Codacy) Wrong hanging indentation before block (add 4 spaces). Warning
Wrong hanging indentation before block (add 4 spaces).
|
||||||||||||||||||||||||||||||||||||||||
| ) -> bool: | ||||||||||||||||||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||||||||||||||||||
| Identifies and deletes existing folders that match the new folder list. | ||||||||||||||||||||||||||||||||||||||||
| Deletes in parallel to save time. | ||||||||||||||||||||||||||||||||||||||||
| Updates existing_folders in-place by removing deleted folders. | ||||||||||||||||||||||||||||||||||||||||
| Returns True if any deletion occurred. | ||||||||||||||||||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||||||||||||||||||
| folders_to_delete = [] | ||||||||||||||||||||||||||||||||||||||||
| seen_deletions = set() | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| # Identify folders to delete | ||||||||||||||||||||||||||||||||||||||||
| for folder_data in folder_data_list: | ||||||||||||||||||||||||||||||||||||||||
| name = folder_data["group"]["group"].strip() | ||||||||||||||||||||||||||||||||||||||||
| if name in existing_folders and name not in seen_deletions: | ||||||||||||||||||||||||||||||||||||||||
| folders_to_delete.append((name, existing_folders[name])) | ||||||||||||||||||||||||||||||||||||||||
| seen_deletions.add(name) | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| if not folders_to_delete: | ||||||||||||||||||||||||||||||||||||||||
| return False | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| if not USE_COLORS: | ||||||||||||||||||||||||||||||||||||||||
| log.info(f"Deleting {len(folders_to_delete)} folders in parallel...") | ||||||||||||||||||||||||||||||||||||||||
Check noticeCode scanning / Pylintpython3 (reported by Codacy) Use lazy % formatting in logging functions Note
Use lazy % formatting in logging functions
Check warningCode scanning / Prospector (reported by Codacy) Use lazy % formatting in logging functions (logging-fstring-interpolation) Warning
Use lazy % formatting in logging functions (logging-fstring-interpolation)
|
||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| deletion_occurred = False | ||||||||||||||||||||||||||||||||||||||||
| # Using 5 workers to be safe with rate limits for write operations | ||||||||||||||||||||||||||||||||||||||||
| with concurrent.futures.ThreadPoolExecutor(max_workers=5) as executor: | ||||||||||||||||||||||||||||||||||||||||
| future_to_name = { | ||||||||||||||||||||||||||||||||||||||||
| executor.submit(delete_folder, client, profile_id, name, fid): name | ||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+1068
to
+1071
|
||||||||||||||||||||||||||||||||||||||||
| # Using 5 workers to be safe with rate limits for write operations | |
| with concurrent.futures.ThreadPoolExecutor(max_workers=5) as executor: | |
| future_to_name = { | |
| executor.submit(delete_folder, client, profile_id, name, fid): name | |
| # Protect shared httpx.Client instance when used across multiple threads. | |
| client_lock = threading.Lock() | |
| def _threadsafe_delete_folder(profile_id_arg: str, name_arg: str, fid_arg: str) -> bool: | |
| """ | |
| Wrapper around delete_folder that serializes access to the shared client. | |
| """ | |
| with client_lock: | |
| return delete_folder(client, profile_id_arg, name_arg, fid_arg) | |
| # Using 5 workers to be safe with rate limits for write operations | |
| with concurrent.futures.ThreadPoolExecutor(max_workers=5) as executor: | |
| future_to_name = { | |
| executor.submit(_threadsafe_delete_folder, profile_id, name, fid): name |
Check notice
Code scanning / Pylintpython3 (reported by Codacy)
Use lazy % formatting in logging functions Note
Outdated
Check warning
Code scanning / Prospector (reported by Codacy)
Use lazy % formatting in logging functions (logging-fstring-interpolation) Warning
Check warning
Code scanning / Pylintpython3 (reported by Codacy)
Variable name "e" doesn't conform to snake_case naming style Warning
| Original file line number | Diff line number | Diff line change | ||
|---|---|---|---|---|
| @@ -0,0 +1,124 @@ | ||||
|
|
||||
Check warningCode scanning / Pylintpython3 (reported by Codacy) Missing module docstring Warning test
Missing module docstring
Check warningCode scanning / Pylint (reported by Codacy) Missing module docstring Warning test
Missing module docstring
|
||||
| import concurrent.futures | ||||
Check noticeCode scanning / Pylintpython3 (reported by Codacy) Unused import concurrent.futures Note test
Unused import concurrent.futures
Check warningCode scanning / Prospector (reported by Codacy) Unused import concurrent.futures (unused-import) Warning test
Unused import concurrent.futures (unused-import)
Check noticeCode scanning / Pylint (reported by Codacy) Unused import concurrent.futures Note test
Unused import concurrent.futures
|
||||
| import concurrent.futures |
Check notice
Code scanning / Pylintpython3 (reported by Codacy)
Unused import pytest Note test
Check warning
Code scanning / Prospector (reported by Codacy)
Unused import pytest (unused-import) Warning test
Check warning
Code scanning / Prospector (reported by Codacy)
Unable to import 'pytest' (import-error) Warning test
Check notice
Code scanning / Pylint (reported by Codacy)
Unused import pytest Note test
Copilot
AI
Feb 5, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Import of 'pytest' is not used.
| import pytest |
Check warning
Code scanning / Pylint (reported by Codacy)
Missing function docstring Warning test
Check notice
Code scanning / Pylintpython3 (reported by Codacy)
Unused argument 'folder_id' Note test
Check notice
Code scanning / Pylintpython3 (reported by Codacy)
Unused argument 'client' Note test
Check warning
Code scanning / Prospector (reported by Codacy)
Unused argument 'folder_id' (unused-argument) Warning test
Check warning
Code scanning / Prospector (reported by Codacy)
expected 1 blank line before a nested definition, found 0 (E306) Warning test
Check notice
Code scanning / Pylintpython3 (reported by Codacy)
Unused argument 'name' Note test
Check warning
Code scanning / Prospector (reported by Codacy)
Unused argument 'profile_id' (unused-argument) Warning test
Check notice
Code scanning / Pylintpython3 (reported by Codacy)
Unused argument 'profile_id' Note test
Check warning
Code scanning / Prospector (reported by Codacy)
Unused argument 'client' (unused-argument) Warning test
Check warning
Code scanning / Prospector (reported by Codacy)
Unused argument 'name' (unused-argument) Warning test
Check notice
Code scanning / Pylint (reported by Codacy)
Unused argument 'profile_id' Note test
Check notice
Code scanning / Pylint (reported by Codacy)
Unused argument 'client' Note test
Check notice
Code scanning / Pylint (reported by Codacy)
Unused argument 'name' Note test
Check notice
Code scanning / Pylint (reported by Codacy)
Unused argument 'folder_id' Note test
Check notice
Code scanning / Pylintpython3 (reported by Codacy)
Unused variable 'original_len' Note test
Check notice
Code scanning / Pylint (reported by Codacy)
Unused variable 'original_len' Note test
Check warning
Code scanning / Prospector (reported by Codacy)
Unused variable 'original_len' (unused-variable) Warning test
Copilot
AI
Feb 5, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The variable original_len is assigned but never used. Consider removing it or using it in an assertion to verify the test setup.
Check notice
Code scanning / Bandit
Use of assert detected. The enclosed code will be removed when compiling to optimised byte code. Note test
Check notice
Code scanning / Bandit (reported by Codacy)
Use of assert detected. The enclosed code will be removed when compiling to optimised byte code. Note test
Check notice
Code scanning / Bandit
Use of assert detected. The enclosed code will be removed when compiling to optimised byte code. Note test
Check notice
Code scanning / Bandit (reported by Codacy)
Use of assert detected. The enclosed code will be removed when compiling to optimised byte code. Note test
Check notice
Code scanning / Bandit
Use of assert detected. The enclosed code will be removed when compiling to optimised byte code. Note test
Check notice
Code scanning / Bandit (reported by Codacy)
Use of assert detected. The enclosed code will be removed when compiling to optimised byte code. Note test
Check notice
Code scanning / Bandit
Use of assert detected. The enclosed code will be removed when compiling to optimised byte code. Note test
Check notice
Code scanning / Bandit (reported by Codacy)
Use of assert detected. The enclosed code will be removed when compiling to optimised byte code. Note test
Check notice
Code scanning / Bandit
Use of assert detected. The enclosed code will be removed when compiling to optimised byte code. Note test
Check notice
Code scanning / Bandit (reported by Codacy)
Use of assert detected. The enclosed code will be removed when compiling to optimised byte code. Note test
Check notice
Code scanning / Bandit
Use of assert detected. The enclosed code will be removed when compiling to optimised byte code. Note test
Check notice
Code scanning / Bandit (reported by Codacy)
Use of assert detected. The enclosed code will be removed when compiling to optimised byte code. Note test
Copilot
AI
Feb 5, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The timing assertion could be flaky on slower systems or under heavy load. Consider either increasing the threshold (e.g., to 0.8s or 1.0s) or using a more flexible assertion that checks if the parallel version is faster than sequential without relying on absolute time, such as comparing the duration to the theoretical sequential time with a tolerance factor.
Check notice
Code scanning / Bandit
Use of assert detected. The enclosed code will be removed when compiling to optimised byte code. Note test
Check notice
Code scanning / Bandit (reported by Codacy)
Use of assert detected. The enclosed code will be removed when compiling to optimised byte code. Note test
Check notice
Code scanning / Pylint (reported by Codacy)
Unused argument 'monkeypatch' Note test
Check notice
Code scanning / Pylintpython3 (reported by Codacy)
Unused argument 'monkeypatch' Note test
Check warning
Code scanning / Prospector (reported by Codacy)
Unused argument 'monkeypatch' (unused-argument) Warning test
Check notice
Code scanning / Bandit
Use of assert detected. The enclosed code will be removed when compiling to optimised byte code. Note test
Check notice
Code scanning / Bandit (reported by Codacy)
Use of assert detected. The enclosed code will be removed when compiling to optimised byte code. Note test
Check notice
Code scanning / Bandit
Use of assert detected. The enclosed code will be removed when compiling to optimised byte code. Note test
Check notice
Code scanning / Bandit (reported by Codacy)
Use of assert detected. The enclosed code will be removed when compiling to optimised byte code. Note test
Check warning
Code scanning / Pylint (reported by Codacy)
Missing function docstring Warning test
Check notice
Code scanning / Pylint (reported by Codacy)
Unused argument 'folder_id' Note test
Check notice
Code scanning / Pylint (reported by Codacy)
Unused argument 'profile_id' Note test
Check notice
Code scanning / Pylint (reported by Codacy)
Unused argument 'client' Note test
Check notice
Code scanning / Pylintpython3 (reported by Codacy)
Unused argument 'folder_id' Note test
Check warning
Code scanning / Prospector (reported by Codacy)
expected 1 blank line before a nested definition, found 0 (E306) Warning test
Check notice
Code scanning / Pylintpython3 (reported by Codacy)
Unused argument 'profile_id' Note test
Check warning
Code scanning / Prospector (reported by Codacy)
Unused argument 'profile_id' (unused-argument) Warning test
Check warning
Code scanning / Prospector (reported by Codacy)
Unused argument 'client' (unused-argument) Warning test
Check notice
Code scanning / Pylintpython3 (reported by Codacy)
Unused argument 'client' Note test
Check warning
Code scanning / Prospector (reported by Codacy)
Unused argument 'folder_id' (unused-argument) Warning test
Check notice
Code scanning / Bandit
Use of assert detected. The enclosed code will be removed when compiling to optimised byte code. Note test
Check notice
Code scanning / Bandit (reported by Codacy)
Use of assert detected. The enclosed code will be removed when compiling to optimised byte code. Note test
Check notice
Code scanning / Bandit
Use of assert detected. The enclosed code will be removed when compiling to optimised byte code. Note test
Check notice
Code scanning / Bandit (reported by Codacy)
Use of assert detected. The enclosed code will be removed when compiling to optimised byte code. Note test
Check notice
Code scanning / Bandit
Use of assert detected. The enclosed code will be removed when compiling to optimised byte code. Note test
Check notice
Code scanning / Bandit (reported by Codacy)
Use of assert detected. The enclosed code will be removed when compiling to optimised byte code. Note test
Check warning
Code scanning / Pylint (reported by Codacy)
Wrong hanging indentation before block (add 4 spaces). Warning