<s> from __future__ import absolute_import <EOL> import logging <EOL> import os <EOL> import six <EOL> from taskflow import exceptions <EOL> from taskflow . listeners import base <EOL> from taskflow import states <EOL> LOG = logging . getLogger ( __name__ ) <EOL> class CheckingClaimListener ( base . Listener ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , engine , job , board , owner , on_job_loss = None ) : <EOL> super ( CheckingClaimListener , self ) . __init__ ( engine ) <EOL> self . _job = job <EOL> self . _board = board <EOL> self . _owner = owner <EOL> if on_job_loss is None : <EOL> self . _on_job_loss = self . _suspend_engine_on_loss <EOL> else : <EOL> if not six . callable ( on_job_loss ) : <EOL> raise ValueError ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> self . _on_job_loss = on_job_loss <EOL> def _suspend_engine_on_loss ( self , engine , state , details ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> engine . suspend ( ) <EOL> except exceptions . TaskFlowException as e : <EOL> LOG . warn ( "<STR_LIT>" <EOL> "<STR_LIT>" , engine , self . _owner , os . linesep , <EOL> e . pformat ( ) ) <EOL> def _flow_receiver ( self , state , details ) : <EOL> self . _claim_checker ( state , details ) <EOL> def _task_receiver ( self , state , details ) : <EOL> self . _claim_checker ( state , details ) <EOL> def _has_been_lost ( self ) : <EOL> try : <EOL> job_state = self . _job . state <EOL> job_owner = self . _board . find_owner ( self . _job ) <EOL> except ( exceptions . NotFound , exceptions . JobFailure ) : <EOL> return True <EOL> else : <EOL> if job_state == states . UNCLAIMED or self . _owner != job_owner : <EOL> return True <EOL> else : <EOL> return False <EOL> def _claim_checker ( self , state , details ) : <EOL> if not self . _has_been_lost ( ) : <EOL> LOG . debug ( "<STR_LIT>" , <EOL> self . _job , self . _owner ) <EOL> else : <EOL> LOG . warn ( "<STR_LIT>" , <EOL> self . _job , self . _owner ) <EOL> self . _on_job_loss ( self . _engine , state , details ) </s>
