Skip to content

Commit

Permalink
Refine the RunQuerySet methods and the AbstractTaskFieldsModel delete()
Browse files Browse the repository at this point in the history
#176

Signed-off-by: Thomas Druez <tdruez@nexb.com>
  • Loading branch information
tdruez committed Oct 8, 2021
1 parent 88acaef commit 38eed01
Showing 1 changed file with 38 additions and 6 deletions.
44 changes: 38 additions & 6 deletions scanpipe/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
from django.utils.translation import gettext_lazy as _

import django_rq
import redis
from packageurl import normalize_qualifiers
from packageurl.contrib.django.models import PackageURLQuerySetMixin
from rq.command import send_stop_job_command
Expand Down Expand Up @@ -116,6 +117,24 @@ class AbstractTaskFieldsModel(models.Model):
class Meta:
abstract = True

def delete(self, *args, **kwargs):
"""
Before deletion of the Run instance, try to stop the task if currently running
or to remove it from the queue if currently queued.
Note that projects with queued or running pipeline runs cannot be deleted.
See the `_raise_if_run_in_progress` method.
The following if statements should not be triggered unless the `.delete()`
method is directly call from a instance of this class.
"""
with suppress(redis.exceptions.ConnectionError, AttributeError):
if self.status == self.Status.RUNNING:
self.stop_task()
elif self.status == self.Status.QUEUED:
self.delete_task(delete_self=False)

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

@staticmethod
def get_job(job_id):
with suppress(NoSuchJobError):
Expand Down Expand Up @@ -304,15 +323,16 @@ def stop_task(self):
)
self.set_task_stopped()

def delete_task(self):
def delete_task(self, delete_self=True):
"""
Deletes a "not started" or "queued" task.
"""
if settings.SCANCODEIO_ASYNC and self.task_id:
# Cancels the job and deletes the job hash from Redis.
self.job.delete()

self.delete()
if delete_self:
self.delete()


class ExtraDataFieldMixin(models.Model):
Expand Down Expand Up @@ -911,19 +931,19 @@ def not_started(self):
"""
Not in the execution queue, no `task_id` assigned.
"""
return self.filter(task_start_date__isnull=True, task_id__isnull=True)
return self.no_exitcode().no_start_date().filter(task_id__isnull=True)

def queued(self):
"""
In the execution queue with a `task_id` assigned but not running yet.
"""
return self.filter(task_start_date__isnull=True, task_id__isnull=False)
return self.no_exitcode().no_start_date().filter(task_id__isnull=False)

def running(self):
"""
Running the pipeline execution.
"""
return self.has_start_date().filter(task_end_date__isnull=True)
return self.no_exitcode().has_start_date().filter(task_end_date__isnull=True)

def executed(self):
"""
Expand All @@ -945,10 +965,22 @@ def failed(self):

def has_start_date(self):
"""
Run has a `start_date` set. It can be running or executed.
Run has a `task_start_date` set. It can be running or executed.
"""
return self.filter(task_start_date__isnull=False)

def no_start_date(self):
"""
Run has no `task_start_date` set.
"""
return self.filter(task_start_date__isnull=True)

def no_exitcode(self):
"""
Run has no `task_exitcode` set.
"""
return self.filter(task_exitcode__isnull=True)

def queued_or_running(self):
"""
Run is queued or currently running.
Expand Down

0 comments on commit 38eed01

Please sign in to comment.