def reconfigure(self): #print("MultiServerSubprocess.reconfigure()...") self.ReconfiguredTime = os.path.getmtime(self.ConfigFile) self.Config = config = expand( yaml.load(open(self.ConfigFile, 'r'), Loader=yaml.SafeLoader)) templates = config.get("templates", {}) services = config.get("services", []) service_list = [] assert isinstance(services, list) for svc_cfg in services: #print("svc_cfg:", svc_cfg) svc = None if "template" in svc_cfg: template = templates.get(svc_cfg.get("template", "*")) if template is not None: c = {} c.update(template) c.update(svc_cfg) svc_cfg = expand(c) names = svc_cfg.get("names", [svc_cfg.get("name")]) for name in names: c = svc_cfg.copy() c["name"] = name svc = Service(expand(c), self.Logger) if svc.Initialized: service_list.append(svc) #print("Service", svc, "created and added to the list") else: self.log( f'service "{svc.ServiceName}" failed to initialize - removing from service list' ) else: #print("MultiServerSubprocess.reconfigure: svc_cfg:", svc_cfg) #print("MultiServerSubprocess.reconfigure: expanded:", expand(svc_cfg)) svc = Service(expand(svc_cfg), self.Logger) if not svc.Initialized: #print("service not initialzed") self.log( f'service "{svc.ServiceName}" failed to initialize - removing from service list' ) else: service_list.append(svc) #print("Service", svc, "created and added to the list") #print("--------") names = ",".join(s.Name for s in service_list) if self.Server is None: self.Server = HTTPServer.from_config(self.Config, service_list, logger=self.Logger) self.log(f"server created with services: {names}") else: self.Server.setServices(service_list) self.log(f"server reconfigured with services: {names}") self.Services = service_list self.log("reconfigured")
def __init__(self, config_file): self.ConfigFile = config_file self.Config = config = expand( yaml.load(open(self.ConfigFile, 'r'), Loader=yaml.SafeLoader)) #self.Config = config = yaml.load(open(self.ConfigFile, 'r'), Loader=yaml.SafeLoader) log_file = config.get("log", "-") self.Logger = Logger(log_file) Logged.__init__(self, "[router]", self.Logger) PyThread.__init__(self, name="[router]", daemon=True) self.ConfigMTime = mtime(self.ConfigFile) self.Apps = None self.configure(config) self.Stop = False
def run(self): self.log("thread started") while not self.Stop: time.sleep(5) mt = mtime(self.ConfigFile) if mt is not None and mt > self.ConfigMTime: self.log("config file modified. reloading.") self.ConfigMTime = mt self.Config = expand( yaml.load(open(self.ConfigFile, 'r'), Loader=yaml.SafeLoader)) for app in self.Apps: app.stop() self.configure(self.Config) self.log("thread ended")
def main(): if not sys.argv[1:] or sys.argv[1] in ("-?", "-h", "--help", "help"): print(Usage) sys.exit(2) config_file = sys.argv[1] config = expand(yaml.load(open(config_file, 'r'), Loader=yaml.SafeLoader)) logger = None if "logger" in config: cfg = config["logger"] debug = cfg.get("debug", False) if cfg.get("enabled", True): logger = Logger(cfg.get("file", "-"), debug=debug) if "pid_file" in config: open(config["pid_file"], "w").write(str(os.getpid())) ms = MPMultiServer(config_file, logger, debug) signal.signal(signal.SIGHUP, ms.reconfigure) #signal.signal(signal.SIGCHLD, ms.child_died) signal.signal(signal.SIGINT, ms.killme) ms.start() ms.join()
def reconfigure(self, *ignore): self.ReconfiguredTime = os.path.getmtime(self.ConfigFile) self.Config = config = expand( yaml.load(open(self.ConfigFile, 'r'), Loader=yaml.SafeLoader)) port = self.Config["port"] if self.Port is None: self.Port = port self.Sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.Sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) self.Sock.bind(('', self.Port)) self.Sock.listen(10) elif port != self.Port: print("Can not change port number") sys.exit(1) new_nprocesses = self.Config.get("processes", 1) if new_nprocesses > len(self.Subprocesses): for p in self.Subprocesses: p.request_reconfigure() for _ in range(new_nprocesses - len(self.Subprocesses)): p = MultiServerSubprocess(self.Port, self.Sock, self.ConfigFile, logger=self.MPLogger) p.start() self.Subprocesses.append(p) #self.log("started new subprocess") elif new_nprocesses < len(self.Subprocesses): while new_nprocesses < len(self.Subprocesses): p = self.Subprocesses.pop() p.stop() #self.log("stopped a subprocess") for p in self.Subprocesses: p.request_reconfigure() else: for p in self.Subprocesses: p.request_reconfigure()
def configure(self, config): self.Apps = [] self.Templates = config.get("templates", {}) for appcfg in config.get("apps", []): if "template" in appcfg: templ = self.Templates[appcfg["template"]] if "name" in appcfg: names = [appcfg["name"]] else: names = appcfg.get("names", []) if not names: print( "Warning: application without a name in the configuration." ) names = ["(unnamed)"] for name in names: cfg = templ.copy() cfg["name"] = name cfg = expand(cfg) self.Apps.append(App(cfg, self.Logger)) else: self.Apps.append(App(appcfg, self.Logger)) for app in self.Apps: if isinstance(app, PyThread): app.start()