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

Fix log cleanup script #1628

Merged
merged 4 commits into from
Aug 16, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions jwql/website/apps/jwql/clean_old_log_files.py
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -38,19 +38,25 @@ def define_options():
"""
usage = 'clean_old_log_files.py -t 14'
parser = argparse.ArgumentParser(usage=usage)
parser.add_argument('-t', '--time_limit', type=int, default=14,
parser.add_argument('-t', '--time_limit', type=float, default=14,
help='Time limit in days. Log files older than this will be deleted.')
parser.add_argument('-d', '--dry_run', action="store_true",
help='If True, the log files that would be deleted are printed to the screen')
return parser


def run(time_limit=timedelta(days=14)):
def run(time_limit=timedelta(days=14), dry_run=False):
"""Look through log directories and delete log files that are older than ``time_limit``.
Have time_limit default to be 14 days.

Inputs
------
time_limit : datetime.timdelta
Files older than this time limit will be deleted

dry_run : bool
If True, log files will not be deleted. Those that would be deleted are instead
printed to the screen
"""
now = datetime.now()

Expand All @@ -75,10 +81,13 @@ def run(time_limit=timedelta(days=14)):
age = now - last_modified_time
if age > time_limit:
full_path = os.path.join(log_dir, logtype, item)
os.remove(full_path)
if not dry_run:
os.remove(full_path)
else:
print(f'DELETE: {full_path}')


if __name__ == '__main__':
parser = define_options()
args = parser.parse_args()
run(timedelta(days=args.time_limit))
run(time_limit=timedelta(days=args.time_limit), dry_run=args.dry_run)
Loading