本文整理汇总了Python中circus.controller.Controller类的典型用法代码示例。如果您正苦于以下问题:Python Controller类的具体用法?Python Controller怎么用?Python Controller使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Controller类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
def __init__(self, watchers, endpoint, pubsub_endpoint, check_delay=1.,
prereload_fn=None, context=None, loop=None,
stats_endpoint=None, plugins=None):
self.watchers = watchers
self.endpoint = endpoint
self.check_delay = check_delay
self.prereload_fn = prereload_fn
self.pubsub_endpoint = pubsub_endpoint
# initialize zmq context
self.context = context or zmq.Context.instance()
self.loop = loop or ioloop.IOLoop()
self.ctrl = Controller(endpoint, self.context, self.loop, self,
check_delay)
self.pid = os.getpid()
self._watchers_names = {}
self.alive = True
self._lock = RLock()
# initializing circusd-stats as a watcher when configured
self.stats_endpoint = stats_endpoint
if self.stats_endpoint is not None:
cmd = "%s -c 'from circus import stats; stats.main()'" % \
sys.executable
cmd += ' --endpoint %s' % self.endpoint
cmd += ' --pubsub %s' % self.pubsub_endpoint
cmd += ' --statspoint %s' % self.stats_endpoint
stats_watcher = Watcher('circusd-stats', cmd)
self.watchers.append(stats_watcher)
self.plugins = plugins
self._plugins = {}
示例2: __init__
def __init__(
self,
watchers,
endpoint,
pubsub_endpoint,
check_delay=1.0,
prereload_fn=None,
context=None,
loop=None,
check_flapping=True,
):
self.watchers = watchers
self.endpoint = endpoint
self.check_delay = check_delay
self.prereload_fn = prereload_fn
self.pubsub_endpoint = pubsub_endpoint
# initialize zmq context
self.context = context or zmq.Context.instance()
self.loop = loop or ioloop.IOLoop()
self.ctrl = Controller(endpoint, self.context, self.loop, self, check_delay)
self.pid = os.getpid()
self._watchers_names = {}
self.alive = True
self.busy = False
self.check_flapping = check_flapping
示例3: _init_context
def _init_context(self, context):
self.context = context or zmq.Context.instance()
if self.loop is None:
ioloop.install()
self.loop = ioloop.IOLoop.instance()
self.ctrl = Controller(self.endpoint, self.multicast_endpoint,
self.context, self.loop, self, self.check_delay)
示例4: __init__
def __init__(self, shows, endpoint, check_delay=1., ipc_path=None):
self.shows = shows
self.endpoint = endpoint
self.check_delay = check_delay
self.ipc_path = ipc_path
self.ctrl = Controller(endpoint, self, self.check_delay,
self.ipc_path)
self.pid = os.getpid()
self._shows_names = {}
self.alive = True
self._lock = Lock()
self.setup()
logger.info("Starting master on pid %s" % self.pid)
示例5: __init__
def __init__(
self,
watchers,
endpoint,
pubsub_endpoint,
check_delay=1.0,
prereload_fn=None,
context=None,
loop=None,
stats_endpoint=None,
plugins=None,
sockets=None,
):
self.watchers = watchers
self.endpoint = endpoint
self.check_delay = check_delay
self.prereload_fn = prereload_fn
self.pubsub_endpoint = pubsub_endpoint
# initialize zmq context
self.context = context or zmq.Context.instance()
self.loop = loop or ioloop.IOLoop()
self.ctrl = Controller(endpoint, self.context, self.loop, self, check_delay)
self.pid = os.getpid()
self._watchers_names = {}
self.alive = True
self._lock = RLock()
# initializing circusd-stats as a watcher when configured
self.stats_endpoint = stats_endpoint
if self.stats_endpoint is not None:
cmd = "%s -c 'from circus import stats; stats.main()'" % sys.executable
cmd += " --endpoint %s" % self.endpoint
cmd += " --pubsub %s" % self.pubsub_endpoint
cmd += " --statspoint %s" % self.stats_endpoint
stats_watcher = Watcher("circusd-stats", cmd)
self.watchers.append(stats_watcher)
# adding each plugin as a watcher
if plugins is not None:
for plugin in plugins:
fqnd = plugin["use"]
name = "plugin:%s" % fqnd.replace(".", "-")
cmd = get_plugin_cmd(plugin, self.endpoint, self.pubsub_endpoint, self.check_delay)
plugin_watcher = Watcher(name, cmd, priority=1, singleton=True)
self.watchers.append(plugin_watcher)
self.sockets = CircusSockets(sockets)
示例6: __init__
def __init__(self, shows, endpoint, pubsub_endpoint, check_delay=1.,
prereload_fn=None):
self.shows = shows
self.endpoint = endpoint
self.check_delay = check_delay
self.prereload_fn = prereload_fn
self.pubsub_endpoint = pubsub_endpoint
self.context = zmq.Context()
self.ctrl = Controller(self.context, endpoint, self, self.check_delay)
self.pid = os.getpid()
self._shows_names = {}
self.alive = True
self._lock = Lock()
self._setup()
logger.info("Starting master on pid %s" % self.pid)
示例7: Arbiter
class Arbiter(object):
"""Class used to control a list of watchers.
Options:
- **watchers** -- a list of Watcher objects
- **endpoint** -- the controller ZMQ endpoint
- **pubsub_endpoint** -- the pubsub endpoint
- **stats_endpoint** -- the stats endpoint. If not provided,
the *circusd-stats* process will not be launched.
- **check_delay** -- the delay between two controller points
(default: 1 s)
- **prereload_fn** -- callable that will be executed on each reload
(default: None)
- **context** -- if provided, the zmq context to reuse.
(default: None)
- **loop**: if provided, a :class:`zmq.eventloop.ioloop.IOLoop` instance
to reuse. (default: None)
- **plugins** -- a list of plugins. Each item is a mapping with:
- **use** -- Fully qualified name that points to the plugin class
- every other value is passed to the plugin in the **config** option
"""
def __init__(self, watchers, endpoint, pubsub_endpoint, check_delay=1.,
prereload_fn=None, context=None, loop=None,
stats_endpoint=None, plugins=None):
self.watchers = watchers
self.endpoint = endpoint
self.check_delay = check_delay
self.prereload_fn = prereload_fn
self.pubsub_endpoint = pubsub_endpoint
# initialize zmq context
self.context = context or zmq.Context.instance()
self.loop = loop or ioloop.IOLoop()
self.ctrl = Controller(endpoint, self.context, self.loop, self,
check_delay)
self.pid = os.getpid()
self._watchers_names = {}
self.alive = True
self._lock = RLock()
# initializing circusd-stats as a watcher when configured
self.stats_endpoint = stats_endpoint
if self.stats_endpoint is not None:
cmd = "%s -c 'from circus import stats; stats.main()'" % \
sys.executable
cmd += ' --endpoint %s' % self.endpoint
cmd += ' --pubsub %s' % self.pubsub_endpoint
cmd += ' --statspoint %s' % self.stats_endpoint
stats_watcher = Watcher('circusd-stats', cmd)
self.watchers.append(stats_watcher)
self.plugins = plugins
self._plugins = {}
def _stop_plugins(self):
for plugin in self._plugins.values():
if not plugin.running:
continue
plugin.stop()
def _start_plugins(self):
self._stop_plugins()
self._plugins.clear()
if self.plugins is None:
return
for config in self.plugins:
fqn = config['use']
del config['use']
cls = resolve_name(fqn)
instance = cls(self.context, self.endpoint,
self.pubsub_endpoint, self.check_delay,
**config)
self._plugins[cls.name] = instance
for plugin in self._plugins.values():
if not plugin.active:
continue
plugin.start()
@classmethod
def load_from_config(cls, config_file):
cfg = get_config(config_file)
# hack reload ioloop to use the monkey patched version
reload(ioloop)
watchers = []
for watcher in cfg.get('watchers', []):
watchers.append(Watcher.load_from_config(watcher))
# creating arbiter
arbiter = cls(watchers, cfg['endpoint'], cfg['pubsub_endpoint'],
check_delay=cfg.get('check_delay', 1.),
prereload_fn=cfg.get('prereload_fn'),
stats_endpoint=cfg.get('stats_endpoint'),
#.........这里部分代码省略.........
示例8: Arbiter
#.........这里部分代码省略.........
if plugins is not None:
for plugin in plugins:
fqn = plugin['use']
cmd = get_plugin_cmd(plugin, self.endpoint,
self.pubsub_endpoint, self.check_delay,
ssh_server, debug=self.debug,
loglevel=self.loglevel,
logoutput=self.logoutput)
plugin_cfg = dict(cmd=cmd, priority=1, singleton=True,
stdout_stream=self.stdout_stream,
stderr_stream=self.stderr_stream,
copy_env=True, copy_path=True,
close_child_stderr=ch_stderr,
close_child_stdout=ch_stdout)
plugin_cfg.update(plugin)
if 'name' not in plugin_cfg:
plugin_cfg['name'] = fqn
plugin_watcher = Watcher.load_from_config(plugin_cfg)
self.watchers.append(plugin_watcher)
self.sockets = CircusSockets(sockets)
self.warmup_delay = warmup_delay
@property
def running(self):
return self._running
def _init_context(self, context):
self.context = context or zmq.Context.instance()
if self.loop is None:
ioloop.install()
self.loop = ioloop.IOLoop.instance()
self.ctrl = Controller(self.endpoint, self.multicast_endpoint,
self.context, self.loop, self, self.check_delay,
self.endpoint_owner)
def get_socket(self, name):
return self.sockets.get(name, None)
def get_socket_config(self, config, name):
for i in config.get('sockets', []):
if i['name'] == name:
return i.copy()
return None
def get_watcher_config(self, config, name):
for i in config.get('watchers', []):
if i['name'] == name:
return i.copy()
return None
def get_plugin_config(self, config, name):
for i in config.get('plugins', []):
if i['name'] == name:
cfg = i.copy()
cmd = get_plugin_cmd(cfg, self.endpoint,
self.pubsub_endpoint, self.check_delay,
self.ssh_server, debug=self.debug)
cfg.update(dict(cmd=cmd, priority=1, singleton=True,
stdout_stream=self.stdout_stream,
stderr_stream=self.stderr_stream,
copy_env=True, copy_path=True))
return cfg
return None
示例9: __init__
def __init__(self, watchers, endpoint, pubsub_endpoint, check_delay=1.,
prereload_fn=None, context=None, loop=None,
stats_endpoint=None, plugins=None, sockets=None,
warmup_delay=0, httpd=False, httpd_host='localhost',
httpd_port=8080, debug=False, stream_backend='thread',
ssh_server=None, proc_name='circusd'):
self.stream_backend = stream_backend
self.watchers = watchers
self.endpoint = endpoint
self.check_delay = check_delay
self.prereload_fn = prereload_fn
self.pubsub_endpoint = pubsub_endpoint
self.proc_name = proc_name
# initialize zmq context
self.context = context or zmq.Context.instance()
self.loop = loop or ioloop.IOLoop()
self.ctrl = Controller(endpoint, self.context, self.loop, self,
check_delay)
self.pid = os.getpid()
self._watchers_names = {}
self.alive = True
self._lock = RLock()
self.debug = debug
if self.debug:
stdout_stream = stderr_stream = {'class': 'StdoutStream'}
else:
stdout_stream = stderr_stream = None
# initializing circusd-stats as a watcher when configured
self.stats_endpoint = stats_endpoint
if self.stats_endpoint is not None:
cmd = "%s -c 'from circus import stats; stats.main()'" % \
sys.executable
cmd += ' --endpoint %s' % self.endpoint
cmd += ' --pubsub %s' % self.pubsub_endpoint
cmd += ' --statspoint %s' % self.stats_endpoint
if ssh_server is not None:
cmd += ' --ssh %s' % ssh_server
if debug:
cmd += ' --log-level DEBUG'
stats_watcher = Watcher('circusd-stats', cmd, use_sockets=True,
singleton=True,
stdout_stream=stdout_stream,
stderr_stream=stderr_stream,
stream_backend=self.stream_backend,
copy_env=True, copy_path=True)
self.watchers.append(stats_watcher)
# adding the httpd
if httpd:
cmd = ("%s -c 'from circus.web import circushttpd; "
"circushttpd.main()'") % sys.executable
cmd += ' --endpoint %s' % self.endpoint
cmd += ' --fd $(circus.sockets.circushttpd)'
if ssh_server is not None:
cmd += ' --ssh %s' % ssh_server
httpd_watcher = Watcher('circushttpd', cmd, use_sockets=True,
singleton=True,
stdout_stream=stdout_stream,
stderr_stream=stderr_stream,
stream_backend=self.stream_backend,
copy_env=True, copy_path=True)
self.watchers.append(httpd_watcher)
httpd_socket = CircusSocket(name='circushttpd', host=httpd_host,
port=httpd_port)
# adding the socket
if sockets is None:
sockets = [httpd_socket]
else:
sockets.append(httpd_socket)
# adding each plugin as a watcher
if plugins is not None:
for plugin in plugins:
fqnd = plugin['use']
name = 'plugin:%s' % fqnd.replace('.', '-')
cmd = get_plugin_cmd(plugin, self.endpoint,
self.pubsub_endpoint, self.check_delay,
ssh_server, debug=self.debug)
plugin_watcher = Watcher(name, cmd, priority=1, singleton=True,
stdout_stream=stdout_stream,
stderr_stream=stderr_stream,
stream_backend=self.stream_backend,
copy_env=True, copy_path=True)
self.watchers.append(plugin_watcher)
self.sockets = CircusSockets(sockets)
self.warmup_delay = warmup_delay
示例10: Arbiter
class Arbiter(object):
"""Class used to control a list of watchers.
Options:
- **watchers** -- a list of Watcher objects
- **endpoint** -- the controller ZMQ endpoint
- **pubsub_endpoint** -- the pubsub endpoint
- **stats_endpoint** -- the stats endpoint. If not provided,
the *circusd-stats* process will not be launched.
- **check_delay** -- the delay between two controller points
(default: 1 s)
- **prereload_fn** -- callable that will be executed on each reload
(default: None)
- **context** -- if provided, the zmq context to reuse.
(default: None)
- **loop**: if provided, a :class:`zmq.eventloop.ioloop.IOLoop` instance
to reuse. (default: None)
- **plugins** -- a list of plugins. Each item is a mapping with:
- **use** -- Fully qualified name that points to the plugin class
- every other value is passed to the plugin in the **config** option
- **sockets** -- a mapping of sockets. Each key is the socket name,
and each value a :class:`CircusSocket` class. (default: None)
- **warmup_delay** -- a delay in seconds between two watchers startup.
(default: 0)
- **httpd** -- If True, a circushttpd process is run (default: False)
- **httpd_host** -- the circushttpd host (default: localhost)
- **httpd_port** -- the circushttpd port (default: 8080)
- **debug** -- if True, adds a lot of debug info in the stdout (default:
False)
- **stream_backend** -- the backend that will be used for the streaming
process. Can be *thread* or *gevent*. When set to *gevent* you need
to have *gevent* and *gevent_zmq* installed.
All watchers will use this setup unless stated otherwise in the
watcher configuration. (default: thread)
- **proc_name** -- the arbiter process name
"""
def __init__(self, watchers, endpoint, pubsub_endpoint, check_delay=1.,
prereload_fn=None, context=None, loop=None,
stats_endpoint=None, plugins=None, sockets=None,
warmup_delay=0, httpd=False, httpd_host='localhost',
httpd_port=8080, debug=False, stream_backend='thread',
ssh_server=None, proc_name='circusd'):
self.stream_backend = stream_backend
self.watchers = watchers
self.endpoint = endpoint
self.check_delay = check_delay
self.prereload_fn = prereload_fn
self.pubsub_endpoint = pubsub_endpoint
self.proc_name = proc_name
# initialize zmq context
self.context = context or zmq.Context.instance()
self.loop = loop or ioloop.IOLoop()
self.ctrl = Controller(endpoint, self.context, self.loop, self,
check_delay)
self.pid = os.getpid()
self._watchers_names = {}
self.alive = True
self._lock = RLock()
self.debug = debug
if self.debug:
stdout_stream = stderr_stream = {'class': 'StdoutStream'}
else:
stdout_stream = stderr_stream = None
# initializing circusd-stats as a watcher when configured
self.stats_endpoint = stats_endpoint
if self.stats_endpoint is not None:
cmd = "%s -c 'from circus import stats; stats.main()'" % \
sys.executable
cmd += ' --endpoint %s' % self.endpoint
cmd += ' --pubsub %s' % self.pubsub_endpoint
cmd += ' --statspoint %s' % self.stats_endpoint
if ssh_server is not None:
cmd += ' --ssh %s' % ssh_server
if debug:
cmd += ' --log-level DEBUG'
stats_watcher = Watcher('circusd-stats', cmd, use_sockets=True,
singleton=True,
stdout_stream=stdout_stream,
stderr_stream=stderr_stream,
stream_backend=self.stream_backend,
copy_env=True, copy_path=True)
self.watchers.append(stats_watcher)
# adding the httpd
if httpd:
cmd = ("%s -c 'from circus.web import circushttpd; "
"circushttpd.main()'") % sys.executable
cmd += ' --endpoint %s' % self.endpoint
cmd += ' --fd $(circus.sockets.circushttpd)'
if ssh_server is not None:
cmd += ' --ssh %s' % ssh_server
httpd_watcher = Watcher('circushttpd', cmd, use_sockets=True,
singleton=True,
stdout_stream=stdout_stream,
stderr_stream=stderr_stream,
#.........这里部分代码省略.........
示例11: Arbiter
class Arbiter(object):
"""Class used to control a list of watchers.
Options:
- **watchers**: a list of Watcher objects
- **endpoint**: the controller ZMQ endpoint
- **pubsub_endpoint**: the pubsub endpoint
- **check_delay**: the delay between two controller points (default: 1 s)
- **prereload_fn**: callable that will be executed on each reload
(default: None)
"""
def __init__(
self,
watchers,
endpoint,
pubsub_endpoint,
check_delay=1.0,
prereload_fn=None,
context=None,
loop=None,
check_flapping=True,
):
self.watchers = watchers
self.endpoint = endpoint
self.check_delay = check_delay
self.prereload_fn = prereload_fn
self.pubsub_endpoint = pubsub_endpoint
# initialize zmq context
self.context = context or zmq.Context.instance()
self.loop = loop or ioloop.IOLoop()
self.ctrl = Controller(endpoint, self.context, self.loop, self, check_delay)
self.pid = os.getpid()
self._watchers_names = {}
self.alive = True
self.busy = False
self.check_flapping = check_flapping
@debuglog
def initialize(self):
# event pub socket
self.evpub_socket = self.context.socket(zmq.PUB)
self.evpub_socket.bind(self.pubsub_endpoint)
self.evpub_socket.linger = 0
# initialize flapping
if self.check_flapping:
self.flapping = Flapping(self.context, self.endpoint, self.pubsub_endpoint, self.check_delay)
# initialize watchers
for watcher in self.watchers:
self._watchers_names[watcher.name.lower()] = watcher
watcher.initialize(self.evpub_socket)
@debuglog
def start(self):
"""Starts all the watchers.
The start command is an infinite loop that waits
for any command from a client and that watches all the
processes and restarts them if needed.
"""
logger.info("Starting master on pid %s", self.pid)
self.initialize()
# start controller
self.ctrl.start()
# start flapping
if self.check_flapping:
logger.debug("Starting flapping")
self.flapping.start()
# initialize processes
logger.debug("Initializing watchers")
for watcher in self.watchers:
watcher.start()
logger.info("Arbiter now waiting for commands")
while True:
try:
self.loop.start()
except zmq.ZMQError as e:
if e.errno == errno.EINTR:
continue
else:
raise
else:
break
if self.check_flapping:
self.flapping.stop()
self.ctrl.stop()
self.evpub_socket.close()
#.........这里部分代码省略.........
示例12: Arbiter
#.........这里部分代码省略.........
sockets = [httpd_socket]
else:
sockets.append(httpd_socket)
# adding each plugin as a watcher
ch_stderr = self.stderr_stream is None
ch_stdout = self.stdout_stream is None
if plugins is not None:
for plugin in plugins:
fqn = plugin['use']
cmd = get_plugin_cmd(plugin, self.endpoint,
self.pubsub_endpoint, self.check_delay,
ssh_server, debug=self.debug)
plugin_cfg = dict(cmd=cmd, priority=1, singleton=True,
stdout_stream=self.stdout_stream,
stderr_stream=self.stderr_stream,
copy_env=True, copy_path=True,
close_child_stderr=ch_stderr,
close_child_stdout=ch_stdout)
plugin_cfg.update(plugin)
if 'name' not in plugin_cfg:
plugin_cfg['name'] = fqn
plugin_watcher = Watcher.load_from_config(plugin_cfg)
self.watchers.append(plugin_watcher)
self.sockets = CircusSockets(sockets)
self.warmup_delay = warmup_delay
def _init_context(self, context):
self.context = context or zmq.Context.instance()
self.loop = ioloop.IOLoop.instance()
self.ctrl = Controller(self.endpoint, self.multicast_endpoint,
self.context, self.loop, self, self.check_delay)
def get_socket(self, name):
return self.sockets.get(name, None)
def get_socket_config(self, config, name):
for i in config.get('sockets', []):
if i['name'] == name:
return i.copy()
return None
def get_watcher_config(self, config, name):
for i in config.get('watchers', []):
if i['name'] == name:
return i.copy()
return None
def get_plugin_config(self, config, name):
for i in config.get('plugins', []):
if i['name'] == name:
cfg = i.copy()
cmd = get_plugin_cmd(cfg, self.endpoint,
self.pubsub_endpoint, self.check_delay,
self.ssh_server, debug=self.debug)
cfg.update(dict(cmd=cmd, priority=1, singleton=True,
stdout_stream=self.stdout_stream,
stderr_stream=self.stderr_stream,
copy_env=True, copy_path=True))
return cfg
return None
示例13: Trainer
class Trainer(object):
def __init__(self, shows, endpoint, check_delay=1.0, ipc_path=None, prereload_fn=None):
self.shows = shows
self.endpoint = endpoint
self.check_delay = check_delay
self.ipc_path = ipc_path
self.prereload_fn = prereload_fn
self.ctrl = Controller(endpoint, self, self.check_delay, self.ipc_path)
self.pid = os.getpid()
self._shows_names = {}
self.alive = True
self._lock = Lock()
self.setup()
logger.info("Starting master on pid %s" % self.pid)
def setup(self):
for show in self.shows:
self._shows_names[show.name] = show
def start(self):
# launch flies
for show in self.shows:
show.manage_flies()
while self.alive:
# manage and reap flies
for show in self.shows:
show.reap_flies()
show.manage_flies()
# wait for the controller
self.ctrl.poll()
def stop(self, graceful=True):
self.alive = False
# kill flies
for show in self.shows:
show.stop(graceful=graceful)
self.ctrl.stop()
def reload(self):
if self.prereload_fn is not None:
self.prereload_fn(self)
# reopen log files
for handler in logger.handlers:
if isinstance(handler, logging.FileHandler):
handler.acquire()
handler.stream.close()
handler.stream = open(handler.baseFilename, handler.mode)
handler.release()
# gracefully reload shows
for show in self.shows:
show.reload()
def num_flies(self):
return sum([len(show) for show in self.shows])
def num_shows(self):
return len(self.shows)
def get_show(self, name):
return self._shows_names[name]
def add_show(self, show):
with self._lock:
if show.name in self._shows_names:
raise AlreadyExist("%r already exist" % show.name)
self.shows.append(show)
self._shows_names[show.name] = show
def del_show(self, name):
with self._lock:
# remove the show from the list
show = self._shows_names.pop(name)
del self.shows[self.shows.index(show)]
# stop the show
show.stop()
###################
# commands
###################
def handle_numflies(self):
return str(self.num_flies())
def handle_numshows(self):
return str(self.num_shows())
def handle_shows(self):
return ",".join(self._shows_names.keys())
def handle_flies(self):
flies = []
for show in self.shows:
flies.append("%s: %s" % (show.name, show.handle_flies()))
#.........这里部分代码省略.........
示例14: Arbiter
class Arbiter(object):
"""Class used to control a list of watchers.
Options:
- **watchers** -- a list of Watcher objects
- **endpoint** -- the controller ZMQ endpoint
- **pubsub_endpoint** -- the pubsub endpoint
- **check_delay** -- the delay between two controller points
(default: 1 s)
- **prereload_fn** -- callable that will be executed on each reload
(default: None)
- **context** -- if provided, the zmq context to reuse.
(default: None)
- **loop**: if provided, a :class:`zmq.eventloop.ioloop.IOLoop` instance
to reuse. (default: None)
- **check_flapping** -- when True, Circus will check for flapping
processes and automatically restart them. (default: True)
"""
def __init__(self,
watchers,
endpoint,
pubsub_endpoint,
check_delay=1.,
prereload_fn=None,
context=None,
loop=None,
check_flapping=True):
self.watchers = watchers
self.endpoint = endpoint
self.check_delay = check_delay
self.prereload_fn = prereload_fn
self.pubsub_endpoint = pubsub_endpoint
# initialize zmq context
self.context = context or zmq.Context.instance()
self.loop = loop or ioloop.IOLoop()
self.ctrl = Controller(endpoint, self.context, self.loop, self,
check_delay)
self.pid = os.getpid()
self._watchers_names = {}
self.alive = True
self.busy = False
self.check_flapping = check_flapping
@classmethod
def load_from_config(cls, config_file):
cfg = get_config(config_file)
# hack reload ioloop to use the monkey patched version
reload(ioloop)
watchers = []
for watcher in cfg.get('watchers', []):
watchers.append(Watcher.load_from_config(watcher))
# creating arbiter
arbiter = cls(
watchers,
cfg['endpoint'],
cfg['pubsub_endpoint'],
check_delay=cfg.get('check_delay', 1.),
prereload_fn=cfg.get('prereload_fn'))
return arbiter
@debuglog
def initialize(self):
# set process title
_setproctitle("circusd")
# event pub socket
self.evpub_socket = self.context.socket(zmq.PUB)
self.evpub_socket.bind(self.pubsub_endpoint)
self.evpub_socket.linger = 0
# initialize flapping
if self.check_flapping:
self.flapping = Flapping(self.context, self.endpoint,
self.pubsub_endpoint, self.check_delay)
# initialize watchers
for watcher in self.watchers:
self._watchers_names[watcher.name.lower()] = watcher
watcher.initialize(self.evpub_socket)
@debuglog
def start(self):
"""Starts all the watchers.
The start command is an infinite loop that waits
for any command from a client and that watches all the
processes and restarts them if needed.
"""
logger.info("Starting master on pid %s", self.pid)
self.initialize()
#.........这里部分代码省略.........
示例15: Trainer
class Trainer(object):
def __init__(self, shows, endpoint, check_delay=1., ipc_path=None):
self.shows = shows
self.endpoint = endpoint
self.check_delay = check_delay
self.ipc_path = ipc_path
self.ctrl = Controller(endpoint, self, self.check_delay,
self.ipc_path)
self.pid = os.getpid()
self._shows_names = {}
self.alive = True
self._lock = Lock()
self.setup()
logger.info("Starting master on pid %s" % self.pid)
def setup(self):
for show in self.shows:
self._shows_names[show.name] = show
def start(self):
# launch flies
for show in self.shows:
show.manage_flies()
while self.alive:
# manage and reap flies
for show in self.shows:
show.reap_flies()
show.manage_flies()
# wait for the controller
self.ctrl.poll()
def stop(self):
self.alive = False
# kill flies
for show in self.shows:
show.stop()
self.ctrl.stop()
def num_flies(self):
return sum([len(show) for show in self.shows])
def num_shows(self):
return len(self.shows)
def get_show(self, name):
return self._shows_names[name]
def add_show(self, show):
with self._lock:
if show.name in self._shows_names:
raise AlreadyExist("%r already exist" % show.name)
self.shows.append(show)
self._shows_names[show.name] = show
def del_show(self, name):
with self._lock:
# remove the show from the list
show = self._shows_names.pop(name)
del self.shows[self.shows.index(show)]
# stop the show
show.stop()
###################
# commands
###################
def handle_shows(self):
return ",".join(self._shows_names.keys())
def handle_flies(self):
flies = []
for show in self.shows:
flies.append("%s: %s" % (show.name, show.handle_flies()))
return buffer("\n".join(flies))
def handle_info_shows(self):
infos = []
for show in self.shows:
infos.append("%s:\n" % show.name)
infos.append("%s\n" % show.handle_info())
return buffer("".join(infos))
def handle_reload(self):
return "ok"
def handle_winch(self):
"SIGWINCH handling"
if os.getppid() == 1 or os.getpgrp() != os.getpid():
for show in self.shows:
show.num_flies = 0
show.kill_flies()
else:
# SIGWINCH ignored. Not daemonized
pass
#.........这里部分代码省略.........