'Called repeatedly by `monitor` to stop jobs.'

def monitor_step(self):

    jobs_to_check = []
    if self.app.config.track_jobs_in_database:
        self.sa_session.expunge_all()
        newly_deleted_jobs = self.sa_session.query(model.Job).enable_eagerloads(False).filter(((model.Job.state == model.Job.states.DELETED_NEW) & (model.Job.handler == self.app.config.server_name))).all()
        for job in newly_deleted_jobs:
            jobs_to_check.append((job, job.stderr))
    try:
        while 1:
            message = self.queue.get_nowait()
            if (message is self.STOP_SIGNAL):
                return
            (job_id, error_msg) = message
            jobs_to_check.append((self.sa_session.query(model.Job).get(job_id), error_msg))
    except Empty:
        pass
    for (job, error_msg) in jobs_to_check:
        if ((job.state not in (job.states.DELETED_NEW, job.states.DELETED)) and job.finished):
            log.debug('Job    %s    already    finished,    not    deleting    or    stopping', job.id)
            continue
        final_state = job.states.DELETED
        if (error_msg is not None):
            final_state = job.states.ERROR
            job.info = error_msg
        job.set_final_state(final_state)
        self.sa_session.add(job)
        self.sa_session.flush()
        if (job.job_runner_name is not None):
            self.dispatcher.stop(job)
