def clientWrapper(argv): """Entry point for an Open Content Platform client. This function loads global settings, sets up logging, calls the service flows, waits for completion, and then cleans up. Usage:: $ python openContentClient.py [contentGathering|universalJob|resultProcessing] """ try: startTime = time.time() globalSettings = basicSetup(argv) ## Create and monitor the client process clientLoop(argv[1], globalSettings) logger = logging.getLogger('ClientStartup') endTime = time.time() runTime = utils.prettyRunTime(startTime, endTime) logger.warning('Client stopped. Total runtime was {}'.format(runTime)) print('Client stopped. Total runtime was {}'.format(runTime)) except: stacktrace = traceback.format_exception(sys.exc_info()[0], sys.exc_info()[1], sys.exc_info()[2]) ## The basic print is here for a console message in case we weren't able ## to connect the logging mechanism before encountering a failure. print('Exception in clientWrapper: {}'.format(stacktrace)) with suppress(Exception): logger.debug(stacktrace) ## end clientWrapper return
def serviceClient(self): """Main entry point for the service platform. This function loads global settings, sets up logging, calls the service flows, waits for completion, and then cleans up. """ try: print( 'Starting Open Content Platform.\nTo stop, you can use the Services console or invoke the command directly: python {} stop' .format(__file__)) if not os.path.exists(env.logPath): os.makedirs(env.logPath) utils.masterLog(env, 'openContentClientMasterLog') ## Parse global settings settings = utils.loadSettings( os.path.join(env.configPath, 'globalSettings.json')) ## Setup requested log handlers startTime = time.time() self.setupLogging( env.logPath, os.path.join(env.configPath, settings['fileContainingClientLogSettings'])) logger = logging.getLogger('ClientStartup') logger.info('Starting {}'.format(clientLabel)) logger.info(' Main-process identifier (PID): {}'.format( os.getpid())) logger.info( ' Started from a service; use the Services console to stop, or invoke the command directly: python {} stop' .format(__file__)) ## Create and monitor the service processes self.clientLoop(settings) ## Finish up logger = logging.getLogger('ClientStartup') endTime = time.time() runTime = utils.prettyRunTime(startTime, endTime) logger.warning('Stopped {}. Total runtime was {}'.format( clientLabel, runTime)) print('Stopped {}. Total runtime was {}'.format( clientLabel, runTime)) except: stacktrace = traceback.format_exception(sys.exc_info()[0], sys.exc_info()[1], sys.exc_info()[2]) ## The basic print is here for a console message in case we weren't able ## to connect the logging mechanism before encountering a failure. print('Exception in serviceClient: {}'.format(stacktrace)) with suppress(Exception): logger.debug(stacktrace) ## end serviceClient return
def servicePlatform(self): """Main entry point for the service platform. This function loads global settings, sets up logging, calls the service flows, waits for completion, and then cleans up. """ try: print( 'Starting Open Content Platform.\nTo stop, you can use the Services console or invoke the command directly: python {}\windowsPlatformService.py stop' .format(path)) if not os.path.exists(env.logPath): os.makedirs(env.logPath) startTime = time.time() ## Parse global settings globalSettings = utils.loadSettings( os.path.join(env.configPath, 'globalSettings.json')) ## Setup logging logger = self.setupLogging(globalSettings) logger.info('Starting Open Content Platform.') logger.info(' Main-process identifier (PID): {}'.format( os.getpid())) logger.info( ' Started from a service; use the Services console to stop, or invoke the command directly: python {}\windowsPlatformService.py stop' .format(path)) ## Create and monitor the service processes self.serviceLoop(globalSettings, logger) ## Finish up endTime = time.time() runTime = utils.prettyRunTime(startTime, endTime) logger.warning( 'Open Content Platform stopped. Total runtime was {}'.format( runTime)) print('Open Content Platform stopped. Total runtime was {}'.format( runTime)) except: stacktrace = traceback.format_exception(sys.exc_info()[0], sys.exc_info()[1], sys.exc_info()[2]) ## The basic print is here for a console message in case we weren't able ## to connect the logging mechanism before encountering a failure. print('Exception in servicePlatform: {}'.format(stacktrace)) with suppress(Exception): logger.debug(stacktrace) ## end servicePlatform return
def getExecutionEnvironment(self): """Get regular execution environment updates for this client.""" data = {} try: data['endpoint'] = self.endpointName currentTime = time.time() data['lastSystemStatus'] = datetime.datetime.fromtimestamp( currentTime).strftime('%Y-%m-%d %H:%M:%S') ## Server wide CPU average (across all cores, threads, virtuals) data['cpuAvgUtilization'] = psutil.cpu_percent() ## Server wide memory memory = psutil.virtual_memory() data['memoryAproxTotal'] = utils.getGigOrMeg(memory.total) data['memoryAproxAvailable'] = utils.getGigOrMeg(memory.available) data['memoryPercentUsed'] = memory.percent ## Info on this process data['pid'] = os.getpid() process = psutil.Process(data['pid']) data['processCpuPercent'] = process.cpu_percent() data['processMemory'] = process.memory_full_info().uss ## Create time in epoc startTime = process.create_time() data['processStartTime'] = datetime.datetime.fromtimestamp( startTime).strftime('%Y-%m-%d %H:%M:%S') data['processRunTime'] = utils.prettyRunTime( startTime, currentTime) except psutil.AccessDenied: exception = traceback.format_exception(sys.exc_info()[0], sys.exc_info()[1], sys.exc_info()[2]) self.logger.error('Exception: {exception!r}', exception=exception) self.logger.error( 'AccessDenied errors on Windows usually mean the client was not started as Administrator.' ) print( 'AccessDenied errors for environment usually mean the client was not started as Administrator; please run with an elevated account.' ) except: exception = traceback.format_exception(sys.exc_info()[0], sys.exc_info()[1], sys.exc_info()[2]) self.logger.error('Exception: {exception!r}', exception=exception) ## end getExecutionEnvironment return data
def servicePlatform(): """Entry point for the Open Content Platform. This function loads global settings, sets up logging, calls the service flows, waits for completion, and then cleans up. Usage:: $ python openContentPlatform.py """ try: print('Starting Open Content Platform.') print(' Main-process identifier (PID): {}.'.format(os.getpid())) print(' Press Ctrl+C to exit.\n'.format(os.getpid())) ## Parse global settings globalSettings = loadSettings( os.path.join(env.configPath, 'globalSettings.json')) startTime = time.time() ## Setup logging logger = setupLogging(globalSettings) ## Register signals registerSignals() ## Create and monitor the service processes serviceLoop(logger, globalSettings) ## Finish up endTime = time.time() runTime = prettyRunTime(startTime, endTime) logger.debug( 'Open Content Platform stopped. Total runtime was {}'.format( runTime)) print('Open Content Platform stopped. Total runtime was {}'.format( runTime)) except: stacktrace = traceback.format_exception(sys.exc_info()[0], sys.exc_info()[1], sys.exc_info()[2]) ## The print is here for a console message in case we weren't able ## to connect the logging mechanism before encountering a failure. print('Exception in servicePlatform: {}'.format(stacktrace)) with suppress(Exception): logger.debug(str(stacktrace)) ## end servicePlatform return