-
Notifications
You must be signed in to change notification settings - Fork 93
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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), | ||
), | ||
] |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# Generated by Django 5.0.2 on 2024-03-02 10:47 | ||
|
||
from django.db import migrations | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
("scanpipe", "0052_project_is_marked_for_deletion"), | ||
("scanpipe", "0053_restructure_pipelines_data"), | ||
] | ||
|
||
operations = [] |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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: | ||
|
@@ -532,6 +535,7 @@ 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() | ||
|
@@ -634,6 +638,14 @@ def delete(self, *args, **kwargs): | |
|
||
return super().delete(*args, **kwargs) | ||
|
||
def mark_for_deletion(self): | ||
self.update(is_marked_for_deletion=True) | ||
|
||
def delete_in_background(self): | ||
# Mark the project for deletion and enqueue background deletion task | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A docstring would be better than a comment. |
||
self.mark_for_deletion() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we can put |
||
django_rq.enqueue(tasks.background_delete_task, self) | ||
|
||
def reset(self, keep_input=True): | ||
""" | ||
Reset the project by deleting all related database objects and all work | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,6 +24,8 @@ | |
|
||
from django.apps import apps | ||
|
||
from django_rq import job | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
|
@@ -76,3 +78,17 @@ 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 | ||
|
||
try: | ||
project.delete() | ||
except Exception as e: | ||
info(f"Deletion failed: {str(e)}", project.pk) | ||
project.update(is_marked_for_deletion=True) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm wondering is this line is of any use, the |
||
project.add_error(description=f"Deletion failed: {str(e)}") |
There was a problem hiding this comment.
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?