'Checks the state of a job already submitted on k8s. Job state is a AsynchronousJobState'

def check_watched_item(self, job_state):

    jobs = Job.objects(self._pykube_api).filter(selector=('app=' + job_state.job_id))
    if (len(jobs.response['items']) == 1):
        job = Job(self._pykube_api, jobs.response['items'][0])
        job_destination = job_state.job_wrapper.job_destination
        succeeded = 0
        active = 0
        failed = 0
        max_pod_retrials = 1
        if ('k8s_pod_retrials' in self.runner_params):
            max_pod_retrials = int(self.runner_params['k8s_pod_retrials'])
        if ('max_pod_retrials' in job_destination.params):
            max_pod_retrials = int(job_destination.params['max_pod_retrials'])
        if ('succeeded' in job.obj['status']):
            succeeded = job.obj['status']['succeeded']
        if ('active' in job.obj['status']):
            active = job.obj['status']['active']
        if ('failed' in job.obj['status']):
            failed = job.obj['status']['failed']
        if (succeeded > 0):
            self.__produce_log_file(job_state)
            error_file = open(job_state.error_file, 'w')
            error_file.write('')
            error_file.close()
            job_state.running = False
            self.mark_as_finished(job_state)
            return None
        elif ((active > 0) and (failed <= max_pod_retrials)):
            job_state.running = True
            return job_state
        elif (failed > max_pod_retrials):
            self.__produce_log_file(job_state)
            error_file = open(job_state.error_file, 'w')
            error_file.write('Exceeded    max    number    of    Kubernetes    pod    retrials    allowed    for    job\n')
            error_file.close()
            job_state.running = False
            job_state.fail_message = 'More    pods    failed    than    allowed.    See    stdout    for    pods    details.'
            self.mark_as_failed(job_state)
            job.scale(replicas=0)
            return None
        log.debug('Reaching    unexpected    point    for    Kubernetes    job,    where    it    is    not    classified    as    succ.,    active    nor    failed.')
        return job_state
    elif (len(jobs.response['items']) == 0):
        log.error(('No    Jobs    are    available    under    expected    selector    app=' + job_state.job_id))
        error_file = open(job_state.error_file, 'w')
        error_file.write((('No    Kubernetes    Jobs    are    available    under    expected    selector    app=' + job_state.job_id) + '\n'))
        error_file.close()
        self.mark_as_failed(job_state)
        return job_state
    else:
        log.error(('There    is    more    than    one    Kubernetes    Job    associated    to    job    id    ' + job_state.job_id))
        self.__produce_log_file(job_state)
        error_file = open(job_state.error_file, 'w')
        error_file.write((('There    is    more    than    one    Kubernetes    Job    associated    to    job    id    ' + job_state.job_id) + '\n'))
        error_file.close()
        self.mark_as_failed(job_state)
        return job_state
