def _configure_petisco_dependencies(self): self.petisco = Petisco.get_instance() self._check_app_name() self._check_app_version() self._check_logger() self._check_notifier() self._check_publisher()
def notify(self, result): if result is not None and result.is_failure: error = result.value if issubclass(error.__class__, CriticalError): self.notifier.publish( NotifierExceptionMessage( exception=error.exception, executor=error.executor, input_parameters=error.input_parameters, traceback=error.traceback, info_petisco=Petisco.get_info(), ))
def petisco_sql_database(request): persistence_source_marker = request.node.get_closest_marker( "persistence_source") if persistence_source_marker is None: persistence_source_name = "petisco" else: persistence_source_name = persistence_source_marker.args[0] try: petisco = Petisco.get_instance() persistence_source_config = petisco.persistence_sources[ persistence_source_name] except ImportError as e: raise ImportError( f"{e}\n petisco_sql_database needs a preconfigured Petisco to have available Model definitions" ) except KeyError as e: raise KeyError( f"{e}\n petisco_sql_database needs a preconfigured Petisco with persistence. Required persistence ({persistence_source_name}) is not available" ) if not persistence_source_config or not persistence_source_config[ "configured"]: yield else: from sqlalchemy import create_engine from petisco.persistence.sqlalchemy.sqlalchemy_persistence import ( SqlAlchemyPersistence, ) sql_database = os.environ.get("SQL_DATABASE") if not sql_database: raise RuntimeError( "Please add required SQL_DATABASE env variable (e.g pytest.ini, pytest.end2end.ini)" ) Base = SqlAlchemyPersistence.get_instance( ).sources[persistence_source_name]["base"] Session = SqlAlchemyPersistence.get_instance( ).sources[persistence_source_name]["session"] connection = f"sqlite:///{sql_database}" engine = create_engine(connection) Base.metadata.create_all(engine) yield session = Session() session.rollback() session.close() Base.metadata.drop_all(bind=engine) os.remove(sql_database)
def _check_app_version(self): if self.app_version == DEFAULT_APP_VERSION: self.app_version = Petisco.get_app_version()
def _check_app_name(self): if self.app_name == DEFAULT_ERROR_MESSAGE: self.app_name = Petisco.get_app_name()
def wrapper(*args, **kwargs): @timer @meiga def run_controller(*args, **kwargs) -> Tuple[Result, float]: params = inspect.getfullargspec(func).args kwargs = {k: v for k, v in kwargs.items() if k in params} return func(*args, **kwargs) self._configure_petisco_dependencies() info_id = None is_success = False elapsed_time = None try: kwargs = add_petisco(kwargs) kwargs = add_headers(kwargs) result_kwargs = add_info_id(kwargs) self.notify(result_kwargs, info_id) log_message = LogMessage( layer="controller", operation=f"{func.__name__}" ) if result_kwargs.is_failure: http_response = self.handle_failure(log_message, result_kwargs) else: kwargs, info_id = result_kwargs.value log_message.info_id = info_id self.logger.log( DEBUG, log_message.set_message("Processing Request") ) result_controller, elapsed_time = run_controller(*args, **kwargs) self.notify(result_controller, info_id) if result_controller.is_failure: http_response = self.handle_failure( log_message, result_controller ) else: message = self._get_success_message(result_controller) self.logger.log(DEBUG, log_message.set_message(message)) http_response = ( self.success_handler(result_controller) if self.success_handler else DEFAULT_SUCCESS_MESSAGE ) is_success = True except Exception as e: log_message = LogMessage( layer="controller", operation=f"{func.__name__}" ) log_message.info_id = info_id if info_id else None message = f"Error {func.__name__}: {repr(e.__class__)} {e} | {traceback.format_exc()}" self.logger.log(ERROR, log_message.set_message(message)) http_response = InternalHttpError().handle() self.notifier.publish( NotifierExceptionMessage( exception=e, executor=func.__name__, traceback=traceback.format_exc(), info_id=info_id, info_petisco=Petisco.get_info(), ) ) if self.send_request_responded_event: request_responded = RequestResponded( app_name=self.app_name, app_version=self.app_version, controller=f"{func.__name__}", is_success=is_success, http_response=http_response, elapsed_time=elapsed_time, ).add_info_id(info_id) self.event_bus.publish(request_responded) return http_response
def _check_publisher(self): if self.event_bus == DEFAULT_EVENT_BUS: self.event_bus = Petisco.get_event_bus()
def _check_notifier(self): if self.notifier == DEFAULT_NOTIFIER: self.notifier = Petisco.get_notifier()
def _check_logger(self): if self.logger == DEFAULT_LOGGER: self.logger = Petisco.get_logger()
def healthcheck() -> Result[Dict, Error]: return HealthcheckProvider().execute(Petisco.get_instance())
def _check_logger(self): if self.logger == DEFAULT_LOGGER: from petisco import Petisco self.logger = Petisco.get_logger()
def environment() -> Result[Dict, Error]: return EnvironmentProvider().execute(Petisco.get_instance())
def given_any_petisco(): Petisco.clear() petisco = Petisco(config=Config(app_name="petisco", app_version="1.0.0")) yield petisco Petisco.clear()
def redis_event_handler(message): event = event_from_redis_message(message).unwrap_or_return() logger = Petisco.get_instance()._logger logger.log(INFO, event)