Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
if args.version:
print(__version__)
sys.exit(0)
if args.plugin is None:
parser.print_usage()
sys.exit(0)
factory = resolve_name(args.plugin)
# configure the logger
configure_logger(logger, args.loglevel, args.logoutput, name=factory.name)
# load the plugin and run it.
logger.info('Loading the plugin...')
logger.info('Endpoint: %r' % args.endpoint)
logger.info('Pub/sub: %r' % args.pubsub)
plugin = factory(args.endpoint, args.pubsub,
args.check_delay, args.ssh,
**_str2cfg(args.config))
logger.info('Starting')
try:
plugin.start()
except KeyboardInterrupt:
pass
finally:
logger.info('Stopping')
plugin.stop()
sys.exit(0)
""" reload
"""
if not graceful and sequential:
logger.warn("with graceful=False, sequential=True is ignored")
if self.prereload_fn is not None:
self.prereload_fn(self)
if not graceful:
yield self._restart()
return
if self.is_stopped():
yield self._start()
elif self.send_hup:
for process in self.processes.values():
logger.info("SENDING HUP to %s" % process.pid)
process.send_signal(signal.SIGHUP)
else:
if sequential:
active_processes = self.get_active_processes()
for process in active_processes:
yield self.kill_process(process)
self.reap_process(process.pid)
self.spawn_process()
yield tornado_sleep(self.warmup_delay)
else:
for i in range(self.numprocesses):
self.spawn_process()
yield self.manage_processes()
self.notify_event("reload", {"time": time.time()})
logger.info('%s reloaded', self.name)
def __stop(self, for_shutdown=False):
logger.info('Arbiter exiting')
self._stopping = True
yield self._stop_watchers(close_output_streams=True,
for_shutdown=for_shutdown)
if self._provided_loop:
cb = self.stop_controller_and_close_sockets
self.loop.add_callback(cb)
else:
# stop_controller_and_close_sockets will be
# called in the end of start() method
self.loop.add_callback(self.loop.stop)
logger.info("%s: flapping detected: retry in %2ds "
"(attempt number %s)", watcher_name,
self._get_conf(conf, 'retry_in'), next_tries)
self.cast("stop", name=watcher_name)
self.timelines[watcher_name] = []
self.tries[watcher_name] = next_tries
def _start():
self.cast("start", name=watcher_name)
timer = Timer(self._get_conf(conf, 'retry_in'), _start)
timer.start()
self.timers[watcher_name] = timer
else:
logger.info(
"%s: flapping detected: reached max retry limit",
watcher_name)
self.timelines[watcher_name] = []
self.tries[watcher_name] = 0
self.cast("stop", name=watcher_name)
else:
self.timelines[watcher_name] = []
self.tries[watcher_name] = 0
def look_after(self):
"""Checks for the watchdoged watchers and restart a process if no
received watchdog after the loop_rate * max_count period.
"""
# if first check, do a full discovery first.
if self.starting:
self._discover_monitored_pids()
self.starting = False
max_timeout = self.loop_rate * self.max_count
too_old_time = time.time() - max_timeout
for pid, detail in self.pid_status.items():
if detail['last_activity'] < too_old_time:
logger.info("watcher:%s, pid:%s is not responding. Kill it !",
detail['watcher'],
pid)
props = dict(name=detail['watcher'], pid=int(pid))
if self.stop_signal is not None:
props['signum'] = self.stop_signal
if self.graceful_timeout is not None:
props['graceful_timeout'] = self.graceful_timeout
self.cast('kill', **props)
# Trusting watcher to eventually stop the process after
# graceful timeout
del self.pid_status[pid]
def clean_stop(watcher, arbiter, hook_name, pid, signum, **kwargs):
if len(watcher.processes) > watcher.numprocesses and signum == signal.SIGQUIT:
name = watcher.name
started = watcher.processes[pid].started
newer_pids = [p for p, w in watcher.processes.items() if p != pid and w.started > started]
# if the one being stopped is actually the newer one, just do it
if len(newer_pids) < watcher.numprocesses:
return True
wid = watcher.processes[pid].wid
base_port = int(watcher._options.get('stats_base_port', 8090))
logger.info('%s pausing', name)
try:
watcher.send_signal(pid, signal.SIGTSTP)
wait_for_workers(name, wid, base_port, 'paused')
logger.info('%s workers idle', name)
except Exception as e:
logger.error('trouble pausing %s: %s', name, e)
return True
self.call_hook('before_stop')
yield self.kill_processes()
self.reap_processes()
# stop redirectors
if self.stream_redirector:
self.stream_redirector.stop()
self.stream_redirector = None
if close_output_streams:
if self.stdout_stream and hasattr(self.stdout_stream, 'close'):
self.stdout_stream.close()
if self.stderr_stream and hasattr(self.stderr_stream, 'close'):
self.stderr_stream.close()
# notify about the stop
if skip:
logger.info('%s left running in papa', self.name)
else:
if self.evpub_socket is not None:
self.notify_event("stop", {"time": time.time()})
self._status = "stopped"
# We ignore the hook result
self.call_hook('after_stop')
logger.info('%s stopped', self.name)
self.stream_redirector = None
if close_output_streams:
if self.stdout_stream and hasattr(self.stdout_stream, 'close'):
self.stdout_stream.close()
if self.stderr_stream and hasattr(self.stderr_stream, 'close'):
self.stderr_stream.close()
# notify about the stop
if skip:
logger.info('%s left running in papa', self.name)
else:
if self.evpub_socket is not None:
self.notify_event("stop", {"time": time.time()})
self._status = "stopped"
# We ignore the hook result
self.call_hook('after_stop')
logger.info('%s stopped', self.name)
def __init__(self, controller):
self.controller = controller
# init signals
logger.info('Registering signals...')
self._old = {}
self._register()
This should be done only at startup time, because if new watchers or
pids are created in running time, we should receive the message
from circusd which is handled by self.handle_recv
"""
self.pid_status = dict()
all_watchers = self.call("list")
for watcher_name in all_watchers['watchers']:
if self._match_watcher_name(watcher_name):
processes = self.call("list", name=watcher_name)
if 'pids' in processes:
for pid in processes['pids']:
pid = str(pid)
self.pid_status[pid] = dict(watcher=watcher_name,
last_activity=time.time())
logger.info("discovered: %s, pid:%s",
watcher_name,
pid)