<s> import glob <EOL> import logging <EOL> import os <EOL> import time <EOL> from datetime import datetime <EOL> from inferno . lib . datefile import Datefile <EOL> log = logging . getLogger ( __name__ ) <EOL> def processes ( pid_dir ) : <EOL> files = glob . glob ( os . path . join ( pid_dir , '<STR_LIT>' ) ) <EOL> result = [ ] <EOL> for pid_file in files : <EOL> with open ( pid_file ) as f : <EOL> pid = f . read ( ) . strip ( ) <EOL> result . append ( { <EOL> '<STR_LIT>' : pid , <EOL> '<STR_LIT>' : time . ctime ( os . path . getctime ( pid_file ) ) , <EOL> '<STR_LIT:name>' : os . path . basename ( pid_file ) . replace ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> } ) <EOL> return result <EOL> def should_run ( pid_dir , rule ) : <EOL> last_run = Datefile ( pid_dir , "<STR_LIT>" % rule . name ) <EOL> if not os . path . exists ( get_pid_path ( pid_dir , rule ) ) : <EOL> if last_run . is_older_than ( rule . time_delta ) : <EOL> return True <EOL> elif rule . retry : <EOL> if os . path . exists ( os . path . join ( pid_dir , "<STR_LIT>" % rule . name ) ) : <EOL> next_retry = Datefile ( pid_dir , "<STR_LIT>" % rule . name ) <EOL> now = datetime . utcnow ( ) <EOL> if now > datetime . strptime ( str ( next_retry ) , '<STR_LIT>' ) : <EOL> return True <EOL> log . debug ( '<STR_LIT>' , rule . name , last_run ) <EOL> return False <EOL> def create_last_complete ( pid_dir , rule ) : <EOL> Datefile ( pid_dir , "<STR_LIT>" % rule . name , timestamp = datetime . utcnow ( ) ) <EOL> def create_last_run ( pid_dir , rule ) : <EOL> Datefile ( pid_dir , "<STR_LIT>" % rule . name , timestamp = datetime . utcnow ( ) ) <EOL> def create_failed ( pid_dir , rule ) : <EOL> Datefile ( pid_dir , "<STR_LIT>" % rule . name , timestamp = datetime . utcnow ( ) ) <EOL> next_retry = os . path . join ( pid_dir , "<STR_LIT>" % rule . name ) <EOL> if os . path . exists ( next_retry ) : <EOL> os . unlink ( next_retry ) <EOL> def create_next_retry ( pid_dir , rule ) : <EOL> from datetime import timedelta <EOL> Datefile ( pid_dir , "<STR_LIT>" % rule . name , <EOL> timestamp = datetime . utcnow ( ) + timedelta ( <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> , int ( rule . retry_delay ) , <NUM_LIT:0> ) ) <EOL> def increment_retry_count ( pid_dir , rule ) : <EOL> path = os . path . join ( pid_dir , "<STR_LIT>" % rule . name ) <EOL> if os . path . exists ( path ) : <EOL> with open ( path ) as r : <EOL> retry_count = int ( r . readlines ( <NUM_LIT:1> ) [ <NUM_LIT:0> ] ) + <NUM_LIT:1> <EOL> with open ( path , '<STR_LIT:w>' ) as w : <EOL> w . write ( str ( retry_count ) ) <EOL> else : <EOL> with open ( path , '<STR_LIT:w>' ) as w : <EOL> w . write ( '<STR_LIT:1>' ) <EOL> def get_retry_count ( pid_dir , rule ) : <EOL> path = os . path . join ( pid_dir , "<STR_LIT>" % rule . name ) <EOL> if os . path . exists ( path ) : <EOL> with open ( path ) as r : <EOL> return int ( r . readlines ( <NUM_LIT:1> ) [ <NUM_LIT:0> ] ) <EOL> else : <EOL> return <NUM_LIT:0> <EOL> def create_pid ( pid_dir , rule , pid ) : <EOL> path = get_pid_path ( pid_dir , rule ) <EOL> try : <EOL> with open ( path , '<STR_LIT:w>' ) as f : <EOL> f . write ( pid ) <EOL> return True <EOL> except Exception as ex : <EOL> raise ex <EOL> def clean_up ( pid_dir , rule ) : <EOL> retry_count = os . path . join ( pid_dir , "<STR_LIT>" % rule . name ) <EOL> next_retry = os . path . join ( pid_dir , "<STR_LIT>" % rule . name ) <EOL> failed = os . path . join ( pid_dir , "<STR_LIT>" % rule . name ) <EOL> if os . path . exists ( retry_count ) : <EOL> os . unlink ( retry_count ) <EOL> if os . path . exists ( next_retry ) : <EOL> os . unlink ( next_retry ) <EOL> if os . path . exists ( failed ) : <EOL> os . unlink ( failed ) <EOL> def remove_pid ( pid_dir , rule ) : <EOL> path = get_pid_path ( pid_dir , rule ) <EOL> os . unlink ( path ) <EOL> def get_pid_path ( pid_dir , rule ) : <EOL> return os . path . join ( pid_dir , '<STR_LIT>' % rule . name ) <EOL> def pid_dir ( settings ) : <EOL> path = settings [ '<STR_LIT>' ] <EOL> if not os . path . exists ( path ) : <EOL> os . mkdir ( path ) <EOL> return path </s>
