Skip to content

Commit 6b5ca98

Browse files
Process deleting projects as background task
1. Fix Default Filtering in ProjectFilterSet: - Modified the `ProjectFilterSet` to include `is_marked_for_deletion` in the default filtering when `is_archived` is not provided. - This ensures that projects marked for deletion are excluded from default views. 2. Migration for `is_marked_for_deletion`: - Added a migration (`0052_project_is_marked_for_deletion.py`) to introduce the `is_marked_for_deletion` field in the `Project` model. 3. Updated `Project` Model: - Added `is_marked_for_deletion` field to the `Project` model with a default value of `False`. 4. Modified `ProjectListView` to Exclude Marked Projects: - Modified the `get_queryset` method in `ProjectListView` to only fetch projects with `is_marked_for_deletion=False`. 5. Introduced `mark_for_deletion` Method: - Added a `mark_for_deletion` method in the `Project` model to set the `is_marked_for_deletion` flag and save the model. 6. Changed `delete` Method in `Project` Model: - Renamed the `delete` method to `delete_action` in the `Project` model to avoid conflicts. - Introduced a new `delete` method that marks the project for deletion and enqueues a background deletion task. 7. Background Deletion Task: - Created a background deletion task using `django_rq` to handle project deletion asynchronously. - Checks if the project is still marked for deletion before proceeding. - If an error occurs during deletion, updates project status and logs an error. 8. Updated `ProjectActionView` Success Message: - Modified the success message in `ProjectActionView` for the "delete" action to provide a more informative message based on the count. Fixes: #1002 Signed-off-by: Jayanth Kumar <[email protected]>
1 parent 71f33c7 commit 6b5ca98

File tree

5 files changed

+51
-3
lines changed

5 files changed

+51
-3
lines changed

scanpipe/filters.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -349,9 +349,9 @@ def __init__(self, data=None, *args, **kwargs):
349349

350350
# Default filtering by "Active" projects.
351351
if not data or data.get("is_archived", "") == "":
352-
self.queryset = self.queryset.filter(is_archived=False)
352+
self.queryset = self.queryset.filter(is_archived=False, is_marked_for_deletion=False)
353353

354-
active_count = Project.objects.filter(is_archived=False).count()
354+
active_count = Project.objects.filter(is_archived=False, is_marked_for_deletion=False).count()
355355
archived_count = Project.objects.filter(is_archived=True).count()
356356
self.filters["is_archived"].extra["widget"] = BulmaLinkWidget(
357357
choices=[
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Generated by Django 5.0.1 on 2024-01-26 12:25
2+
3+
from django.db import migrations, models
4+
5+
6+
class Migration(migrations.Migration):
7+
8+
dependencies = [
9+
('scanpipe', '0051_rename_pipelines_data'),
10+
]
11+
12+
operations = [
13+
migrations.AddField(
14+
model_name='project',
15+
name='is_marked_for_deletion',
16+
field=models.BooleanField(default=False),
17+
),
18+
]

scanpipe/models.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@
7979
from rq.command import send_stop_job_command
8080
from rq.exceptions import NoSuchJobError
8181
from rq.job import Job
82+
from rq import Queue
8283
from rq.job import JobStatus
8384
from taggit.managers import TaggableManager
8485
from taggit.models import GenericUUIDTaggedItemBase
@@ -534,6 +535,7 @@ class Project(UUIDPKModel, ExtraDataFieldMixin, UpdateMixin, models.Model):
534535
labels = TaggableManager(through=UUIDTaggedItem)
535536

536537
objects = ProjectQuerySet.as_manager()
538+
is_marked_for_deletion = models.BooleanField(default=False)
537539

538540
class Meta:
539541
ordering = ["-created_date"]
@@ -621,7 +623,7 @@ def delete_related_objects(self):
621623

622624
return deleted_counter
623625

624-
def delete(self, *args, **kwargs):
626+
def delete_action(self, *args, **kwargs):
625627
"""Delete the `work_directory` along project-related data in the database."""
626628
self._raise_if_run_in_progress()
627629

@@ -633,6 +635,16 @@ def delete(self, *args, **kwargs):
633635

634636
return super().delete(*args, **kwargs)
635637

638+
def mark_for_deletion(self):
639+
self.is_marked_for_deletion = True
640+
self.save()
641+
642+
def delete(self):
643+
# Mark the project for deletion and enqueue background deletion task
644+
self.mark_for_deletion()
645+
q = Queue("default", connection=redis.Redis())
646+
job = q.enqueue(tasks.background_delete_task, self)
647+
636648
def reset(self, keep_input=True):
637649
"""
638650
Reset the project by deleting all related database objects and all work

scanpipe/tasks.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import logging
2424

2525
from django.apps import apps
26+
from django_rq import job
2627

2728
logger = logging.getLogger(__name__)
2829

@@ -76,3 +77,18 @@ def execute_pipeline_task(run_pk):
7677
project.clear_tmp_directory()
7778
if next_run := project.get_next_run():
7879
next_run.start()
80+
81+
@job
82+
def background_delete_task(project):
83+
# Check if the project is still marked for deletion
84+
if not project.is_marked_for_deletion:
85+
return
86+
87+
# Perform the deletion process
88+
try:
89+
project.delete_action()
90+
except Exception as e:
91+
# Handle errors and update project errors or display a banner
92+
project.is_marked_for_deletion = False
93+
project.save()
94+
project.add_error(description=f"Deletion failed: {str(e)}")

scanpipe/views.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1086,6 +1086,8 @@ def perform_action(self, action, project_uuid, action_kwargs=None):
10861086
raise Http404
10871087

10881088
def get_success_message(self, action, count):
1089+
if action == "delete":
1090+
return f"{count} project{'s' if count != 1 else ''} {'is' if count == 1 else 'are'} being deleted in the background."
10891091
return f"{count} projects have been {action}."
10901092

10911093

0 commit comments

Comments
 (0)