'Get the output/error for a finished job, pass to `job_wrapper.finish`
and cleanup all the job\'s temporary files.'

def finish_job(self, job_state):

    galaxy_id_tag = job_state.job_wrapper.get_id_tag()
    external_job_id = job_state.job_id
    job_state.job_wrapper.reclaim_ownership()
    which_try = 0
    while (which_try < (self.app.config.retry_job_output_collection + 1)):
        try:
            stdout = shrink_stream_by_size(open(job_state.output_file, 'r'), DATABASE_MAX_STRING_SIZE, join_by='\n..\n', left_larger=True, beginning_on_size_error=True)
            stderr = shrink_stream_by_size(open(job_state.error_file, 'r'), DATABASE_MAX_STRING_SIZE, join_by='\n..\n', left_larger=True, beginning_on_size_error=True)
            which_try = (self.app.config.retry_job_output_collection + 1)
        except Exception as e:
            if (which_try == self.app.config.retry_job_output_collection):
                stdout = ''
                stderr = 'Job    output    not    returned    from    cluster'
                log.error(('(%s/%s)    %s:    %s' % (galaxy_id_tag, external_job_id, stderr, str(e))))
            else:
                time.sleep(1)
            which_try += 1
    try:
        exit_code_str = open(job_state.exit_code_file, 'r').read(32)
    except:
        exit_code_str = '0'
    try:
        exit_code = int(exit_code_str)
    except:
        log.warning(("(%s/%s)    Exit    code    '%s'    invalid.    Using    0." % (galaxy_id_tag, external_job_id, exit_code_str)))
        exit_code = 0
    cleanup_job = job_state.job_wrapper.cleanup_job
    if ((cleanup_job == 'always') or ((not stderr) and (cleanup_job == 'onsuccess'))):
        job_state.cleanup()
    try:
        job_state.job_wrapper.finish(stdout, stderr, exit_code)
    except:
        log.exception(('(%s/%s)    Job    wrapper    finish    method    failed' % (galaxy_id_tag, external_job_id)))
        job_state.job_wrapper.fail('Unable    to    finish    job', exception=True)
