Skip to content

Commit

Permalink
Create command to requeue outstanding ScannableURIs #539
Browse files Browse the repository at this point in the history
Signed-off-by: Jono Yang <[email protected]>
  • Loading branch information
JonoYang committed Oct 15, 2024
1 parent b711d2f commit 62bce7b
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 0 deletions.
40 changes: 40 additions & 0 deletions minecode/management/commands/requeue_outstanding_scannableuris.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#
# Copyright (c) nexB Inc. and others. All rights reserved.
# purldb is a trademark of nexB Inc.
# SPDX-License-Identifier: Apache-2.0
# See http://www.apache.org/licenses/LICENSE-2.0 for the license text.
# See https://github.com/aboutcode-org/purldb for support or download.
# See https://aboutcode.org for more information about nexB OSS projects.
#

import logging
import sys

from minecode.management.commands import VerboseCommand
from minecode.models import ScannableURI


logger = logging.getLogger(__name__)
logging.basicConfig(stream=sys.stdout)
logger.setLevel(logging.INFO)

TRACE = False
if TRACE:
logger.setLevel(logging.DEBUG)


class Command(VerboseCommand):
help = "Check for outstanding ScannableURIs and requeue them."

def handle(self, *args, **options):
"""
Check for outstanding ScannableURIs and requeue them.
"""

outstanding_scannable_uris = ScannableURI.objects.get_outstanding_scannable_resource_uris()
if outstanding_scannable_uris.exists():
logger.info(f"Resetting {outstanding_scannable_uris.count()} ScannableURIs")
for scannable_uri in outstanding_scannable_uris:
if TRACE:
logger.debug(f"Resetting ScannableURI for: {scannable_uri}")
scannable_uri.reset()
20 changes: 20 additions & 0 deletions minecode/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
# See https://aboutcode.org for more information about nexB OSS projects.
#

import datetime
import logging
import sys
import uuid
Expand Down Expand Up @@ -653,6 +654,14 @@ def _recent(self, scan_status, extra_value=None, most_recent=10):
recent[extra_value] = getattr(scauri, extra_value)
yield recent

def get_outstanding_scannable_uris(self):
"""
Return ScannableURIs that are older than a day from when this
queryset is called and that are being worked on.
"""
time_delta = datetime.datetime.now() - datetime.timedelta(days=1)
return self.filter(scan_date__lte=time_delta, wip_date__isnull=False)


class ScannableURI(BaseURI):
"""
Expand Down Expand Up @@ -818,6 +827,17 @@ def process_scan_results(
)
return job

def reset(self):
"""
Reset fields such that a ScannableURI can be sent off for scanning again
"""
self.scan_status = ScannableURI.SCAN_NEW
self.scan_error = None
self.index_error = None
self.scan_date = None
self.wip_date = None
self.save()


# TODO: Use the QuerySet.as_manager() for more flexibility and chaining.
class PriorityResourceURIManager(models.Manager):
Expand Down

0 comments on commit 62bce7b

Please sign in to comment.