'Called by the monitor thread to look at each watched job and deal
with state changes.'

def check_watched_items(self):

    new_watched = []
    job_states = self.__get_job_states()
    for ajs in self.watched:
        external_job_id = ajs.job_id
        id_tag = ajs.job_wrapper.get_id_tag()
        old_state = ajs.old_state
        state = job_states.get(external_job_id, None)
        if (state is None):
            if (ajs.job_wrapper.get_state() == model.Job.states.DELETED):
                continue
            external_metadata = (not asbool(ajs.job_wrapper.job_destination.params.get('embed_metadata_in_job', DEFAULT_EMBED_METADATA_IN_JOB)))
            if external_metadata:
                self._handle_metadata_externally(ajs.job_wrapper, resolve_requirements=True)
            log.debug(('(%s/%s)    job    not    found    in    batch    state    check' % (id_tag, external_job_id)))
            (shell_params, job_params) = self.parse_destination_params(ajs.job_destination.params)
            (shell, job_interface) = self.get_cli_plugins(shell_params, job_params)
            cmd_out = shell.execute(job_interface.get_single_status(external_job_id))
            state = job_interface.parse_single_status(cmd_out.stdout, external_job_id)
            if (not (state == model.Job.states.OK)):
                log.warning(('(%s/%s)    job    not    found    in    batch    state    check,    but    found    in    individual    state    check' % (id_tag, external_job_id)))
        if (state != old_state):
            log.debug(('(%s/%s)    state    change:    from    %s    to    %s' % (id_tag, external_job_id, old_state, state)))
            if (not (state == model.Job.states.OK)):
                ajs.job_wrapper.change_state(state)
        if ((state == model.Job.states.RUNNING) and (not ajs.running)):
            ajs.running = True
        ajs.old_state = state
        if (state == model.Job.states.OK):
            log.debug(('(%s/%s)    job    execution    finished,    running    job    wrapper    finish    method' % (id_tag, external_job_id)))
            self.work_queue.put((self.finish_job, ajs))
        else:
            new_watched.append(ajs)
    self.watched = new_watched
