Skip to content

Commit

Permalink
feat: add scheduled task to clean users documents folder at 00:00
Browse files Browse the repository at this point in the history
  • Loading branch information
dev-lymar committed Jun 28, 2024
1 parent 8424a1d commit 790117c
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
22 changes: 22 additions & 0 deletions makedoc/tasks.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import os
import shutil

from celery import shared_task
from django.conf import settings


@shared_task
def delete_files():
directory = settings.BASE_DIR / "makedoc" / "tempdoc"

if not os.path.exists(directory):
return "Directory does not exist"
for item in os.listdir(directory):
item_path = os.path.join(directory, item)
try:
if os.path.isdir(item_path):
shutil.rmtree(item_path)
except Exception:
return f"Failed to delete {item_path}. Reason {e}"

return "Successfully deleted all user folders"
10 changes: 10 additions & 0 deletions melnichanka/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from datetime import timedelta
from pathlib import Path

from celery.schedules import crontab
from dotenv import load_dotenv

load_dotenv()
Expand Down Expand Up @@ -215,3 +216,12 @@
"VERSION": "1.0.0",
"SERVE_INCLUDE_SCHEMA": False,
}

# Celery

CELERY_BEAT_SCHEDULE = {
"delete_files_daily": {
"task": "makedoc.tasks.delete_files",
"schedule": crontab(hour=23, minute=59),
},
}

0 comments on commit 790117c

Please sign in to comment.