<s> import logging <EOL> logger = logging . getLogger ( __name__ ) <EOL> from boto . exception import SDBResponseError <EOL> from nymms . state . State import StateManager <EOL> from nymms . schemas import StateRecord <EOL> from nymms . exceptions import OutOfDateState <EOL> from nymms . providers . sdb import SimpleDBBackend <EOL> class SDBStateManager ( StateManager ) : <EOL> def __init__ ( self , region , domain_name , schema_class = StateRecord ) : <EOL> self . region = region <EOL> self . domain_name = domain_name <EOL> super ( SDBStateManager , self ) . __init__ ( schema_class ) <EOL> @ property <EOL> def conn ( self ) : <EOL> return self . backend . conn <EOL> @ property <EOL> def domain ( self ) : <EOL> return self . backend . domain <EOL> def get_backend ( self ) : <EOL> return SimpleDBBackend ( self . region , self . domain_name ) <EOL> def save_state ( self , task_id , result , previous ) : <EOL> new_state = self . build_new_state ( task_id , result , previous ) <EOL> expected_value = [ '<STR_LIT>' , False ] <EOL> if previous : <EOL> expected_value = [ '<STR_LIT>' , previous . last_update . isoformat ( ) ] <EOL> if previous . last_update > new_state . last_update : <EOL> logger . warning ( task_id + "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> logger . warning ( task_id + "<STR_LIT>" , <EOL> previous . to_primitive ( ) ) <EOL> logger . warning ( task_id + "<STR_LIT>" , <EOL> new_state . to_primitive ( ) ) <EOL> raise OutOfDateState ( new_state , previous ) <EOL> logger . debug ( task_id + "<STR_LIT>" , <EOL> new_state . to_primitive ( ) ) <EOL> try : <EOL> self . domain . put_attributes ( task_id , new_state . to_primitive ( ) , <EOL> replace = True , <EOL> expected_value = expected_value ) <EOL> except SDBResponseError as e : <EOL> if e . error_code == '<STR_LIT>' : <EOL> logger . warning ( '<STR_LIT>' , <EOL> task_id ) <EOL> return <EOL> raise <EOL> def get_old_states ( self ) : <EOL> query = ( "<STR_LIT>" <EOL> "<STR_LIT>" % ( self . backend . domain_name , <EOL> self . schema_class . CURRENT_VERSION ) ) <EOL> return self . domain . select ( query , consistent_read = True ) </s>
