<s> doc = """<STR_LIT>""" <EOL> import os <EOL> import socket <EOL> import sys <EOL> import time <EOL> from superlance . compat import urlparse <EOL> from superlance . compat import xmlrpclib <EOL> from supervisor import childutils <EOL> from supervisor . states import ProcessStates <EOL> from supervisor . options import make_namespec <EOL> from superlance import timeoutconn <EOL> def usage ( ) : <EOL> print ( doc ) <EOL> sys . exit ( <NUM_LIT:255> ) <EOL> class HTTPOk : <EOL> connclass = None <EOL> def __init__ ( self , rpc , programs , any , url , timeout , status , inbody , <EOL> email , sendmail , coredir , gcore , eager , retry_time ) : <EOL> self . rpc = rpc <EOL> self . programs = programs <EOL> self . any = any <EOL> self . url = url <EOL> self . timeout = timeout <EOL> self . retry_time = retry_time <EOL> self . status = status <EOL> self . inbody = inbody <EOL> self . email = email <EOL> self . sendmail = sendmail <EOL> self . coredir = coredir <EOL> self . gcore = gcore <EOL> self . eager = eager <EOL> self . stdin = sys . stdin <EOL> self . stdout = sys . stdout <EOL> self . stderr = sys . stderr <EOL> def listProcesses ( self , state = None ) : <EOL> return [ x for x in self . rpc . supervisor . getAllProcessInfo ( ) <EOL> if x [ '<STR_LIT:name>' ] in self . programs and <EOL> ( state is None or x [ '<STR_LIT:state>' ] == state ) ] <EOL> def runforever ( self , test = False ) : <EOL> parsed = urlparse . urlsplit ( self . url ) <EOL> scheme = parsed [ <NUM_LIT:0> ] . lower ( ) <EOL> hostport = parsed [ <NUM_LIT:1> ] <EOL> path = parsed [ <NUM_LIT:2> ] <EOL> query = parsed [ <NUM_LIT:3> ] <EOL> if query : <EOL> path += '<STR_LIT:?>' + query <EOL> if self . connclass : <EOL> ConnClass = self . connclass <EOL> elif scheme == '<STR_LIT:http>' : <EOL> ConnClass = timeoutconn . TimeoutHTTPConnection <EOL> elif scheme == '<STR_LIT>' : <EOL> ConnClass = timeoutconn . TimeoutHTTPSConnection <EOL> else : <EOL> raise ValueError ( '<STR_LIT>' % scheme ) <EOL> while <NUM_LIT:1> : <EOL> headers , payload = childutils . listener . wait ( self . stdin , self . stdout ) <EOL> if not headers [ '<STR_LIT>' ] . startswith ( '<STR_LIT>' ) : <EOL> childutils . listener . ok ( self . stdout ) <EOL> if test : <EOL> break <EOL> continue <EOL> conn = ConnClass ( hostport ) <EOL> conn . timeout = self . timeout <EOL> specs = self . listProcesses ( ProcessStates . RUNNING ) <EOL> if self . eager or len ( specs ) > <NUM_LIT:0> : <EOL> try : <EOL> for will_retry in range ( <EOL> self . timeout // ( self . retry_time or <NUM_LIT:1> ) - <NUM_LIT:1> , <EOL> - <NUM_LIT:1> , - <NUM_LIT:1> ) : <EOL> try : <EOL> headers = { '<STR_LIT>' : '<STR_LIT>' } <EOL> conn . request ( '<STR_LIT:GET>' , path , headers = headers ) <EOL> break <EOL> except socket . error as e : <EOL> if e . errno == <NUM_LIT> and will_retry : <EOL> time . sleep ( self . retry_time ) <EOL> else : <EOL> raise <EOL> res = conn . getresponse ( ) <EOL> body = res . read ( ) <EOL> status = res . status <EOL> msg = '<STR_LIT>' % ( self . url , <EOL> res . status , <EOL> res . reason ) <EOL> except Exception as e : <EOL> body = '<STR_LIT>' <EOL> status = None <EOL> msg = '<STR_LIT>' % ( self . url , e ) <EOL> if str ( status ) != str ( self . status ) : <EOL> subject = '<STR_LIT>' % self . url <EOL> self . act ( subject , msg ) <EOL> elif self . inbody and self . inbody not in body : <EOL> subject = '<STR_LIT>' % self . url <EOL> self . act ( subject , msg ) <EOL> childutils . listener . ok ( self . stdout ) <EOL> if test : <EOL> break <EOL> def act ( self , subject , msg ) : <EOL> messages = [ msg ] <EOL> def write ( msg ) : <EOL> self . stderr . write ( '<STR_LIT>' % msg ) <EOL> self . stderr . flush ( ) <EOL> messages . append ( msg ) <EOL> try : <EOL> specs = self . rpc . supervisor . getAllProcessInfo ( ) <EOL> except Exception as e : <EOL> write ( '<STR_LIT>' % e ) <EOL> return <EOL> waiting = list ( self . programs ) <EOL> if self . any : <EOL> write ( '<STR_LIT>' ) <EOL> for spec in specs : <EOL> name = spec [ '<STR_LIT:name>' ] <EOL> group = spec [ '<STR_LIT>' ] <EOL> self . restart ( spec , write ) <EOL> namespec = make_namespec ( group , name ) <EOL> if name in waiting : <EOL> waiting . remove ( name ) <EOL> if namespec in waiting : <EOL> waiting . remove ( namespec ) <EOL> else : <EOL> write ( '<STR_LIT>' % self . programs ) <EOL> for spec in specs : <EOL> name = spec [ '<STR_LIT:name>' ] <EOL> group = spec [ '<STR_LIT>' ] <EOL> namespec = make_namespec ( group , name ) <EOL> if ( name in self . programs ) or ( namespec in self . programs ) : <EOL> self . restart ( spec , write ) <EOL> if name in waiting : <EOL> waiting . remove ( name ) <EOL> if namespec in waiting : <EOL> waiting . remove ( namespec ) <EOL> if waiting : <EOL> write ( <EOL> '<STR_LIT>' % <EOL> waiting ) <EOL> if self . email : <EOL> message = '<STR_LIT:\n>' . join ( messages ) <EOL> self . mail ( self . email , subject , message ) <EOL> def mail ( self , email , subject , msg ) : <EOL> body = '<STR_LIT>' % self . email <EOL> body += '<STR_LIT>' % subject <EOL> body += '<STR_LIT:\n>' <EOL> body += msg <EOL> with os . popen ( self . sendmail , '<STR_LIT:w>' ) as m : <EOL> m . write ( body ) <EOL> self . stderr . write ( '<STR_LIT>' % body ) <EOL> self . mailed = body <EOL> def restart ( self , spec , write ) : <EOL> namespec = make_namespec ( spec [ '<STR_LIT>' ] , spec [ '<STR_LIT:name>' ] ) <EOL> if spec [ '<STR_LIT:state>' ] is ProcessStates . RUNNING : <EOL> if self . coredir and self . gcore : <EOL> corename = os . path . join ( self . coredir , namespec ) <EOL> cmd = self . gcore + '<STR_LIT>' % ( corename , spec [ '<STR_LIT>' ] ) <EOL> with os . popen ( cmd ) as m : <EOL> write ( '<STR_LIT>' % ( <EOL> namespec , m . read ( ) ) ) <EOL> write ( '<STR_LIT>' % namespec ) <EOL> try : <EOL> self . rpc . supervisor . stopProcess ( namespec ) <EOL> except xmlrpclib . Fault as e : <EOL> write ( '<STR_LIT>' % ( <EOL> namespec , e ) ) <EOL> try : <EOL> self . rpc . supervisor . startProcess ( namespec ) <EOL> except xmlrpclib . Fault as e : <EOL> write ( '<STR_LIT>' % ( <EOL> namespec , e ) ) <EOL> else : <EOL> write ( '<STR_LIT>' % namespec ) <EOL> else : <EOL> write ( '<STR_LIT>' % namespec ) <EOL> def main ( argv = sys . argv ) : <EOL> import getopt <EOL> short_args = "<STR_LIT>" <EOL> long_args = [ <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> ] <EOL> arguments = argv [ <NUM_LIT:1> : ] <EOL> try : <EOL> opts , args = getopt . getopt ( arguments , short_args , long_args ) <EOL> except : <EOL> usage ( ) <EOL> if not args : <EOL> usage ( ) <EOL> if len ( args ) > <NUM_LIT:1> : <EOL> usage ( ) <EOL> programs = [ ] <EOL> any = False <EOL> sendmail = '<STR_LIT>' <EOL> gcore = '<STR_LIT>' <EOL> coredir = None <EOL> eager = True <EOL> email = None <EOL> timeout = <NUM_LIT:10> <EOL> retry_time = <NUM_LIT:10> <EOL> status = '<STR_LIT>' <EOL> inbody = None <EOL> for option , value in opts : <EOL> if option in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> usage ( ) <EOL> if option in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> programs . append ( value ) <EOL> if option in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> any = True <EOL> if option in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> sendmail = value <EOL> if option in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> email = value <EOL> if option in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> timeout = int ( value ) <EOL> if option in ( '<STR_LIT:-c>' , '<STR_LIT>' ) : <EOL> status = value <EOL> if option in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> inbody = value <EOL> if option in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> gcore = value <EOL> if option in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> coredir = value <EOL> if option in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> eager = True <EOL> if option in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> eager = False <EOL> url = arguments [ - <NUM_LIT:1> ] <EOL> try : <EOL> rpc = childutils . getRPCInterface ( os . environ ) <EOL> except KeyError as e : <EOL> if e . args [ <NUM_LIT:0> ] != '<STR_LIT>' : <EOL> raise <EOL> sys . stderr . write ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> sys . stderr . flush ( ) <EOL> return <EOL> prog = HTTPOk ( rpc , programs , any , url , timeout , status , inbody , email , <EOL> sendmail , coredir , gcore , eager , retry_time ) <EOL> prog . runforever ( ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> main ( ) </s>
