'Create job script and submit it to the DRM'

def queue_job(self, job_wrapper):

    external_runjob_script = job_wrapper.get_destination_configuration('drmaa_external_runjob_script', None)
    include_metadata = asbool(job_wrapper.job_destination.params.get('embed_metadata_in_job', True))
    if (not self.prepare_job(job_wrapper, include_metadata=include_metadata)):
        return
    job_destination = job_wrapper.job_destination
    galaxy_id_tag = job_wrapper.get_id_tag()
    job_name = self._job_name(job_wrapper)
    ajs = AsynchronousJobState(files_dir=job_wrapper.working_directory, job_wrapper=job_wrapper, job_name=job_name)
    jt = dict(remoteCommand=ajs.job_file, jobName=ajs.job_name, workingDirectory=job_wrapper.working_directory, outputPath=(':%s' % ajs.output_file), errorPath=(':%s' % ajs.error_file))
    native_spec = job_destination.params.get('nativeSpecification', None)
    if (native_spec is not None):
        jt['nativeSpecification'] = native_spec
    script = self.get_job_file(job_wrapper, exit_code_path=ajs.exit_code_file)
    try:
        self.write_executable_script(ajs.job_file, script)
    except:
        job_wrapper.fail('failure    preparing    job    script', exception=True)
        log.exception(('(%s)    failure    writing    job    script' % galaxy_id_tag))
        return
    if (job_wrapper.get_state() == model.Job.states.DELETED):
        log.debug(('(%s)    Job    deleted    by    user    before    it    entered    the    queue' % galaxy_id_tag))
        if (job_wrapper.cleanup_job in ('always', 'onsuccess')):
            job_wrapper.cleanup()
        return
    log.debug('(%s)    submitting    file    %s', galaxy_id_tag, ajs.job_file)
    if native_spec:
        log.debug('(%s)    native    specification    is:    %s', galaxy_id_tag, native_spec)
    if (external_runjob_script is None):
        trynum = 0
        external_job_id = None
        fail_msg = None
        while ((external_job_id is None) and (trynum < 5)):
            try:
                external_job_id = self.ds.run_job(**jt)
                break
            except (drmaa.InternalException, drmaa.DeniedByDrmException) as e:
                trynum += 1
                log.warning('(%s)    drmaa.Session.runJob()    failed,    will    retry:    %s', galaxy_id_tag, e)
                fail_msg = 'Unable    to    run    this    job    due    to    a    cluster    error,    please    retry    it    later'
                time.sleep(5)
            except:
                log.exception('(%s)    drmaa.Session.runJob()    failed    unconditionally', galaxy_id_tag)
                trynum = 5
        else:
            log.error(('(%s)    All    attempts    to    submit    job    failed' % galaxy_id_tag))
            if (not fail_msg):
                fail_msg = DEFAULT_JOB_PUT_FAILURE_MESSAGE
            job_wrapper.fail(fail_msg)
            return
    else:
        job_wrapper.change_ownership_for_run()
        allow_guests = asbool(job_wrapper.job_destination.params.get('allow_guests', False))
        pwent = job_wrapper.user_system_pwent
        if (pwent is None):
            if (not allow_guests):
                fail_msg = ('User    %s    is    not    mapped    to    any    real    user,    and    not    permitted    to    start    jobs.' % job_wrapper.user)
                job_wrapper.fail(fail_msg)
                return
            pwent = job_wrapper.galaxy_system_pwent
        log.debug(('(%s)    submitting    with    credentials:    %s    [uid:    %s]' % (galaxy_id_tag, pwent[0], pwent[2])))
        filename = self.store_jobtemplate(job_wrapper, jt)
        self.userid = pwent[2]
        external_job_id = self.external_runjob(external_runjob_script, filename, pwent[2]).strip()
    log.info(('(%s)    queued    as    %s' % (galaxy_id_tag, external_job_id)))
    job_wrapper.set_job_destination(job_destination, external_job_id)
    ajs.job_id = external_job_id
    ajs.old_state = 'new'
    ajs.job_destination = job_destination
    self.monitor_queue.put(ajs)
