"""
Submit the pipeline job as soon as the file download is complete.
"""
import sys
import time

from datetime import datetime
from django.core.management.base import BaseCommand

from chore import JobSubmissionError

from apps.predict.models import (
    PredictStrain, get_timeout, STATUS_WAIT, STATUS_START, STATUS_ERROR
)

def bitset(*args):
    """Turn a set of booleans into a bit array and then into an integer"""
    return int(''.join(reversed([str(int(bool(arg))) for arg in args])), 2)

def log(msg, *args, **kwargs):
    """Write consistantly to output"""
    kwargs['dt'] = datetime.now().isoformat()
    msg = "{dt}: " + msg
    sys.stderr.write(msg.format(*args, **kwargs) + "\n")

class Command(BaseCommand):
    """Schedule each of the pipeline tasks with the shell or LSF"""
    help = __doc__

    @staticmethod
    def submit_strain_pipeline(strain):
        """Submit a strain pipleine to the job queue"""
        status = strain.files_status
        if status in (STATUS_WAIT, STATUS_START):
            # Waiting or busy doing file upload
            return False
        if status is STATUS_ERROR:
            raise IOError("Download Error")

        return strain.run()

    def handle(self, **_):
        """Called from the command line"""
        # Limit all interactions to a timeout (usually a few weeks)
        qset = PredictStrain.objects.filter(dataset__created__gt=get_timeout())

        for strain in qset.filter(piperun__isnull=True):
            try:
                self.submit_strain_pipeline(strain)
                log("RUN: {} ({})", strain, strain.pipeline)
                time.sleep(0.25)
            except IOError as err:
                raise
                log("ERR: {} (Bad Download) {}", strain, err)
            except Exception as err: # pylint: disable=broad-except
                log("ERR: {} ({}): {}", strain, strain.pipeline, err)

@@ -1161,16 +1161,33 @@
 rror%22)%0A%0A
+        try:%0A    
         
@@ -1205,16 +1205,97 @@
 in.run()
+%0A        except JobSubmissionError:%0A            raise ValueError(%22Can't run job%22)
 %0A%0A    de
@@ -1766,30 +1766,8 @@
 rr:%0A
-                raise%0A
     

Add chore job submit protection