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
Prev Previous commit
Next Next commit
feat: Address review comments
- Remove comments
- Use django rq
- Use update instead of save

Signed-off-by: Jayanth Kumar <jknani111@gmail.com>
  • Loading branch information
jayanth-kumar-morem committed Feb 26, 2024
commit bb312f786022d1b8060e4fbdfec40a27467c04ec
4 changes: 1 addition & 3 deletions scanpipe/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -349,9 +349,7 @@ 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, is_marked_for_deletion=False
)
self.queryset = self.queryset.active()

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?

Expand Down
11 changes: 6 additions & 5 deletions scanpipe/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -489,6 +489,9 @@ def with_counts(self, *fields):

return self.annotate(**annotations)

def active(self):
return self.filter(is_archived=False, is_marked_for_deletion=False)


class UUIDTaggedItem(GenericUUIDTaggedItemBase, TaggedItemBase):
class Meta:
Expand Down Expand Up @@ -532,10 +535,10 @@ class Project(UUIDPKModel, ExtraDataFieldMixin, UpdateMixin, models.Model):
)
notes = models.TextField(blank=True)
settings = models.JSONField(default=dict, blank=True)
is_marked_for_deletion = models.BooleanField(default=False)
labels = TaggableManager(through=UUIDTaggedItem)

objects = ProjectQuerySet.as_manager()
is_marked_for_deletion = models.BooleanField(default=False)

class Meta:
ordering = ["-created_date"]
Expand Down Expand Up @@ -636,14 +639,12 @@ def delete(self, *args, **kwargs):
return super().delete(*args, **kwargs)

def mark_for_deletion(self):
self.is_marked_for_deletion = True
self.save()
self.update(is_marked_for_deletion=True)

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)
django_rq.enqueue(tasks.background_delete_task, self)

def reset(self, keep_input=True):
"""
Expand Down
6 changes: 2 additions & 4 deletions scanpipe/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,9 @@ def background_delete_task(project):
if not project.is_marked_for_deletion:
return

# Perform the deletion process
try:
project.delete()
except Exception as e:
# Handle errors and update project errors or display a banner
project.is_marked_for_deletion = False
project.save()
info(f"Deletion failed: {str(e)}", project.pk)
project.update(is_marked_for_deletion=True)
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm wondering is this line is of any use, the is_marked_for_deletion must be already True to reach this line, no?

project.add_error(description=f"Deletion failed: {str(e)}")
4 changes: 2 additions & 2 deletions scanpipe/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,9 +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 project is being deleted in the background.</div>'
expected = '<div class="message-body">1 projects have been delete.</div>'
self.assertContains(response, expected, html=True)
expected = f"1 project is being deleted in the background."
expected = f"1 projects have been delete."
self.assertContains(response, expected, html=True)

def test_scanpipe_views_project_details_is_archived(self):
Expand Down
2 changes: 0 additions & 2 deletions scanpipe/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -1089,8 +1089,6 @@ 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."
return f"{count} projects have been {action}."


Expand Down
Loading