def _async_callback(task, event_type=None): event = Event(name=event_type.name) try: result = task.result() event.payload_type = result.__class__.__name__ event.payload = result except Exception as ex: event.error = True event.error_message = str(ex) finally: try: publish(event) except Exception as ex: logger.exception(f"Error publishing event: {ex}")
def wrapper(wrapped, _, args, kwargs): # Allows for conditionally disabling publishing _publish_success = kwargs.pop("_publish_success", True) _publish_error = kwargs.pop("_publish_error", True) event = Event(name=event_type.name) try: result = wrapped(*args, **kwargs) event.payload_type = result.__class__.__name__ event.payload = result return result except Exception as ex: event.error = True event.error_message = str(ex) raise finally: if (not event.error and _publish_success) or (event.error and _publish_error): publish(event)