This repository has been archived by the owner on Apr 26, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Fix empty url_cache_thumbnails/yyyy-mm-dd/
directories being left behind
#10924
Merged
squahtx
merged 6 commits into
develop
from
squah/fix_url_cache_thumbnail_directory_removal
Sep 29, 2021
Merged
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
d387b8e
Catch `FileNotFoundError`s instead of errnos in `_expire_url_cache_data`
ed23655
Extract 2 day expiry threshold into a constant
02d6f54
Fix empty `url_cache_thumbnails/yyyy-mm-dd/` directories being left b…
ecd5148
Add test for URL cache expiry
64733e9
Add newsfile
01492d6
Log failure to remove directories
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Fix a bug where empty `yyyy-mm-dd/` directories would be left behind in the media store's `url_cache_thumbnails/` directory. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,7 +13,6 @@ | |
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
import datetime | ||
import errno | ||
import fnmatch | ||
import itertools | ||
import logging | ||
|
@@ -73,6 +72,7 @@ | |
|
||
ONE_HOUR = 60 * 60 * 1000 | ||
ONE_DAY = 24 * ONE_HOUR | ||
IMAGE_CACHE_EXPIRY_MS = 2 * ONE_DAY | ||
|
||
|
||
@attr.s(slots=True, frozen=True, auto_attribs=True) | ||
|
@@ -504,20 +504,22 @@ async def _expire_url_cache_data(self) -> None: | |
fname = self.filepaths.url_cache_filepath(media_id) | ||
try: | ||
os.remove(fname) | ||
except FileNotFoundError: | ||
pass # If the path doesn't exist, meh | ||
except OSError as e: | ||
# If the path doesn't exist, meh | ||
if e.errno != errno.ENOENT: | ||
logger.warning("Failed to remove media: %r: %s", media_id, e) | ||
continue | ||
logger.warning("Failed to remove media: %r: %s", media_id, e) | ||
continue | ||
|
||
removed_media.append(media_id) | ||
|
||
try: | ||
dirs = self.filepaths.url_cache_filepath_dirs_to_delete(media_id) | ||
for dir in dirs: | ||
dirs = self.filepaths.url_cache_filepath_dirs_to_delete(media_id) | ||
for dir in dirs: | ||
try: | ||
os.rmdir(dir) | ||
except Exception: | ||
pass | ||
except FileNotFoundError: | ||
pass # Already deleted, continue with deleting the rest | ||
except Exception: | ||
break # Failed, skip deleting the rest of the parent dirs | ||
|
||
await self.store.delete_url_cache(removed_media) | ||
|
||
|
@@ -530,44 +532,50 @@ async def _expire_url_cache_data(self) -> None: | |
# These may be cached for a bit on the client (i.e., they | ||
# may have a room open with a preview url thing open). | ||
# So we wait a couple of days before deleting, just in case. | ||
expire_before = now - 2 * ONE_DAY | ||
expire_before = now - IMAGE_CACHE_EXPIRY_MS | ||
media_ids = await self.store.get_url_cache_media_before(expire_before) | ||
|
||
removed_media = [] | ||
for media_id in media_ids: | ||
fname = self.filepaths.url_cache_filepath(media_id) | ||
try: | ||
os.remove(fname) | ||
except FileNotFoundError: | ||
pass # If the path doesn't exist, meh | ||
except OSError as e: | ||
# If the path doesn't exist, meh | ||
if e.errno != errno.ENOENT: | ||
logger.warning("Failed to remove media: %r: %s", media_id, e) | ||
continue | ||
logger.warning("Failed to remove media: %r: %s", media_id, e) | ||
continue | ||
|
||
try: | ||
dirs = self.filepaths.url_cache_filepath_dirs_to_delete(media_id) | ||
for dir in dirs: | ||
dirs = self.filepaths.url_cache_filepath_dirs_to_delete(media_id) | ||
for dir in dirs: | ||
try: | ||
os.rmdir(dir) | ||
except Exception: | ||
pass | ||
except FileNotFoundError: | ||
pass # Already deleted, continue with deleting the rest | ||
except Exception: | ||
break # Failed, skip deleting the rest of the parent dirs | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. and here? |
||
|
||
thumbnail_dir = self.filepaths.url_cache_thumbnail_directory(media_id) | ||
try: | ||
shutil.rmtree(thumbnail_dir) | ||
except FileNotFoundError: | ||
pass # If the path doesn't exist, meh | ||
except OSError as e: | ||
# If the path doesn't exist, meh | ||
if e.errno != errno.ENOENT: | ||
logger.warning("Failed to remove media: %r: %s", media_id, e) | ||
continue | ||
logger.warning("Failed to remove media: %r: %s", media_id, e) | ||
continue | ||
|
||
removed_media.append(media_id) | ||
|
||
try: | ||
dirs = self.filepaths.url_cache_thumbnail_dirs_to_delete(media_id) | ||
for dir in dirs: | ||
dirs = self.filepaths.url_cache_thumbnail_dirs_to_delete(media_id) | ||
# Note that one of the directories to be deleted has already been | ||
# removed by the `rmtree` above. | ||
for dir in dirs: | ||
try: | ||
os.rmdir(dir) | ||
except Exception: | ||
pass | ||
except FileNotFoundError: | ||
pass # Already deleted, continue with deleting the rest | ||
except Exception: | ||
break # Failed, skip deleting the rest of the parent dirs | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. and here? |
||
|
||
await self.store.delete_url_cache_media(removed_media) | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
we should probably log something here?
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.
Sounds like a good idea. I got tired of the duplicated logic and factored it out into a function.