def setup(self): # load config file if self.config_file: Config.ACTUAL_CONFIG_FILE = self.config_file else: self.config_file = Config.DEFAULT_CONFIG_FILE if self.section: Config.SECTION_NAME = self.section self.cfg = Config(self.section) if isinstance(self.cfg.daemonize, basestring): self.daemonize = str2bool(self.cfg.daemonize) if self.cfg.log_file is not None: self.log_file = self.cfg.log_file if self.cfg.pidfile is not None: self.pidfile = Pidfile(self.cfg.pidfile) self.master_name = "master: %s" % self.cfg.proc_name if self.cfg.number_workers: self.number_workers = int(self.cfg.number_workers) if self.cfg.graceful_timeout: self.graceful_timeout = float(self.cfg.graceful_timeout) if self.cfg.user: user = self.cfg.user.split(':') if len(user) <= 1: user[1] = user[0] self.uid, self.gid = get_user_info(*user[:2]) else: # set as default self.uid, self.gid = os.getuid(), os.getgid() if self.cfg.bind: binds = [] for b in self.cfg.bind.split(','): addr = b.strip().split(':') binds.append((addr[0], int(addr[1]))) self.bind = binds # self.bind = [tuple(b.strip().split(":")) for b in self.cfg.bind.split(',')] # bind address comma separate else: self.bind = None self.unix_socket = self.cfg.unix_socket if self.cfg.kill_interval: self.kill_interval = int(self.cfg.kill_interval) else: self.kill_interval = 0
def setup(self): # load config file if self.config_file: Config.ACTUAL_CONFIG_FILE = self.config_file else: self.config_file = Config.DEFAULT_CONFIG_FILE if self.section: Config.SECTION_NAME = self.section self.cfg = Config() if isinstance(self.cfg.daemonize, basestring): self.daemonize = str2bool(self.cfg.daemonize) if self.cfg.log_file is not None: self.log_file = self.cfg.log_file if self.cfg.pidfile is not None: self.pidfile = Pidfile(self.cfg.pidfile) self.master_name = "master: %s" % self.cfg.proc_name if self.cfg.number_workers: self.number_workers = int(self.cfg.number_workers) if self.cfg.graceful_timeout: self.graceful_timeout = float(self.cfg.graceful_timeout) if self.cfg.user: user = self.cfg.user.split(':') if len(user) <= 1: user[1] = user[0] self.uid, self.gid = get_user_info(*user[:2]) else: # set as default self.uid, self.gid = os.getuid(), os.getgid() if self.cfg.bind: binds = [] for b in self.cfg.bind.split(','): addr = b.strip().split(':') binds.append((addr[0], int(addr[1]))) self.bind = binds # self.bind = [tuple(b.strip().split(":")) for b in self.cfg.bind.split(',')] # bind address comma separate else: self.bind = None self.unix_socket = self.cfg.unix_socket if self.cfg.kill_interval: self.kill_interval = int(self.cfg.kill_interval) else: self.kill_interval = 0
class Arbiter(object): PIPE = [] START_CTX = {} # for exec LISTENERS = [] # listening sockets WORKERS = {} WORKER_BOOT_ERROR = 3 SOCK_BACKLOG = 20 # signals SIG_QUEUE = [] SIGNALS = [getattr(signal, "SIG%s" % x) \ for x in "HUP QUIT INT TERM TTIN TTOU USR1 USR2 WINCH".split()] SIG_NAMES = dict((getattr(signal, name), name[3:].lower()) for name in dir(signal) if name[:3] == "SIG" and name[3] != "_") def __init__(self, worker_uri, config_file=None, section=None): self.worker_uri = worker_uri self.config_file = config_file self.section = section self.reexec_pid = 0 self.worker_age = 0 self.setup() # init start context cwd = getcwd() #self.master_name = "Master" args = sys.argv[:] args.insert(0, sys.executable) self.START_CTX = {"args": args, "cwd": cwd, 0: sys.executable} self.worker_boot_time = time.time() def setup(self): # load config file if self.config_file: Config.ACTUAL_CONFIG_FILE = self.config_file else: self.config_file = Config.DEFAULT_CONFIG_FILE if self.section: Config.SECTION_NAME = self.section self.cfg = Config(self.section) if isinstance(self.cfg.daemonize, basestring): self.daemonize = str2bool(self.cfg.daemonize) if self.cfg.log_file is not None: self.log_file = self.cfg.log_file if self.cfg.pidfile is not None: self.pidfile = Pidfile(self.cfg.pidfile) self.master_name = "master: %s" % self.cfg.proc_name if self.cfg.number_workers: self.number_workers = int(self.cfg.number_workers) if self.cfg.graceful_timeout: self.graceful_timeout = float(self.cfg.graceful_timeout) if self.cfg.user: user = self.cfg.user.split(':') if len(user) <= 1: user[1] = user[0] self.uid, self.gid = get_user_info(*user[:2]) else: # set as default self.uid, self.gid = os.getuid(), os.getgid() if self.cfg.bind: binds = [] for b in self.cfg.bind.split(','): addr = b.strip().split(':') binds.append((addr[0], int(addr[1]))) self.bind = binds # self.bind = [tuple(b.strip().split(":")) for b in self.cfg.bind.split(',')] # bind address comma separate else: self.bind = None self.unix_socket = self.cfg.unix_socket if self.cfg.kill_interval: self.kill_interval = int(self.cfg.kill_interval) else: self.kill_interval = 0 def __getattr__(self, name): value = getattr(self.cfg, name) if value is None: self.file_logger.warning('No config option %s' % name) return value @property def worker_class(self): """ lazy load after fork """ return import_app(self.worker_uri) def init_signals(self): """\ Initialize master signal handling. Most of the signals are queued. Child signals only wake up the master. """ [signal.signal(s, self.signal) for s in self.SIGNALS] signal.signal(signal.SIGCHLD, self.handle_chld) def start(self): # Initialize the arbiter, create pidfile if needed. self.set_process_owner(self.uid, self.gid) # setup logging self.file_logger = setup_file_logging("prefork", self.log_file) self.file_logger.info("Startting arbiter") self.file_logger.info('Use Config File %s', self.config_file) if self.daemonize: daemonize() _setproctitle(self.master_name) self.pid = os.getpid() self.file_logger.info("Master Pid --> %s" % self.pid) try: if self.pidfile: self.pidfile.create(self.pid) # chown(self.pidfile.fname, self.uid, self.gid) except Exception, e: self.file_logger.info(str(e)) self.file_logger.info("aa") self.init_signals() # close old PIPE on reexec if self.PIPE: [os.close(p) for p in self.PIPE] # initialize the pipe self.PIPE = pair = os.pipe() for p in pair: set_non_blocking(p) close_on_exec(p) if not self.LISTENERS and (self.bind or self.unix_socket): self.file_logger.info("Listern on %s, unixdomian:%s", self.cfg.bind or "", self.cfg.unix_socket or "") self.LISTENERS = create_sockets(self.bind, self.unix_socket, self.SOCK_BACKLOG) for s in self.LISTENERS: close_on_exec(s) s.setblocking(0)
class Arbiter(object): PIPE = [] START_CTX = {} # for exec LISTENERS = [] # listening sockets WORKERS = {} WORKER_BOOT_ERROR = 3 SOCK_BACKLOG = 20 # signals SIG_QUEUE = [] SIGNALS = [getattr(signal, "SIG%s" % x) \ for x in "HUP QUIT INT TERM TTIN TTOU USR1 USR2 WINCH".split()] SIG_NAMES = dict( (getattr(signal, name), name[3:].lower()) for name in dir(signal) if name[:3] == "SIG" and name[3] != "_" ) def __init__(self, worker_uri, config_file=None, section=None): self.worker_uri = worker_uri self.config_file = config_file self.section = section self.reexec_pid = 0 self.worker_age = 0 self.setup() # init start context cwd = getcwd() #self.master_name = "Master" args = sys.argv[:] args.insert(0, sys.executable) self.START_CTX = { "args": args, "cwd": cwd, 0: sys.executable } self.worker_boot_time = time.time() def setup(self): # load config file if self.config_file: Config.ACTUAL_CONFIG_FILE = self.config_file else: self.config_file = Config.DEFAULT_CONFIG_FILE if self.section: Config.SECTION_NAME = self.section self.cfg = Config() if isinstance(self.cfg.daemonize, basestring): self.daemonize = str2bool(self.cfg.daemonize) if self.cfg.log_file is not None: self.log_file = self.cfg.log_file if self.cfg.pidfile is not None: self.pidfile = Pidfile(self.cfg.pidfile) self.master_name = "master: %s" % self.cfg.proc_name if self.cfg.number_workers: self.number_workers = int(self.cfg.number_workers) if self.cfg.graceful_timeout: self.graceful_timeout = float(self.cfg.graceful_timeout) if self.cfg.user: user = self.cfg.user.split(':') if len(user) <= 1: user[1] = user[0] self.uid, self.gid = get_user_info(*user[:2]) else: # set as default self.uid, self.gid = os.getuid(), os.getgid() if self.cfg.bind: binds = [] for b in self.cfg.bind.split(','): addr = b.strip().split(':') binds.append((addr[0], int(addr[1]))) self.bind = binds # self.bind = [tuple(b.strip().split(":")) for b in self.cfg.bind.split(',')] # bind address comma separate else: self.bind = None self.unix_socket = self.cfg.unix_socket if self.cfg.kill_interval: self.kill_interval = int(self.cfg.kill_interval) else: self.kill_interval = 0 def __getattr__(self, name): value = getattr(self.cfg, name) if value is None: self.file_logger.warning('No config option %s' % name) return value @property def worker_class(self): """ lazy load after fork """ return import_app(self.worker_uri) def init_signals(self): """\ Initialize master signal handling. Most of the signals are queued. Child signals only wake up the master. """ [signal.signal(s, self.signal) for s in self.SIGNALS] signal.signal(signal.SIGCHLD, self.handle_chld) def start(self): # Initialize the arbiter, create pidfile if needed. self.set_process_owner(self.uid, self.gid) # setup logging self.file_logger = setup_file_logging("prefork", self.log_file) self.file_logger.info("Startting arbiter") self.file_logger.info('Use Config File %s', self.config_file) if self.daemonize: daemonize() _setproctitle(self.master_name) self.pid = os.getpid() self.file_logger.info("Master Pid --> %s"%self.pid) try: if self.pidfile: self.pidfile.create(self.pid) # chown(self.pidfile.fname, self.uid, self.gid) except Exception,e: self.file_logger.info(str(e)) self.file_logger.info("aa") self.init_signals() # close old PIPE on reexec if self.PIPE: [os.close(p) for p in self.PIPE] # initialize the pipe self.PIPE = pair = os.pipe() for p in pair: set_non_blocking(p) close_on_exec(p) if not self.LISTENERS and (self.bind or self.unix_socket): self.file_logger.info("Listern on %s, unixdomian:%s", self.cfg.bind or "", self.cfg.unix_socket or "") self.LISTENERS = create_sockets(self.bind, self.unix_socket, self.SOCK_BACKLOG) for s in self.LISTENERS: close_on_exec(s) s.setblocking(0)