def restart_with_reloader(): to_mon = [] while 1: _log('info', ' * Clastic restarting with reloader') args = [sys.executable] + sys.argv new_environ = os.environ.copy() new_environ['WERKZEUG_RUN_MAIN'] = 'true' if os.name == 'nt': for key, value in new_environ.iteritems(): if isinstance(value, unicode): new_environ[key] = value.encode('iso-8859-1') stderr_buff = [] child_proc = subprocess.Popen(args, env=new_environ, stderr=subprocess.PIPE) rf = child_proc.stderr exit_code, lines = None, [] while exit_code is None or lines: if child_proc.poll() is None: lines.append(rf.readline()) elif exit_code is None: lines.extend(rf.readlines()) exit_code = child_proc.returncode if not lines: break cur_line = lines.pop(0) if cur_line.startswith(_MON_PREFIX): to_mon = literal_eval(cur_line[len(_MON_PREFIX):]) else: sys.stderr.write(cur_line) stderr_buff.append(cur_line) if len(stderr_buff) > _STDERR_BUFF_SIZE: stderr_buff.pop(0) if exit_code == 3: continue elif exit_code == 1 and stderr_buff: enable_tty_echo() from clastic import flaw tb_str = ''.join(stderr_buff) err_app = flaw.create_app(tb_str, to_mon) # TODO: these values should be passed through err_server = make_server('localhost', 5000, err_app) thread.start_new_thread(err_server.serve_forever, ()) try: reloader_loop(to_mon, 1) except KeyboardInterrupt: return 0 except SystemExit as se: if se.code == 3: continue return se.code finally: err_server.shutdown() err_server.server_close() return 0 else: return exit_code
def run_with_reloader(*args): """Run the given function in an independent python interpreter.""" import signal signal.signal(signal.SIGTERM, lambda *args: sys.exit(0)) if os.environ.get('WERKZEUG_RUN_MAIN') == 'true': thread.start_new_thread(main_func, (args)) try: reloader_loop(extra_files, interval) except KeyboardInterrupt: return try: sys.exit(restart_with_reloader()) except KeyboardInterrupt: pass
def restart_with_reloader(error_func=None): to_mon = [] while 1: _log('info', ' * Clastic restarting with reloader') args = [sys.executable] + sys.argv new_environ = os.environ.copy() new_environ['WERKZEUG_RUN_MAIN'] = 'true' if os.name == 'nt': for key, value in new_environ.iteritems(): if isinstance(value, unicode): new_environ[key] = value.encode('iso-8859-1') child_proc = subprocess.Popen(args, env=new_environ, stderr=subprocess.PIPE) stderr_buff = deque(maxlen=_STDERR_BUFF_SIZE) def consume_lines(): for line in iter(child_proc.stderr.readline, ''): if line.startswith(_MON_PREFIX): to_mon[:] = literal_eval(line[len(_MON_PREFIX):]) else: sys.stderr.write(line) stderr_buff.append(line) while child_proc.poll() is None: consume_lines() consume_lines() exit_code = child_proc.returncode if exit_code == 3: continue elif error_func and exit_code == 1 and stderr_buff: enable_tty_echo() tb_str = ''.join(stderr_buff) err_server = error_func(tb_str, to_mon) try: reloader_loop(to_mon, 1) except KeyboardInterrupt: return 0 except SystemExit as se: if se.code == 3: continue return se.code finally: err_server.shutdown() err_server.server_close() return 0 else: return exit_code
def run_with_reloader(main_func, extra_files=None, interval=1, error_func=None): signal.signal(signal.SIGTERM, lambda *args: sys.exit(0)) enable_tty_echo() if os.environ.get('WERKZEUG_RUN_MAIN') == 'true': thread.start_new_thread(main_func, ()) try: reloader_loop(extra_files, interval) except KeyboardInterrupt: return except SystemExit: mon_list = list(chain(iter_monitor_files(), extra_files or ())) sys.stderr.write('%s%r\n' % (_MON_PREFIX, mon_list)) raise try: sys.exit(restart_with_reloader(error_func=error_func)) except KeyboardInterrupt: pass
def run_with_reloader(main_func, extra_files=None, interval=1): signal.signal(signal.SIGTERM, lambda *args: sys.exit(0)) enable_tty_echo() if os.environ.get('WERKZEUG_RUN_MAIN') == 'true': thread.start_new_thread(main_func, ()) try: reloader_loop(extra_files, interval) except KeyboardInterrupt: return except SystemExit: mon_list = list(chain(iter_monitor_files(), extra_files or ())) sys.stderr.write(_MON_PREFIX) sys.stderr.write(repr(mon_list)) raise try: sys.exit(restart_with_reloader()) except KeyboardInterrupt: pass