Skip to content
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

Cleanup script to search and remove rageshakes from applications based on a time #61

Merged
merged 10 commits into from
Jan 16, 2023

Conversation

michaelkaye
Copy link
Contributor

This script as a cronjob could be used to tidy up old rageshakes and implement #59

@michaelkaye michaelkaye force-pushed the michaelk/script_to_remove_unwanted_rageshkes branch from 0c40cba to 3f57c18 Compare December 14, 2022 12:19
@michaelkaye
Copy link
Contributor Author

Have tried this in dry run mode on the server. and it is selecting the right rageshakes to delete.

@michaelkaye michaelkaye marked this pull request as ready for review January 11, 2023 17:21
Copy link
Member

@richvdh richvdh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks good but oops I seem to have made a lot of comments!

.gitignore Outdated Show resolved Hide resolved
changelog.d/61.feature Outdated Show resolved Hide resolved
scripts/cleanup.py Show resolved Hide resolved
scripts/cleanup.py Outdated Show resolved Hide resolved
scripts/cleanup.py Outdated Show resolved Hide resolved
scripts/cleanup.py Outdated Show resolved Hide resolved
scripts/cleanup.py Outdated Show resolved Hide resolved
app_name = parts[1].strip()
if parts[0] == "user_id":
mxid = parts[1].strip()
print(f"app_name {app_name} user_id {mxid}")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggest moving everything other than the with gzip.open block outside the try/except.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure about this;

I was aiming for not taking any action (deletion etc) if the details.log.gz file was not found. Putting the remaining logic outside the try/except would mean it does get run, even if the (current) if block would always evaluate to False as app_name would always be None.

I feel it's more obvious if we simply don't evaluate the decision at all when we get a file not found error.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I failed to add: you'd want an early return False inside the except block.

The idea is to be clearer about which parts of the code we're expecting to throw. For example: currently we need to worry about what happens if _delete throws a FileNotFoundError. In that case, we'll report a misleading message about not being able to check the application name.

Generally, it's good practice to be minimal with the size of your try block; sticking extra code inside the try block as a way of skipping that code if the exception is throw means (a) you could end up catching exceptions you didn't want to; (b) it disrupts the flow of the code: the error handling for gzip.open is now a long way from that call.

scripts/cleanup.py Outdated Show resolved Hide resolved
scripts/cleanup.py Show resolved Hide resolved
scripts/cleanup.py Outdated Show resolved Hide resolved
scripts/cleanup.py Outdated Show resolved Hide resolved
scripts/cleanup.py Outdated Show resolved Hide resolved
app_name = parts[1].strip()
if parts[0] == "user_id":
mxid = parts[1].strip()
print(f"app_name {app_name} user_id {mxid}")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I failed to add: you'd want an early return False inside the except block.

The idea is to be clearer about which parts of the code we're expecting to throw. For example: currently we need to worry about what happens if _delete throws a FileNotFoundError. In that case, we'll report a misleading message about not being able to check the application name.

Generally, it's good practice to be minimal with the size of your try block; sticking extra code inside the try block as a way of skipping that code if the exception is throw means (a) you could end up catching exceptions you didn't want to; (b) it disrupts the flow of the code: the error handling for gzip.open is now a long way from that call.

Copy link
Member

@richvdh richvdh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm!

Comment on lines +205 to +206
for l in args.limits:
parts = l.rsplit(":", 1)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

l may be a poor name for a var due to the confusion with I and 1, but 🤷‍♂️

@michaelkaye michaelkaye merged commit 3f03f64 into master Jan 16, 2023
@michaelkaye michaelkaye deleted the michaelk/script_to_remove_unwanted_rageshkes branch January 16, 2023 18:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants