def wrapper(self: Karton, *args, **kwargs): handler = LocalLogBuffer() try: # Register new log handler self.log.addHandler(handler) method(self, *args, **kwargs) except Exception: self.log.exception("Analysis failed") finally: # Unregister local handler self.log.removeHandler(handler) try: buffer = StringIO() for idx, entry in enumerate(handler.buffer): if idx > 0: buffer.write("\n") buffer.write(json.dumps(entry)) res = LocalResource(object_name, buffer.getvalue(), bucket="drakrun") task_uid = (self.current_task.payload.get("analysis_uid") or self.current_task.uid) res._uid = f"{task_uid}/{res.name}" # Karton rejects empty resources # Ensure that we upload it only when some data was actually generated if buffer.tell() > 0: res.upload(self.backend) except Exception: self.log.exception("Failed to upload analysis logs")
def wrapper(self: Karton, *args, **kwargs): handler = LocalLogBuffer() try: # Register new log handler self.log.addHandler(handler) method(self, *args, **kwargs) except Exception: self.log.exception("Analysis failed") finally: # Unregister local handler self.log.removeHandler(handler) try: buffer = StringIO() for idx, entry in enumerate(handler.buffer): if idx > 0: buffer.write("\n") buffer.write(json.dumps(entry)) res = LocalResource(object_name, buffer.getvalue(), bucket="drakrun") task_uid = self.current_task.payload.get( 'override_uid') or self.current_task.uid res._uid = f"{task_uid}/{res.name}" res.upload(self.backend) except Exception: self.log.exception("Failed to upload analysis logs")