Skip to content

Commit

Permalink
feat: add a function to clean tmp after 30 min
Browse files Browse the repository at this point in the history
  • Loading branch information
fabiogra committed Jul 13, 2023
1 parent 9baeeda commit 72ad181
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 0 deletions.
35 changes: 35 additions & 0 deletions app/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from io import BytesIO
from pathlib import Path
from loguru import logger as log
import time

import matplotlib.pyplot as plt
import numpy as np
Expand Down Expand Up @@ -173,3 +174,37 @@ def file_size_is_valid(response):
st.error(
f"The file is too large to download. Maximum size allowed: {max_size_mb}MB. Duplicate this space to remove any limit."
)


def _get_files_to_not_delete():
not_delete = []
if os.environ.get("PREPARE_SAMPLES"):
for filename in ["sample_songs.json", "separate_songs.json"]:
try:
with open(filename) as f:
not_delete += list(json.load(f).keys())
except Exception as e:
log.warning(e)
return not_delete


def _remove_file_older_than(file_path: str, max_age_limit: float):
# If the file is older than the age limit, delete it
if os.path.getmtime(file_path) < max_age_limit:
try:
os.remove(file_path)
except OSError as e:
log.warning(f"Error: Could not delete {file_path}. Reason: {e.strerror}")


def delete_old_files(directory: str, age_limit_seconds: int):
files_to_not_delete = _get_files_to_not_delete()
age_limit = time.time() - age_limit_seconds

# Walk through the directory
for dirpath, dirnames, filenames in os.walk(directory):
if dirpath.split("/")[-1] not in files_to_not_delete:
for filename in filenames:
if filename.split(".")[0] not in files_to_not_delete:
file_path = os.path.join(dirpath, filename)
_remove_file_older_than(file_path, age_limit)
2 changes: 2 additions & 0 deletions app/pages/About.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from header import header
from footer import footer
from helpers import delete_old_files


def body():
Expand Down Expand Up @@ -152,3 +153,4 @@ def body():
header(logo_and_title=False)
body()
footer()
delete_old_files("/tmp", 60 * 30)
2 changes: 2 additions & 0 deletions app/pages/Karaoke.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
load_audio_segment,
streamlit_player,
local_audio,
delete_old_files,
)

from service.vocal_remover.runner import separate, load_model
Expand Down Expand Up @@ -182,3 +183,4 @@ def body():
header()
body()
footer()
delete_old_files("/tmp", 60 * 30)
2 changes: 2 additions & 0 deletions app/pages/Separate.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
st_local_audio,
url_is_valid,
file_size_is_valid,
delete_old_files,
)
from service.demucs_runner import separator
from service.vocal_remover.runner import load_model, separate
Expand Down Expand Up @@ -281,3 +282,4 @@ def body():
header()
body()
footer()
delete_old_files("/tmp", 60 * 30)

0 comments on commit 72ad181

Please sign in to comment.