'Set metadata externally. Used by the Pulsar job runner where this
shouldn\'t be attached to command line to execute.'

def _handle_metadata_externally(self, job_wrapper, resolve_requirements=False):

    if ((job_wrapper.get_state() not in [model.Job.states.ERROR, model.Job.states.DELETED]) and job_wrapper.output_paths):
        lib_adjust = (GALAXY_LIB_ADJUST_TEMPLATE % job_wrapper.galaxy_lib_dir)
        venv = (GALAXY_VENV_TEMPLATE % job_wrapper.galaxy_virtual_env)
        external_metadata_script = job_wrapper.setup_external_metadata(output_fnames=job_wrapper.get_output_fnames(), set_extension=True, tmp_dir=job_wrapper.working_directory, kwds={'overwrite': False})
        external_metadata_script = ('%s    %s    %s' % (lib_adjust, venv, external_metadata_script))
        if resolve_requirements:
            dependency_shell_commands = self.app.datatypes_registry.set_external_metadata_tool.build_dependency_shell_commands(job_directory=job_wrapper.working_directory)
            if dependency_shell_commands:
                if isinstance(dependency_shell_commands, list):
                    dependency_shell_commands = '&&'.join(dependency_shell_commands)
                external_metadata_script = ('%s&&%s' % (dependency_shell_commands, external_metadata_script))
        log.debug(('executing    external    set_meta    script    for    job    %d:    %s' % (job_wrapper.job_id, external_metadata_script)))
        external_metadata_proc = subprocess.Popen(args=external_metadata_script, shell=True, cwd=job_wrapper.working_directory, env=os.environ, preexec_fn=os.setpgrp)
        job_wrapper.external_output_metadata.set_job_runner_external_pid(external_metadata_proc.pid, self.sa_session)
        external_metadata_proc.wait()
        log.debug(('execution    of    external    set_meta    for    job    %d    finished' % job_wrapper.job_id))
