<s> """<STR_LIT>""" <EOL> import atexit <EOL> import errno <EOL> import logging <EOL> import os <EOL> import signal <EOL> import sys <EOL> import time <EOL> from utils . process import pid_exists <EOL> log = logging . getLogger ( __name__ ) <EOL> class AgentSupervisor ( object ) : <EOL> '''<STR_LIT>''' <EOL> RESTART_EXIT_STATUS = <NUM_LIT:5> <EOL> @ classmethod <EOL> def start ( cls , parent_func , child_func = None ) : <EOL> '''<STR_LIT>''' <EOL> exit_code = cls . RESTART_EXIT_STATUS <EOL> signal . signal ( signal . SIGTERM , cls . _handle_sigterm ) <EOL> cls . need_stop = False <EOL> while True : <EOL> try : <EOL> if hasattr ( cls , '<STR_LIT>' ) : <EOL> delattr ( cls , '<STR_LIT>' ) <EOL> pid = os . fork ( ) <EOL> if pid > <NUM_LIT:0> : <EOL> cls . child_pid = pid <EOL> while not cls . need_stop : <EOL> cpid , status = os . waitpid ( pid , os . WNOHANG ) <EOL> if ( cpid , status ) != ( <NUM_LIT:0> , <NUM_LIT:0> ) : <EOL> break <EOL> time . sleep ( <NUM_LIT:1> ) <EOL> exit_code = status >> <NUM_LIT:8> <EOL> if parent_func is not None : <EOL> parent_func ( ) <EOL> if cls . need_stop : <EOL> break <EOL> else : <EOL> if child_func is not None : <EOL> child_func ( ) <EOL> else : <EOL> break <EOL> except OSError , e : <EOL> msg = "<STR_LIT>" % ( e . errno , e . strerror ) <EOL> logging . error ( msg ) <EOL> sys . stderr . write ( msg + "<STR_LIT:\n>" ) <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> if pid > <NUM_LIT:0> : <EOL> sys . exit ( <NUM_LIT:0> ) <EOL> @ classmethod <EOL> def _handle_sigterm ( cls , signum , frame ) : <EOL> if hasattr ( cls , '<STR_LIT>' ) : <EOL> os . kill ( cls . child_pid , signal . SIGTERM ) <EOL> cls . need_stop = True <EOL> else : <EOL> sys . exit ( <NUM_LIT:0> ) <EOL> class Daemon ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , pidfile , stdin = os . devnull , stdout = os . devnull , stderr = os . devnull , autorestart = False ) : <EOL> self . autorestart = autorestart <EOL> self . stdin = stdin <EOL> self . stdout = stdout <EOL> self . stderr = stderr <EOL> self . pidfile = pidfile <EOL> def daemonize ( self ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> pid = os . fork ( ) <EOL> if pid > <NUM_LIT:0> : <EOL> sys . exit ( <NUM_LIT:0> ) <EOL> except OSError , e : <EOL> msg = "<STR_LIT>" % ( e . errno , e . strerror ) <EOL> log . error ( msg ) <EOL> sys . stderr . write ( msg + "<STR_LIT:\n>" ) <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> log . debug ( "<STR_LIT>" ) <EOL> os . chdir ( "<STR_LIT:/>" ) <EOL> os . setsid ( ) <EOL> if self . autorestart : <EOL> logging . info ( '<STR_LIT>' ) <EOL> AgentSupervisor . start ( parent_func = None , child_func = None ) <EOL> else : <EOL> try : <EOL> pid = os . fork ( ) <EOL> if pid > <NUM_LIT:0> : <EOL> sys . exit ( <NUM_LIT:0> ) <EOL> except OSError , e : <EOL> msg = "<STR_LIT>" % ( e . errno , e . strerror ) <EOL> logging . error ( msg ) <EOL> sys . stderr . write ( msg + "<STR_LIT:\n>" ) <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> if sys . platform != '<STR_LIT>' : <EOL> sys . stdout . flush ( ) <EOL> sys . stderr . flush ( ) <EOL> si = file ( self . stdin , '<STR_LIT:r>' ) <EOL> so = file ( self . stdout , '<STR_LIT>' ) <EOL> se = file ( self . stderr , '<STR_LIT>' , <NUM_LIT:0> ) <EOL> os . dup2 ( si . fileno ( ) , sys . stdin . fileno ( ) ) <EOL> os . dup2 ( so . fileno ( ) , sys . stdout . fileno ( ) ) <EOL> os . dup2 ( se . fileno ( ) , sys . stderr . fileno ( ) ) <EOL> log . info ( "<STR_LIT>" ) <EOL> def start ( self , foreground = False ) : <EOL> log . info ( "<STR_LIT>" ) <EOL> pid = self . pid ( ) <EOL> if pid : <EOL> if pid_exists ( pid ) : <EOL> log . error ( "<STR_LIT>" <EOL> "<STR_LIT>" . format ( self . pidfile ) ) <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> else : <EOL> log . warn ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> log . info ( "<STR_LIT>" % self . pidfile ) <EOL> if not foreground : <EOL> self . daemonize ( ) <EOL> self . write_pidfile ( ) <EOL> self . run ( ) <EOL> def stop ( self ) : <EOL> log . info ( "<STR_LIT>" ) <EOL> pid = self . pid ( ) <EOL> if os . path . exists ( self . pidfile ) : <EOL> os . remove ( self . pidfile ) <EOL> if pid > <NUM_LIT:1> : <EOL> try : <EOL> if self . autorestart : <EOL> try : <EOL> os . kill ( os . getpgid ( pid ) , signal . SIGTERM ) <EOL> except OSError : <EOL> log . warn ( "<STR_LIT>" % os . getpgid ( pid ) ) <EOL> os . kill ( pid , signal . SIGTERM ) <EOL> else : <EOL> os . kill ( pid , signal . SIGTERM ) <EOL> log . info ( "<STR_LIT>" ) <EOL> except OSError , err : <EOL> if str ( err ) . find ( "<STR_LIT>" ) <= <NUM_LIT:0> : <EOL> log . exception ( "<STR_LIT>" % pid ) <EOL> sys . stderr . write ( str ( err ) + "<STR_LIT:\n>" ) <EOL> else : <EOL> message = "<STR_LIT>" % self . pidfile <EOL> log . info ( message ) <EOL> sys . stderr . write ( message ) <EOL> if os . path . exists ( self . pidfile ) : <EOL> os . remove ( self . pidfile ) <EOL> return <EOL> def restart ( self ) : <EOL> "<STR_LIT>" <EOL> self . stop ( ) <EOL> self . start ( ) <EOL> def run ( self ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError <EOL> @ classmethod <EOL> def info ( cls ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError <EOL> def status ( self ) : <EOL> """<STR_LIT>""" <EOL> pid = self . pid ( ) <EOL> if pid < <NUM_LIT:0> : <EOL> message = '<STR_LIT>' % self . __class__ . __name__ <EOL> exit_code = <NUM_LIT:1> <EOL> else : <EOL> try : <EOL> os . kill ( pid , <NUM_LIT:0> ) <EOL> except OSError , e : <EOL> if e . errno != errno . EPERM : <EOL> message = '<STR_LIT>' % ( self . __class__ . __name__ , pid ) <EOL> else : <EOL> message = '<STR_LIT>' <EOL> exit_code = <NUM_LIT:1> <EOL> else : <EOL> message = '<STR_LIT>' % ( self . __class__ . __name__ , pid ) <EOL> exit_code = <NUM_LIT:0> <EOL> log . info ( message ) <EOL> sys . stdout . write ( message + "<STR_LIT:\n>" ) <EOL> sys . exit ( exit_code ) <EOL> def pid ( self ) : <EOL> try : <EOL> pf = file ( self . pidfile , '<STR_LIT:r>' ) <EOL> pid = int ( pf . read ( ) . strip ( ) ) <EOL> pf . close ( ) <EOL> return pid <EOL> except IOError : <EOL> return None <EOL> except ValueError : <EOL> return None <EOL> def write_pidfile ( self ) : <EOL> atexit . register ( self . delpid ) <EOL> pid = str ( os . getpid ( ) ) <EOL> try : <EOL> fp = open ( self . pidfile , '<STR_LIT>' ) <EOL> fp . write ( str ( pid ) ) <EOL> fp . close ( ) <EOL> os . chmod ( self . pidfile , <NUM_LIT:0> <NUM_LIT> ) <EOL> except Exception , e : <EOL> msg = "<STR_LIT>" % self . pidfile <EOL> log . exception ( msg ) <EOL> sys . stderr . write ( msg + "<STR_LIT:\n>" ) <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> def delpid ( self ) : <EOL> try : <EOL> os . remove ( self . pidfile ) <EOL> except OSError : <EOL> pass </s>
