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

Process deleting projects as background task #1060

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
8 changes: 6 additions & 2 deletions scanpipe/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -349,9 +349,13 @@ def __init__(self, data=None, *args, **kwargs):

# Default filtering by "Active" projects.
if not data or data.get("is_archived", "") == "":
self.queryset = self.queryset.filter(is_archived=False)
self.queryset = self.queryset.filter(
is_archived=False, is_marked_for_deletion=False
)
jayanth-kumar-morem marked this conversation as resolved.
Show resolved Hide resolved

active_count = Project.objects.filter(is_archived=False).count()
active_count = Project.objects.filter(
is_archived=False, is_marked_for_deletion=False
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should use active() here as well, no?

).count()
archived_count = Project.objects.filter(is_archived=True).count()
self.filters["is_archived"].extra["widget"] = BulmaLinkWidget(
choices=[
Expand Down
18 changes: 18 additions & 0 deletions scanpipe/migrations/0052_project_is_marked_for_deletion.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 5.0.1 on 2024-01-26 12:25

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('scanpipe', '0051_rename_pipelines_data'),
]

operations = [
migrations.AddField(
model_name='project',
name='is_marked_for_deletion',
field=models.BooleanField(default=False),
),
]
12 changes: 12 additions & 0 deletions scanpipe/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@
from packageurl.contrib.django.models import PackageURLMixin
from packageurl.contrib.django.models import PackageURLQuerySetMixin
from rest_framework.authtoken.models import Token
from rq import Queue
from rq.command import send_stop_job_command
from rq.exceptions import NoSuchJobError
from rq.job import Job
Expand Down Expand Up @@ -534,6 +535,7 @@ class Project(UUIDPKModel, ExtraDataFieldMixin, UpdateMixin, models.Model):
labels = TaggableManager(through=UUIDTaggedItem)

objects = ProjectQuerySet.as_manager()
is_marked_for_deletion = models.BooleanField(default=False)
jayanth-kumar-morem marked this conversation as resolved.
Show resolved Hide resolved

class Meta:
ordering = ["-created_date"]
Expand Down Expand Up @@ -633,6 +635,16 @@ def delete(self, *args, **kwargs):

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

def mark_for_deletion(self):
self.is_marked_for_deletion = True
self.save()
jayanth-kumar-morem marked this conversation as resolved.
Show resolved Hide resolved

def delete_in_background(self):
# Mark the project for deletion and enqueue background deletion task
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A docstring would be better than a comment.

self.mark_for_deletion()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can put self.update(is_marked_for_deletion=True) directly here, no need for the mark_for_deletion method yet since it's a one-liner that is only used in one place.

q = Queue("default", connection=redis.Redis())
job = q.enqueue(tasks.background_delete_task, self)
jayanth-kumar-morem marked this conversation as resolved.
Show resolved Hide resolved

def reset(self, keep_input=True):
"""
Reset the project by deleting all related database objects and all work
Expand Down
18 changes: 18 additions & 0 deletions scanpipe/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@

from django.apps import apps

from django_rq import job

logger = logging.getLogger(__name__)


Expand Down Expand Up @@ -76,3 +78,19 @@ def execute_pipeline_task(run_pk):
project.clear_tmp_directory()
if next_run := project.get_next_run():
next_run.start()


@job
def background_delete_task(project):
# Check if the project is still marked for deletion
if not project.is_marked_for_deletion:
return

# Perform the deletion process
jayanth-kumar-morem marked this conversation as resolved.
Show resolved Hide resolved
try:
project.delete()
except Exception as e:
# Handle errors and update project errors or display a banner
jayanth-kumar-morem marked this conversation as resolved.
Show resolved Hide resolved
project.is_marked_for_deletion = False
project.save()
jayanth-kumar-morem marked this conversation as resolved.
Show resolved Hide resolved
project.add_error(description=f"Deletion failed: {str(e)}")
6 changes: 2 additions & 4 deletions scanpipe/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,11 +183,9 @@ def test_scanpipe_views_project_actions_view(self):
}
response = self.client.post(url, data=data, follow=True)
self.assertRedirects(response, reverse("project_list"))
expected = '<div class="message-body">1 projects have been delete.</div>'
expected = '<div class="message-body">1 project is being deleted in the background.</div>'
self.assertContains(response, expected, html=True)
expected = (
f'<div class="message-body">Project {random_uuid} does not exist.</div>'
)
expected = f"1 project is being deleted in the background."
self.assertContains(response, expected, html=True)

def test_scanpipe_views_project_details_is_archived(self):
Expand Down
7 changes: 6 additions & 1 deletion scanpipe/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -1076,7 +1076,10 @@ def perform_action(self, action, project_uuid, action_kwargs=None):

try:
project = Project.objects.get(pk=project_uuid)
getattr(project, action)(**action_kwargs)
if action == "delete":
project.delete_in_background()
else:
getattr(project, action)(**action_kwargs)
return True
except Project.DoesNotExist:
messages.error(self.request, f"Project {project_uuid} does not exist.")
Expand All @@ -1086,6 +1089,8 @@ def perform_action(self, action, project_uuid, action_kwargs=None):
raise Http404

def get_success_message(self, action, count):
if action == "delete":
return f"{count} project{'s' if count != 1 else ''} {'is' if count == 1 else 'are'} being deleted in the background."
jayanth-kumar-morem marked this conversation as resolved.
Show resolved Hide resolved
return f"{count} projects have been {action}."


Expand Down
Loading