def db_query(error_code, close=True): """Provide a transactional scope around Query operations. From https://docs.sqlalchemy.org/en/13/orm/session_basics.html Args: error_code: Error code to use in messages close: Close session if True. GraphQL mutations sometimes require the session to remain open. Returns: None """ # Initialize key variables prefix = 'Unable to read database.' # Create session from pool session = POOL() # Setup basic functions try: yield session except Exception as exception_error: session.close() log_message = '{}. Error: "{}"'.format(prefix, exception_error) log.log2info(error_code, log_message) except: session.close() log_message = '{}. Unknown error'.format(prefix) log.log2info(error_code, log_message) finally: # Return the Connection to the pool if bool(close) is True: session.close()
def start(self): """Start the daemon. Args: None Returns: None """ # Check for a pidfile to see if the daemon already runs pid = _pid(self.pidfile) # Die if already running if bool(pid) is True: log_message = ( 'PID file: {} already exists. Daemon already running?' ''.format(self.pidfile)) log.log2die(1073, log_message) # Start the daemon self._daemonize() # Log success log_message = ( 'Daemon {} started - PID file: {}' ''.format(self.name, self.pidfile)) log.log2info(1070, log_message) # Run code for daemon self.run()
def query(self): """Query all remote targets for data. Args: None Returns: None """ # Check for lock and pid files if os.path.exists(self.lockfile_parent) is True: log_message = ('''\ Lock file {} exists. Multiple API daemons running API may have died \ catastrophically in the past, in which case the lockfile should be deleted.\ '''.format(self.lockfile_parent)) log.log2see(1083, log_message) if os.path.exists(self.pidfile_parent) is True: log_message = ('''\ PID file: {} already exists. Daemon already running? If not, it may have died \ catastrophically in the past in which case you should use --stop --force to \ fix.'''.format(self.pidfile_parent)) log.log2see(1084, log_message) ###################################################################### # # Assign options in format that the Gunicorn WSGI will accept # # NOTE! to get a full set of valid options pprint(self.cfg.settings) # in the instantiation of _StandaloneApplication. The option names # do not exactly match the CLI options found at # http://docs.gunicorn.org/en/stable/settings.html # ###################################################################### options = { 'bind': _ip_binding(self._agent_api_variable), 'accesslog': self.config.log_file_api, 'errorlog': self.config.log_file_api, 'capture_output': True, 'pidfile': self._pidfile_child, 'loglevel': self.config.log_level, 'workers': _number_of_workers(), 'umask': 0o0007, } # Log so that user running the script from the CLI knows that something # is happening log_message = ( 'API running on {}:{} and logging to file {}.' ''.format( self._agent_api_variable.ip_listen_address, self._agent_api_variable.ip_bind_port, self.config.log_file_api)) log.log2info(1022, log_message) # Run _StandaloneApplication(self._app, self.parent, options=options).run()
def stop(self): """Stop the daemon. Args: None Returns: None """ # Check for a pidfile to see if the daemon already runs pid = _pid(self.pidfile) if bool(pid) is False: log_message = ( 'PID file: {} does not exist. Daemon not running?' ''.format(self.pidfile)) log.log2warning(1063, log_message) # Not an error in a restart return # Try killing the daemon process try: os.kill(pid, signal.SIGTERM) except OSError as err: error = str(err.args) if error.find('No such process') > 0: self.delpid() self.dellock() else: log_message = (str(err.args)) log_message = ( '{} - PID file: {}'.format(log_message, self.pidfile)) log.log2die(1068, log_message) except: log_message = ( 'Unknown daemon "stopped" error for PID file: {}' ''.format(self.pidfile)) log.log2die(1066, log_message) # Log success self.delpid() self.dellock() log_message = ( 'Daemon {} stopped - PID file: {}' ''.format(self.name, self.pidfile)) log.log2info(1071, log_message)