Skip to content

Commit

Permalink
fix(temp files): added task to clear out old temp files
Browse files Browse the repository at this point in the history
closes #70
  • Loading branch information
Christopher Pickering committed Apr 22, 2022
1 parent 011427e commit ffef5c5
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 1 deletion.
47 changes: 47 additions & 0 deletions scheduler/maintenance.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
"""Scheduler Maintenance."""

import os
import shutil
import time
from pathlib import Path

from sqlalchemy import update

from scheduler.extensions import atlas_scheduler, db
Expand Down Expand Up @@ -40,3 +45,45 @@ def job_sync() -> None:
# pylint: disable=broad-except
except BaseException as e:
print(str(e)) # noqa: T001


@atlas_scheduler.task(
"interval",
id="temp_clean",
hours=1,
max_instances=1,
start_date="2000-01-01 12:19:00",
)
def temp_clean() -> None:
"""Job to clean up dangling temp files."""
try:
with atlas_scheduler.app.app_context():
temp_path = Path(__file__).parents[1].joinpath("runner", "temp")
if not temp_path.exists():
return

# glob to get us to run id
# temp/project/task/runid
for file in temp_path.glob("*/*/*"):
# older than 2 hour
if os.stat(file.resolve()).st_mtime < time.time() - 7200:

try:
if (
Path(file.resolve()).exists()
and Path(file.resolve()).is_dir()
):
shutil.rmtree(file.resolve())
if (
Path(file.resolve()).exists()
and Path(file.resolve()).is_file()
):
os.remove(file.resolve())

# pylint: disable=broad-except
except BaseException as e:
print(str(e)) # noqa: T001

# pylint: disable=broad-except
except BaseException as e:
print(str(e)) # noqa: T001
2 changes: 1 addition & 1 deletion scheduler/web.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def schedule() -> Response:

for job in atlas_scheduler.get_jobs():
if (
job.id == "job_sync"
job.id in ["job_sync", "temp_clean"]
or not hasattr(job, "next_run_time")
or job.next_run_time is None
and job.args
Expand Down

0 comments on commit ffef5c5

Please sign in to comment.