def start(self): '''Maps to the CLI command and starts one or more Wishbone processes in background. ''' router_config = ConfigFile(self.config, 'SYSLOG').dump() pid_file = PIDFile(self.pid) with DaemonContext(stdout=sys.stdout, stderr=sys.stderr, detach_process=True): if self.instances == 1: sys.stdout.write("\nWishbone instance started with pid %s\n" % (os.getpid())) sys.stdout.flush() pid_file.create([os.getpid()]) self.initializeRouter(router_config) else: for instance in range(self.instances): self.routers.append( gipc.start_process(self.initializeRouter, args=(router_config, ), daemon=True)) pids = [str(p.pid) for p in self.routers] print(("\nInstances started in foreground with pid %s\n" % (", ".join(pids)))) pid_file.create(pids) self.bootstrapBlock()
def start(self, command, config, instances, pid, queue_size, frequency, identification, module_path): ''' Handles the Wishbone start command. ''' if module_path is not None: self.__expandSearchPath(module_path) module_manager = ModuleManager() router_config = ConfigFile().load(config) pid_file = PIDFile(pid) with DaemonContext(stdout=sys.stdout, stderr=sys.stderr, files_preserve=self.__getCurrentFD(), detach_process=True): if instances == 1: sys.stdout.write("\nWishbone instance started with pid %s\n" % (os.getpid())) sys.stdout.flush() pid_file.create([os.getpid()]) Default(router_config, module_manager, size=queue_size, frequency=frequency, identification=identification, stdout_logging=False).start() else: processes = [] for instance in range(instances): processes.append(Default(router_config, module_manager, size=queue_size, frequency=frequency, identification=identification, stdout_logging=False, process=True).start()) pids = [str(p.pid) for p in processes] print(("\n%s Wishbone instances started in background with pid %s\n" % (len(pids), ", ".join(pids)))) pid_file.create(pids) for proc in processes: proc.join()
def start(self): '''Maps to the CLI command and starts one or more Wishbone processes in background. ''' router_config = ConfigFile(self.config, 'SYSLOG').dump() pid_file = PIDFile(self.pid) with DaemonContext(stdout=sys.stdout, stderr=sys.stderr, detach_process=True): if self.instances == 1: sys.stdout.write("\nWishbone instance started with pid %s\n" % (os.getpid())) sys.stdout.flush() pid_file.create([os.getpid()]) self.initializeRouter(router_config) else: for instance in range(self.instances): self.routers.append( gipc.start_process( self.initializeRouter, args=(router_config, ), daemon=True ) ) pids = [str(p.pid) for p in self.routers] print(("\nInstances started in foreground with pid %s\n" % (", ".join(pids)))) pid_file.create(pids) self.bootstrapBlock()
def initializeManyRouters(self, config, number, background): '''Initialize many routers and background if required Args: config (Wishbone.config.configfile:ConfigFile): The router configration number (int): The number of instances to intialize background (bool): Whether to background the routers or not ''' if background: pid_file = PIDFile(self.pid) with DaemonContext(stdout=sys.stdout, stderr=sys.stderr, detach_process=True): if self.instances == 1: sys.stdout.write("\nWishbone instance started with pid %s\n" % (os.getpid())) sys.stdout.flush() pid_file.create([os.getpid()]) self.initializeOneRouter(config) else: for instance in range(self.instances): self.routers.append( gipc.start_process( self.initializeOneRouter, args=(config, ), daemon=True ) ) pids = [str(p.pid) for p in self.routers] print(("\nInstances started in foreground with pid %s\n" % (", ".join(pids)))) pid_file.create(pids) self.bootstrapBlock() else: if self.instances == 1: sys.stdout.write("\nInstance started in foreground with pid %s\n" % (os.getpid())) self.initializeOneRouter(config) else: for instance in range(self.instances): self.routers.append( gipc.start_process( self.initializeOneRouter, args=(config, ), daemon=True ) ) pids = [str(p.pid) for p in self.routers] print(("\nInstances started in foreground with pid %s\n" % (", ".join(pids)))) self.bootstrapBlock()
class Dispatch(): ''' Handles the Wishbone instance commands. ''' def __init__(self): self.config = BootstrapFile() self.routers = [] signal(2, self.__stopSequence) self.__stopping = False self.module_manager = ModuleManager() def generateHeader(self): ''' Prints a header. ''' return """ __ __ __ .--.--.--|__.-----| |--| |--.-----.-----.-----. | | | | |__ --| | _ | _ | | -__| |________|__|_____|__|__|_____|_____|__|__|_____| version %s Build event pipeline servers with minimal effort. """ % (get_distribution('wishbone').version) def debug(self, command, config, instances, queue_size, frequency, identification): ''' Handles the Wishbone debug command. ''' config = self.config.load(config) if instances == 1: self.routers.append(RouterBootstrap(config, debug=True, queue_size=queue_size, frequency=frequency, identification=identification)) self.routers[-1].start() else: for x in xrange(instances): self.routers.append(RouterBootstrapProcess(config, debug=True, queue_size=queue_size, frequency=frequency, identification=identification)) self.routers[-1].start() while multiprocessing.active_children(): sleep(1) sys.exit(0) def list(self, command, group, category=None, include_groups=[]): print self.generateHeader() print "Available modules:" print self.module_manager.getModuleTable(category, group, include_groups) def show(self, command, module): ''' Shows the help message of a module. ''' print self.generateHeader() try: (category, group, module) = module.split('.') except ValueError: (category, sub, group, module) = module.split('.') category = "%s.%s" % (category, sub) try: title = self.module_manager.getModuleTitle(category, group, module) version = self.module_manager.getModuleVersion(category, group, module) header = "%s.%s.%s" % (category, group, module) print print "="*len(header) print header print "="*len(header) print print "Version: %s" % (version) print print title print "-"*len(title) print self.module_manager.getModuleDoc(category, group, module) except Exception: print "Failed to load module %s.%s.%s." % (category, group, module) def start(self, command, config, instances, pid, queue_size, frequency, identification): ''' Handles the Wishbone start command. ''' config = self.config.load(config) self.pid = PIDFile(pid) if instances == 1: print ("Starting 1 instance to background with pid %s." % (os.getpid())) try: with DaemonContext(stdout=sys.stdout, stderr=sys.stderr, files_preserve=self.__getCurrentFD(), detach_process=True): self.pid.create([os.getpid()]) instance = RouterBootstrap(config, debug=False, queue_size=queue_size, frequency=frequency, identification=identification) instance.start() except Exception as err: sys.stdout.write("Failed to start instance. Reason: %s\n" % (err)) else: try: print "Starting %s instances in background." % (instances) with DaemonContext(stdout=sys.stdout, stderr=sys.stderr, files_preserve=self.__getCurrentFD(), detach_process=True): pids = [] processes = [] for counter in xrange(instances): processes.append(RouterBootstrapProcess(config, debug=False, queue_size=queue_size, frequency=frequency, identification=identification)) processes[-1].start() pids.append(processes[-1].pid) self.pid.create(pids) for process in processes: process.join() except Exception as err: sys.stdout.write("Failed to start instance. Reason: %s\n" % (err)) def stop(self, command, pid): ''' Handles the Wishbone stop command. ''' try: pid = PIDFile(pid) sys.stdout.write("Stopping instance with PID ") sys.stdout.flush() for entry in pid.read(): sys.stdout.write(" %s " % (entry)) sys.stdout.flush() pid.sendSigint(entry) pid.cleanup() print("") except Exception as err: print ("") print ("Failed to stop instances. Reason: %s" % (err)) def __stopSequence(self): ''' Calls the stop() function of each instance. ''' if not self.__stopping: # TODO: Weird hack, otherwise when trapping signal(2) this function is # executed many times. self.__stopping = True for instance in self.routers: if hasattr(instance, "stop"): instance.stop() def __getCurrentFD(self): ''' returns a list with filedescriptors in use. ''' try: return [int(x) for x in os.listdir("/proc/self/fd")] except Exception as err: print ("Failed to get active filedescriptors. Reason: %s." % (err)) sys.exit(1) def __alive(self, pid): try: os.kill(pid, 0) return True except: False