def __call__(self, *args, **kwargs): try: return self.main(*args, **kwargs) except Exception as err: if not isinstance(err, exceptions.BrokerError): err = exceptions.BrokerError(err) sys.exit(err.error_code)
def handle_keyboardinterrupt(*args): choice = input( "\nEnding Broker while running won't end processes being monitored.\n" "Would you like to switch Broker to run in the background?\n" "[y/n]: ") if choice.lower()[0] == "y": fork_broker() else: raise exceptions.BrokerError("Broker killed by user.")
def __call__(self, *args, **kwargs): try: return self.main(*args, **kwargs) except Exception as err: if not isinstance(err, exceptions.BrokerError): err = exceptions.BrokerError(err) helpers.emit(return_code=err.error_code, error_message=str(err.message)) sys.exit(err.error_code) helpers.emit(return_code=0)
def wait_file(self): start = time.time() while self.lock.exists(): if (time.time() - start) < self.timeout: time.sleep(1) continue else: raise exceptions.BrokerError( f"Timeout while attempting to open {self.file.absolute()}") self.lock.touch() return self.file
def emit_to_file(self, *args, **kwargs): if not self.file: return for arg in args: if not isinstance(arg, dict): raise exceptions.BrokerError( f"Received an invalid data emission {arg}") kwargs.update(arg) for key in kwargs.keys(): if getattr(kwargs[key], "json", None): kwargs[key] = kwargs[key].json curr_data = json.loads(self.file.read_text() or "{}") curr_data.update(kwargs) self.file.write_text(json.dumps(curr_data, indent=4, sort_keys=True))
def resolve_file_args(broker_args): """Check for files being passed in as values to arguments, then attempt to resolve them. If not resolved, keep arg/value pair intact. """ final_args = {} for key, val in broker_args.items(): if isinstance(val, Path) or (isinstance(val, str) and val[-4:] in ("json", "yaml", ".yml")): if data := load_file(val): if key == "args_file": if isinstance(data, dict): final_args.update(data) elif isinstance(data, list): for d in data: final_args.update(d) else: final_args[key] = data elif key == "args_file": raise exceptions.BrokerError(f"No data loaded from {val}") else: final_args[key] = val else: final_args[key] = val