'Called to indicate that the associated command has been run. Updates
the output datasets based on stderr and stdout from the command, and
the contents of the output files.'

def finish(self, stdout, stderr, tool_exit_code=None):

    log.debug(('task    %s    for    job    %d    ended;    exit    code:    %d' % (self.task_id, self.job_id, (tool_exit_code if (tool_exit_code is not None) else (-256)))))
    self.sa_session.expunge_all()
    task = self.get_task()
    if (task.state == task.states.DELETED):
        delete_files = (self.cleanup_job in ('always', 'onsuccess'))
        self.cleanup(delete_files=delete_files)
        return
    elif (task.state == task.states.ERROR):
        self.fail(task.info)
        return
    if self.check_tool_output(stdout, stderr, tool_exit_code, task):
        task.state = task.states.OK
    else:
        task.state = task.states.ERROR
    task.set_streams(stdout, stderr)
    self._collect_metrics(task)
    task.exit_code = tool_exit_code
    task.command_line = self.command_line
    self.sa_session.flush()
