Release 0.178.3#2906
Conversation
* Update reset_sync_states management command * Improve efficiency and messages to user
Summary of ChangesHello @odlbot, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request constitutes release Highlights
Changelog
Activity
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request updates the reset_sync_states management command to allow syncing specific websites passed via filters. The changes are generally good, but I've identified a few areas for improvement related to performance, potential rate-limiting issues, and code maintainability. Specifically, there's an N+1 query when fetching websites, a missing call to throttle backend API calls which could lead to rate-limiting, and some code duplication that could be refactored.
| filtered_websites = [ | ||
| fetch_website(site_identifier) for site_identifier in self.filter_list | ||
| ] |
There was a problem hiding this comment.
This list comprehension executes a separate database query for each site identifier in self.filter_list. This can lead to performance issues (N+1 queries) if the list of sites is large. It would be more efficient to fetch all the websites in a single bulk query.
from websites.models import Website
from django.db.models import Q
filtered_websites = []
if self.filter_list:
filtered_websites = list(Website.objects.filter(
Q(name__in=self.filter_list) | Q(short_id__in=self.filter_list)
))| for website in filtered_websites: | ||
| backend = get_sync_backend(website) | ||
| if create_backends or backend.backend_exists(): | ||
| self.stdout.write( | ||
| f"Syncing website '{website.title}' to backend..." | ||
| ) | ||
| backend.create_website_in_backend() | ||
| backend.sync_all_content_to_backend() | ||
| reset_publishing_fields(website.name) | ||
| else: | ||
| self.stderr.write( | ||
| f"Skipping website '{website.title}': " | ||
| "backend does not exist " | ||
| "(use --create_backends to create it)" | ||
| ) |
There was a problem hiding this comment.
The loop for syncing filtered websites performs backend operations but does not include throttling. The sync_unsynced_websites task calls api.throttle_git_backend_calls to prevent hitting API rate limits. This is especially important for git-based backends when syncing a large number of websites. Please add a call to api.throttle_git_backend_calls(backend) within the loop.
for website in filtered_websites:
backend = get_sync_backend(website)
from content_sync.api import throttle_git_backend_calls
throttle_git_backend_calls(backend)
if create_backends or backend.backend_exists():
self.stdout.write(
f"Syncing website '{website.title}' to backend..."
)
backend.create_website_in_backend()
backend.sync_all_content_to_backend()
reset_publishing_fields(website.name)
else:
self.stderr.write(
f"Skipping website '{website.title}': "
"backend does not exist "
"(use --create_backends to create it)"
)| if filtered_websites: | ||
| self.stdout.write( | ||
| f"Syncing {len(filtered_websites)} filtered " | ||
| "website(s) to the designated backend" | ||
| ) | ||
| for website in filtered_websites: | ||
| backend = get_sync_backend(website) | ||
| if create_backends or backend.backend_exists(): | ||
| self.stdout.write( | ||
| f"Syncing website '{website.title}' to backend..." | ||
| ) | ||
| backend.create_website_in_backend() | ||
| backend.sync_all_content_to_backend() | ||
| reset_publishing_fields(website.name) | ||
| else: | ||
| self.stderr.write( | ||
| f"Skipping website '{website.title}': " | ||
| "backend does not exist " | ||
| "(use --create_backends to create it)" | ||
| ) | ||
| else: | ||
| self.stdout.write( | ||
| "Syncing all unsynced websites to the designated backend" | ||
| ) | ||
| task = sync_unsynced_websites.delay(create_backends=create_backends) | ||
| self.stdout.write(f"Starting task {task}...") | ||
| task.get() |
There was a problem hiding this comment.
The logic for syncing a website inside the if filtered_websites: block is very similar to the logic inside the sync_unsynced_websites Celery task. This duplication could make future maintenance harder. Also, there's an inconsistency: reset_publishing_fields is called after syncing here, but before syncing in the task. Consider refactoring the common logic into a shared function to improve maintainability and ensure consistency.
pt2302