<s> import os <EOL> import sys <EOL> import logging <EOL> import time <EOL> import pythoncom <EOL> import win32serviceutil <EOL> import win32service <EOL> import win32event <EOL> import win32api <EOL> import servicemanager <EOL> import pulsar <EOL> from pulsar . utils . importer import import_module <EOL> import multiprocessing <EOL> class ServiceManagerLogHandler ( logging . Handler ) : <EOL> def emit ( self , record ) : <EOL> try : <EOL> msg = self . format ( record ) <EOL> if record . levelno >= logging . ERROR : <EOL> servicemanager . LogErrorMsg ( msg ) <EOL> elif record . levelno >= logging . INFO : <EOL> servicemanager . LogiNFOMsg ( msg ) <EOL> except Exception : <EOL> pass <EOL> def ctrlHandler ( ctrlType ) : <EOL> return True <EOL> class PulsarService ( win32serviceutil . ServiceFramework ) : <EOL> _svc_name_ = '<STR_LIT>' % pulsar . __version__ <EOL> _svc_display_name_ = "<STR_LIT>" % pulsar . __version__ <EOL> _svc_description_ = "<STR_LIT>" <EOL> _command_lines = '<STR_LIT>' <EOL> def __init__ ( self , args ) : <EOL> win32serviceutil . ServiceFramework . __init__ ( self , args ) <EOL> self . hWaitStop = win32event . CreateEvent ( None , <NUM_LIT:0> , <NUM_LIT:0> , None ) <EOL> self . running = False <EOL> def SvcStop ( self ) : <EOL> pulsar . arbiter ( ) . stop ( ) <EOL> self . running = False <EOL> self . ReportServiceStatus ( win32service . SERVICE_STOP_PENDING ) <EOL> win32event . SetEvent ( self . hWaitStop ) <EOL> def SvcDoRun ( self ) : <EOL> mod = import_module ( self . _command_lines ) <EOL> os . remove ( os . path . abspath ( mod . __file__ ) ) <EOL> self . setup ( mod . argv ) <EOL> def setup ( self , argv ) : <EOL> raise NotImplementedError <EOL> def main ( self ) : <EOL> arbiter = pulsar . arbiter ( ) <EOL> arbiter . log . info ( '<STR_LIT>' % self . _svc_name_ ) <EOL> self . running = True <EOL> while self . running : <EOL> time . sleep ( <NUM_LIT:1> ) <EOL> arbiter . log . info ( '<STR_LIT>' % self . _svc_name_ ) <EOL> @ classmethod <EOL> def run ( cls , ** params ) : <EOL> argv = sys . argv <EOL> args = sys . argv [ : <NUM_LIT:1> ] <EOL> if len ( sys . argv ) > <NUM_LIT:2> : <EOL> args += sys . argv [ <NUM_LIT:2> : ] <EOL> f = open ( cls . _command_lines + '<STR_LIT>' , '<STR_LIT:w>' ) <EOL> f . write ( '<STR_LIT>' . format ( args ) ) <EOL> f . close ( ) <EOL> argv = argv [ : <NUM_LIT:2> ] <EOL> if not argv or not argv [ <NUM_LIT:0> ] : <EOL> main_path = getattr ( sys . modules [ '<STR_LIT:__main__>' ] , '<STR_LIT>' , None ) <EOL> path , script = os . path . split ( main_path ) <EOL> argv = [ script , '<STR_LIT:start>' ] <EOL> sys . argv = argv <EOL> cls . params = params <EOL> win32api . SetConsoleCtrlHandler ( ctrlHandler , True ) <EOL> win32serviceutil . HandleCommandLine ( cls ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> '''<STR_LIT>''' <EOL> PulsarService . run ( ) </s>
