'Checks all jobs that are in the \'new\', \'queued\' or \'running\' state in
the database and requeues or cleans up as necessary.  Only run as the
job handler starts.
In case the activation is enforced it will filter out the jobs of inactive users.'

def __check_jobs_at_startup(self):

    jobs_at_startup = []
    if self.track_jobs_in_database:
        in_list = (model.Job.states.QUEUED, model.Job.states.RUNNING)
    else:
        in_list = (model.Job.states.NEW, model.Job.states.QUEUED, model.Job.states.RUNNING)
    if self.app.config.user_activation_on:
        jobs_at_startup = self.sa_session.query(model.Job).enable_eagerloads(False).outerjoin(model.User).filter(((model.Job.state.in_(in_list) & (model.Job.handler == self.app.config.server_name)) & or_((model.Job.user_id == null()), (model.User.active == true())))).all()
    else:
        jobs_at_startup = self.sa_session.query(model.Job).enable_eagerloads(False).filter((model.Job.state.in_(in_list) & (model.Job.handler == self.app.config.server_name))).all()
    for job in jobs_at_startup:
        if (not self.app.toolbox.has_tool(job.tool_id, job.tool_version, exact=True)):
            log.warning(("(%s)    Tool    '%s'    removed    from    tool    config,    unable    to    recover    job" % (job.id, job.tool_id)))
            self.job_wrapper(job).fail('This    tool    was    disabled    before    the    job    completed.        Please    contact    your    Galaxy    administrator.')
        elif ((job.job_runner_name is not None) and (job.job_runner_external_id is None)):
            log.debug(('(%s)    Job    runner    assigned    but    no    external    ID    recorded,    adding    to    the    job    handler    queue' % job.id))
            job.job_runner_name = None
            if self.track_jobs_in_database:
                job.set_state(model.Job.states.NEW)
            else:
                self.queue.put((job.id, job.tool_id))
        elif ((job.job_runner_name is not None) and (job.job_runner_external_id is not None) and (job.destination_id is None)):
            job_wrapper = self.job_wrapper(job)
            job_destination = self.dispatcher.url_to_destination(job.job_runner_name)
            if (job_destination.id is None):
                job_destination.id = 'legacy_url'
            job_wrapper.set_job_destination(job_destination, job.job_runner_external_id)
            self.dispatcher.recover(job, job_wrapper)
            log.info(('(%s)    Converted    job    from    a    URL    to    a    destination    and    recovered' % job.id))
        elif (job.job_runner_name is None):
            log.debug(("(%s)    No    job    runner    assigned    and    job    still    in    '%s'    state,    adding    to    the    job    handler    queue" % (job.id, job.state)))
            if self.track_jobs_in_database:
                job.set_state(model.Job.states.NEW)
            else:
                self.queue.put((job.id, job.tool_id))
        else:
            job_wrapper = self.__recover_job_wrapper(job)
            self.dispatcher.recover(job, job_wrapper)
    if self.sa_session.dirty:
        self.sa_session.flush()
