<commit_before>import logging

from django.core.management.base import BaseCommand
from django.conf import settings
from django.utils import timezone

from queue.models import Submission
from queue.consumer import post_failure_to_lms
from queue.queue_producer import push_to_queue

log = logging.getLogger(__name__)


class Command(BaseCommand):
    args = "<queue_name>"
    help = "Process currently open, pulled submissions on queue <queue_name>, and requeue if external grader has not replied in settings.PULLED_SUBMISSION_TIMEOUT seconds" 

    def handle(self, *args, **options):
        log.info(' [*] Running requeue of pulled submissions...')

        queue_name = args[0]

        open_submissions = Submission.objects.filter(queue_name=queue_name, lms_ack=False) 
        open_submissions = open_submissions.exclude(pull_time=None)

        for open_submission in open_submissions:
            current_time = timezone.now()
            time_difference = (current_time - open_submission.pull_time).total_seconds()
            if time_difference > settings.PULLED_SUBMISSION_TIMEOUT:
                open_submission.num_failures += 1
                if open_submission.num_failures < settings.MAX_NUMBER_OF_FAILURES:
                    log.info(' [ ] Requeuing submission.id=%d which has been outstanding for %d seconds' % (open_submission.id, time_difference))
                    qitem = str(open_submission.id)
                    push_to_queue(queue_name, qitem)
                else:
                    log.info(' [ ] NOT requeueing submission.id=%d because num_failures=%d >= MAX_NUMBER_OF_FAILURES=%d' %\
                                (open_submission.id, open_submission.num_failures, settings.MAX_NUMBER_OF_FAILURES))
                open_submission.save()
                #post_failure_to_lms(open_submission.xqueue_header) # TODO: Requeue, not retire <commit_msg>Don't requeue items that's already been requeued<commit_after>import logging

from django.core.management.base import BaseCommand
from django.conf import settings
from django.utils import timezone

from queue.models import Submission
from queue.consumer import post_failure_to_lms
from queue.queue_producer import push_to_queue

log = logging.getLogger(__name__)


class Command(BaseCommand):
    args = "<queue_name>"
    help = "Process currently open, pulled submissions on queue <queue_name>, and requeue if external grader has not replied in settings.PULLED_SUBMISSION_TIMEOUT seconds" 

    def handle(self, *args, **options):
        log.info(' [*] Running requeue of pulled submissions...')

        queue_name = args[0]

        open_submissions = Submission.objects.filter(queue_name=queue_name, lms_ack=False) 
        open_submissions = open_submissions.exclude(pull_time=None)

        for open_submission in open_submissions:
            current_time = timezone.now()
            time_difference = (current_time - open_submission.pull_time).total_seconds()
            if time_difference > settings.PULLED_SUBMISSION_TIMEOUT:
                open_submission.num_failures += 1
                if open_submission.num_failures < settings.MAX_NUMBER_OF_FAILURES:
                    log.info(' [ ] Requeuing submission.id=%d which has been outstanding for %d seconds' % (open_submission.id, time_difference))
                    qitem = str(open_submission.id)
                    open_submission.pull_time = None
                    push_to_queue(queue_name, qitem)
                else:
                    log.info(' [ ] NOT requeueing submission.id=%d because num_failures=%d >= MAX_NUMBER_OF_FAILURES=%d' %\
                                (open_submission.id, open_submission.num_failures, settings.MAX_NUMBER_OF_FAILURES))
                open_submission.save()
                #post_failure_to_lms(open_submission.xqueue_header) # TODO: Requeue, not retire 
{'fraction_non_alphanumeric': 0.07767995857068877, 'fraction_numerical': 0.0010357327809425167, 'mean_word_length': 2.9865841073271415, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5625134', 'n_tokens_mistral': 1124, 'n_tokens_neox': 1073, 'n_words': 246}