def __init__(self): settings = dict( static_path = os.path.join(os.path.dirname(__file__), 'static'), template_path = os.path.join(os.path.dirname(__file__), 'templates'), autoescape = None, debug = options.debug, gzip = True ) handlers = [ (r'/', IndexHandler), (r'/_hcheck.hdn', HCheckHandler), (r'/etagger', EtaggerHandler), (r'/etaggertest', EtaggerTestHandler), ] tornado.web.Application.__init__(self, handlers, **settings) autoreload.add_reload_hook(self.finalize) self.log = setupAppLogger() ppid = os.getpid() self.ppid = ppid self.log.info('initialize parent process[%s] ... done' % (ppid)) ############################################################################################### # create etagger config only once self.config = Config(options, is_training=False, emb_class=options.emb_class, use_crf=True) self.log.info('initialize config on parent process[%s] ... done' % (ppid)) # create nlp(spacy) only once self.nlp = spacy.load('en') self.log.info('initialize spacy on parent process[%s] ... done' % (ppid)) ############################################################################################### log.info('http start...')
def __init__(self): settings = dict( static_path = os.path.join(os.path.dirname(__file__), 'static'), template_path = os.path.join(os.path.dirname(__file__), 'templates'), autoescape = None, debug = options.debug, gzip = True ) handlers = [ (r'/', IndexHandler), (r'/_hcheck.hdn', HCheckHandler), (r'/dragnn', DragnnHandler), (r'/dragnntest', DragnnTestHandler), ] tornado.web.Application.__init__(self, handlers, **settings) autoreload.add_reload_hook(self.finalize) self.log = setupAppLogger() ppid = os.getpid() self.log.info('initialize parent process[%s] ...' % (ppid)) self.ppid = ppid self.enable_tracing = options.enable_tracing # import konlpy if enabled self.enable_konlpy = options.enable_konlpy self.komoran = None if options.enable_konlpy : from konlpy.tag import Komoran komoran = Komoran() self.komoran = komoran self.log.info('initialize parent process[%s] ... done' % (ppid)) log.info('start http start...')
def sputnik_init(_logging_config, debug=False, cache_config=None, spusys_config=None, assert_config=default_assert_config): """ Sputnik Init """ global G_DEBUG global G_INIT if G_INIT: return G_INIT = True G_DEBUG = debug SpuLogging.setting(_logging_config) autoreload.add_reload_hook(_autoreload_remove_db_connect) global global_assert_config global_assert_config = assert_config enable_pretty_logging() if cache_config: from SpuCacheManager import SpuCacheManager SpuCacheManager.setting(cache_config, debug) if spusys_config: import SpuSys SpuSys.setting(spusys_config, debug)
def main(): rospy.init_node('mycroft_message_bus') rospy.loginfo(rospy.get_caller_id() + " started") import tornado.options reset_sigint_handler() lock = Lock("service") tornado.options.parse_command_line() def reload_hook(): """ Hook to release lock when autoreload is triggered. """ lock.delete() autoreload.add_reload_hook(reload_hook) config = Configuration.get().get("websocket") host = config.get("host") port = config.get("port") route = config.get("route") validate_param(host, "websocket.host") validate_param(port, "websocket.port") validate_param(route, "websocket.route") routes = [(route, WebsocketEventHandler)] application = web.Application(routes, **settings) application.listen(port, host) create_daemon(ioloop.IOLoop.instance().start) wait_for_exit_signal() rospy.spin()
def main(): import tornado.options lock = Lock("service") tornado.options.parse_command_line() def reload_hook(): """ Hook to release lock when autoreload is triggered. """ lock.delete() autoreload.add_reload_hook(reload_hook) config = ConfigurationManager.get().get("websocket") host = config.get("host") port = config.get("port") route = config.get("route") validate_param(host, "websocket.host") validate_param(port, "websocket.port") validate_param(route, "websocket.route") routes = [ (route, WebsocketEventHandler) ] application = web.Application(routes, **settings) application.listen(port, host) ioloop.IOLoop.instance().start()
def __init__(self): settings = dict( static_path = os.path.join(os.path.dirname(__file__), 'static'), template_path = os.path.join(os.path.dirname(__file__), 'templates'), autoescape = None, debug = options.debug, gzip = True ) handlers = [ (r'/', IndexHandler), (r'/_hcheck.hdn', HCheckHandler), (r'/etagger', EtaggerHandler), (r'/etaggertest', EtaggerTestHandler), ] tornado.web.Application.__init__(self, handlers, **settings) autoreload.add_reload_hook(self.finalize) self.log = setupAppLogger() ppid = os.getpid() self.ppid = ppid self.log.info('initialize parent process[%s] ... done' % (ppid)) ############################################################################################### # save Etagger(python instance) for passing to handlers. self.Etagger = Etagger # create nlp(spacy) only once. self.nlp = spacy.load('en') self.log.info('initialize spacy on parent process[%s] ... done' % (ppid)) ############################################################################################### log.info('http start...')
def __init__(self): settings = dict(static_path=os.path.join(os.path.dirname(__file__), 'static'), template_path=os.path.join(os.path.dirname(__file__), 'templates'), autoescape=None, debug=options.debug, gzip=True) handlers = [(r'/', IndexHandler), (r'/_hcheck.hdn', HCheckHandler), (r'/LNG', LNGHandler), (r'/LNG_reload_dic', LNGReloadDicHandler)] tornado.web.Application.__init__(self, handlers, **settings) autoreload.add_reload_hook(self.finalize) self.log = setupAppLogger() self.log.info('initialize...') self.log.info('tornado.version' + str(tornado.version_info)) self.lng = search_lotto_number.LNG('./data/lotto_statistics.json') self.localhost_ip = socket.gethostbyname(socket.gethostname()) self.log.info('initialize... done') log.info('start http start...')
def show( markdown_file: Path, host: str = 'localhost', port: int = 8123, no_warmup: bool = False, ): """ Visualize your presentation (default). """ markdown_file = Path(markdown_file) config = load_config() # Initial generation generate(markdown_file, no_warmup=no_warmup) observer = Observer() url = 'http://{host}:{port}'.format(host=host, port=port) reload_url = url + '/forcereload' ignore_regexes = [r'.*/\.[^/]*'] handler = Handler(regexes=['.*'], ignore_regexes=ignore_regexes, ignore_directories=True) handler.configure(markdown_file, reload_url) observer.schedule(handler, '.', recursive=True) observer.start() server = Server() server.root = str(config['output_path']) server.application(port, host, liveport=None, debug=True, live_css=True) threading.Thread(target=webbrowser.open, args=(url, )).start() add_reload_hook(lambda: IOLoop.instance().close(all_fds=True)) IOLoop.instance().start()
def pool_init_processes(pool_size, debug=False): micro_pool = ProcessPoolExecutor(pool_size) pool_init(micro_pool) if debug is True: add_reload_hook(micro_pool.shutdown) logging.info("pool intialized with %s processes", pool_size) return micro_pool
def start_thememapper(): global nav global mapper #initialize the necessary classes mapper = Mapper(get_settings()) nav = Navigation() # Adds the ability to set config file and port through commandline p = optparse.OptionParser() p.add_option('--port', '-p', default=mapper.port,help='port thememapper should run at') p.add_option('--diazo', '-d', default=False,action="store_true",dest="diazo",help='force diazo server to run') p.add_option('--diazo_port', '-f', default=mapper.diazo_port,help='port diazo should run at') options = p.parse_args()[0] mapper.port = options.port mapper.diazo_port = options.diazo_port #start thememapper print "Starting thememapper on http://0.0.0.0:" + mapper.port HTTPServer(WSGIContainer(app)).listen(mapper.port) if options.diazo or mapper.diazo_run == 'True': try: from thememapper.diazo import server print "Starting diazo on http://0.0.0.0:" + mapper.diazo_port HTTPServer(server.get_application(mapper)).listen(mapper.diazo_port) except ImportError: print "You will need to install thememapper.diazo before being able to use this function." ioloop = IOLoop.instance() autoreload.watch(os.path.join(os.path.dirname(__file__), 'settings.properties')) autoreload.add_reload_hook(reload) autoreload.start(ioloop) ioloop.start()
def __init__(self, config, watch, web_root, cache_index, debug=False): autoreload.watch(config) autoreload.watch(watch) autoreload.add_reload_hook(self.before_reload_done) self.log('config ' + config) self.log('watch ' + watch) self.load_config(config) self.load_config(watch) if not os.path.exists(os.path.dirname(cache_index)): os.makedirs(os.path.dirname(cache_index)) self.settings['web_root'] = web_root self.cache_index = cache_index self.debug = debug if self.debug: self.log("debug is enabled") self.log("config used: " + config) self.log("web root: " + web_root) self.log("cache index: " + cache_index) self.cache = self.load_cache() if self.settings['css_inlined'] != '': self.settings['css_files'].append(self.settings['css_inlined']) for f in self.settings['css_files']: self.log('watch css ' + f) autoreload.watch(self.get_file_path(f)) self.settings['js_files'].append(self.settings['js_loader']) for f in self.settings['js_files']: self.log('watch js [' + f['name'] + '] ' + f['file']) if f['name'] != 'loader': autoreload.watch(self.get_file_path(f['file'])) else: autoreload.watch(f['file']) if 'preload_templates' in self.settings: loader = template.Loader(self.settings['templates_dir']) for f in self.settings['preload_templates']: self.log('watch template ' + f) autoreload.watch(self.settings['templates_dir'] + f) self.templates.append({'file': f, 'template': loader.load(f)}) if not os.path.exists(os.path.dirname(self.settings['css_min_dir'])): os.makedirs(os.path.dirname(self.settings['css_min_dir'])) if not os.path.exists(os.path.dirname(self.settings['js_min_dir'])): os.makedirs(os.path.dirname(self.settings['js_min_dir'])) self.recompile() self.cache_css_inlined = self.get_inlined_css() self.cache_css_js_loader = self.get_loader()
def serve(self, port=5500, liveport=None, host=None, root=None, debug=None, open_url=False, restart_delay=2, open_url_delay=None, live_css=True, default_filename='index.html'): """Start serve the server with the given port. :param port: serve on this port, default is 5500 :param liveport: live reload on this port :param host: serve on this hostname, default is 127.0.0.1 :param root: serve static on this root directory :param debug: set debug mode, which autoreloads the app on code changes via Tornado (and causes polling). Defaults to True when ``self.app`` is set, otherwise False. :param open_url_delay: open webbrowser after the delay seconds :param live_css: whether to use live css or force reload on css. Defaults to True :param default_filename: launch this file from the selected root on startup """ host = host or '127.0.0.1' if root is not None: self.root = root self._setup_logging() logger.info(f'Serving on http://{host}:{port}') self.default_filename = default_filename self.application(port, host, liveport=liveport, debug=debug, live_css=live_css) # Async open web browser after 5 sec timeout if open_url: logger.error('Use `open_url_delay` instead of `open_url`') if open_url_delay is not None: def opener(): time.sleep(open_url_delay) webbrowser.open(f'http://{host}:{port}') threading.Thread(target=opener).start() try: self.watcher._changes.append(('__livereload__', restart_delay)) LiveReloadHandler.start_tasks() add_reload_hook(lambda: IOLoop.instance().close(all_fds=True)) IOLoop.instance().start() except KeyboardInterrupt: logger.info('Shutting down...')
def new_application(): mappings = [('/res/(.*)', StaticFileHandler, { 'path': os.path.join(os.path.dirname(__file__), 'res') })] + MAPPINGS application = Application( mappings, cookie_secret="__theEOCDB_secretEncryptionString__") autoreload.add_reload_hook('ocdb/core/res/validation_config.json') autoreload.start() return application
def start_diazo_server(settings): app = get_application(settings) if app is not False: print "Starting diazo on http://0.0.0.0:" + settings['diazo_port'] HTTPServer(app).listen(settings['diazo_port']) ioloop = IOLoop.instance() autoreload.add_reload_hook(reload) autoreload.start(ioloop) ioloop.start()
def run( self ): port = RESTConf.port() if self.__sslops: url = "https://0.0.0.0:%s" % port else: url = "http://0.0.0.0:%s" % port gLogger.always( "Starting REST server on %s" % url ) autoreload.add_reload_hook( self.__reloadAppCB ) ioloop.IOLoop.instance().start()
def __init__(self): current_path = os.path.dirname(__file__) settings = dict( debug=options.debug, autoreload=True, template_path=os.path.join(current_path, "templates"), static_path=os.path.join(current_path, "static"), cookie_secret=options.cookie_secret, login_url='user/login', ) handlers = [ (r'/', IndexHandler), (r'/save', IndexHandler), (r'/user/login', UserHandler), (r'/qrcode', QrcodeHandler), ] if options.wx_token is None: logging.info('wx_token is not configured') else: handlers.append((r'/weixin', WeiXinHandler)) if options.wx_app_id is None or options.wx_secret is None or options.wx_template_id is None: logging.info( 'wx_app_id/wx_secret/wx_template_id is not configured') else: Shadowsocks.add_password_callback( lambda ss: WeiXin.send_ss_info(ss)) RequestHandler.set_default_headers = App._set_default_header try: ss_config = Shadowsocks.read_config() except FileNotFoundError as e: print('can\'t found ' + e.filename) sys.exit(e.errno) Shadowsocks.workers = [ Shadowsocks(i, ss_config) for i in range(options.workers) ] for index in ss_config['running']: Shadowsocks.workers[index].start() super(App, self).__init__(handlers, **settings) logging.info("listening on http://localhost:%s" % options.port) self.reset_timer = None if options.password_timeout > 0: self._reset_timer_callback() from tornado import autoreload autoreload.add_reload_hook(App._stop_all_worker)
def main(): logger.info("服务器监听在" + const.basic.get("host", "") + ":" + str(options.port)) logger.info("部署的环境是:" + options.env) logger.info("debug:" + str(settings["debug"])) add_reload_hook(do_before_autoreload) from tornado.httpserver import HTTPServer global server server = HTTPServer(app, xheaders=True) server.listen(port=options.port, address=const.basic.get("host", "")) IOLoop.current().start()
def start(host, port, config): """ Entry point for application. This setups IOLoop, load classes and run HTTP server """ app = App.instance(config) ioloop = IOLoop.instance() http_server = HTTPServer(app, xheaders=True) http_server.listen(port, host) logger.info('Running PID {pid} @ http://{host}:{port}'.format( host=host, pid=os.getpid(), port=port)) def _reload(): """ reload Python code should also clear cache """ Assets.reset() with Request._template_loader_lock: for loader in Request._template_loaders.values(): loader.reset() add_reload_hook(_reload) if config.graceful: def _graceful(): """ Schedule shutdown on next tick """ def _shutdown(): http_server.stop() ioloop.stop() logger.info('Stopped') ioloop.call_later(1, _shutdown) def _on_term(*args): ioloop.add_callback_from_signal(_graceful) # Automatically kill if anything blocks the process signal.signal(signal.SIGTERM, _on_term) signal.signal(signal.SIGINT, _on_term) ioloop.set_blocking_log_threshold(1) time_to_kill = config['env']['app'].get('kill_blocking_sec', 2) ioloop.set_blocking_signal_threshold(time_to_kill, action=None) try: Event.get('start').emit(app) ioloop.start() except KeyboardInterrupt: logger.info('Bye.') except Exception as e: logger.exception(e) logger.info('Server was down.')
def __init__(self, config, watch, web_root, cache_index, debug=False): autoreload.watch(config) autoreload.watch(watch) autoreload.add_reload_hook(self.before_reload_done) self.log('config '+config) self.log('watch '+watch) self.load_config(config) self.load_config(watch) if not os.path.exists(os.path.dirname(cache_index)): os.makedirs(os.path.dirname(cache_index)) self.settings['web_root'] = web_root self.cache_index = cache_index self.debug = debug if self.debug: self.log("debug is enabled") self.log("config used: "+config) self.log("web root: "+web_root) self.log("cache index: "+cache_index) self.cache = self.load_cache() if self.settings['css_inlined']!='': self.settings['css_files'].append(self.settings['css_inlined']) for f in self.settings['css_files']: self.log('watch css '+f) autoreload.watch(self.get_file_path(f)) self.settings['js_files'].append(self.settings['js_loader']) for f in self.settings['js_files']: self.log('watch js ['+f['name']+'] '+f['file']) if f['name']!='loader': autoreload.watch(self.get_file_path(f['file'])) else: autoreload.watch(f['file']) if 'preload_templates' in self.settings: loader = template.Loader(self.settings['templates_dir']) for f in self.settings['preload_templates']: self.log('watch template '+f) autoreload.watch(self.settings['templates_dir']+f) self.templates.append({'file':f,'template':loader.load(f)}) self.recompile() self.cache_css_inlined = self.get_inlined_css() self.cache_css_js_loader = self.get_loader()
def __init__(self): current_path = os.path.dirname(__file__) settings = dict( debug=options.debug, autoreload=True, template_path=os.path.join(current_path, "templates"), static_path=os.path.join(current_path, "static"), cookie_secret=options.cookie_secret, login_url='user/login', ) handlers = [ (r'/', IndexHandler), (r'/save', IndexHandler), (r'/user/login', UserHandler), (r'/qrcode', QrcodeHandler), ] if options.wx_token is None: logging.info('wx_token is not configured') else: handlers.append((r'/weixin', WeiXinHandler)) if options.wx_app_id is None or options.wx_secret is None or options.wx_template_id is None: logging.info('wx_app_id/wx_secret/wx_template_id is not configured') else: Shadowsocks.add_password_callback(lambda ss: WeiXin.send_ss_info(ss)) RequestHandler.set_default_headers = App._set_default_header try: ss_config = Shadowsocks.read_config() except FileNotFoundError as e: print('can\'t found ' + e.filename) sys.exit(e.errno) Shadowsocks.workers = [Shadowsocks(i, ss_config) for i in range(options.workers)] for index in ss_config['running']: Shadowsocks.workers[index].start() super(App, self).__init__(handlers, **settings) logging.info("listening on http://localhost:%s" % options.port) self.reset_timer = None if options.password_timeout > 0: self._reset_timer_callback() from tornado import autoreload autoreload.add_reload_hook(App._stop_all_worker)
def start(self): loop = asyncio.get_event_loop() for sig in [signal.SIGINT, signal.SIGTERM]: loop.add_signal_handler(sig, self.stop) if self.settings.get('autoreload'): autoreload.add_reload_hook(self.proxy_manager.stop) self.proxy_manager.start() self.listen(self.config.web_server_port) logger.info( f'Web server listening at {self.config.web_server_port} port.') IOLoop.current().start()
def serve(self): with self: # Automatically reload modified modules (from Tornado's # Application.__init__). This code must be done here *after* # daemonize, because it instanciates ioloop for current PID. if self.app.webapp.settings.get('autoreload'): autoreload.add_reload_hook(self.autoreload_hook) autoreload.start() logger.info( "Serving temboardui on https://%s:%d", self.app.config.temboard.address, self.app.config.temboard.port) tornado.ioloop.IOLoop.instance().start()
def configure(self, configuration): # Call parent configure method BaseModule.configure(self, configuration) self.server = False self.max_buffer_size = self.getConfigurationValue('max_buffer_size') * 10240 #* 10240 self.start_ioloop = False try: self.sockets = bind_sockets(self.getConfigurationValue("port"), self.getConfigurationValue("interface"), backlog=128) for server_socket in self.sockets: server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) except: etype, evalue, etb = sys.exc_info() self.logger.error("Could not listen on %s:%s. Exception: %s, Error: %s." % (self.getConfigurationValue("interface"), self.getConfigurationValue("port"), etype, evalue)) self.lumbermill.shutDown() return autoreload.add_reload_hook(self.shutDown)
def main(): define_options() options.parse_command_line() settings.LISTEN_PORT += options.port_offset create_db() if options.prepare_startup: prepare_startup() elif options.create_room: with session() as db: RoomService(db).create_default_room(options.create_room) else: if settings.DEBUG: add_reload_hook(prepare_startup) run_app()
def configure(self, configuration): # Call parent configure method BaseModule.BaseModule.configure(self, configuration) self.server = False self.max_buffer_size = self.getConfigurationValue('max_buffer_size') * 10240 #* 10240 self.start_ioloop = False try: self.sockets = bind_sockets(self.getConfigurationValue("port"), self.getConfigurationValue("interface"), backlog=128) for server_socket in self.sockets: server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) except: etype, evalue, etb = sys.exc_info() self.logger.error("Could not listen on %s:%s. Exception: %s, Error: %s." % (self.getConfigurationValue("interface"), self.getConfigurationValue("port"), etype, evalue)) self.gp.shutDown() return autoreload.add_reload_hook(self.shutDown)
def start_server(root_dir): application = init_application(root_dir) logging.info("Starting SomeMemoS server on port %d.", options.host_port) application.listen(options.host_port) pid_dir = os.path.join(root_dir, "env", "run") if not os.path.isdir(pid_dir): if raw_input("OK to create directory %s? (y/N): " % pid_dir) != "y": exit(1) os.makedirs(pid_dir) with PidFile( os.path.join(pid_dir, "somememos-%d.pid" % options.host_port), on_exit=server_shutdown, daemon=options.daemon, on_signal=on_signal, ) as pid: if options.debug: add_reload_hook(pid.__exit__) ioloop.IOLoop.instance().start()
def serve(self, port=5500, liveport=None, host=None, root=None, debug=None, open_url=False, restart_delay=2, open_url_delay=None, live_css=True): """Start serve the server with the given port. :param port: serve on this port, default is 5500 :param liveport: live reload on this port :param host: serve on this hostname, default is 127.0.0.1 :param root: serve static on this root directory :param debug: set debug mode, which autoreloads the app on code changes via Tornado (and causes polling). Defaults to True when ``self.app`` is set, otherwise False. :param open_url_delay: open webbrowser after the delay seconds :param live_css: whether to use live css or force reload on css. Defaults to True """ host = host or '127.0.0.1' if root is not None: self.root = root self._setup_logging() logger.info('Serving on http://%s:%s' % (host, port)) self.application( port, host, liveport=liveport, debug=debug, live_css=live_css) # Async open web browser after 5 sec timeout if open_url or open_url_delay: if open_url: logger.warn('Use `open_url_delay` instead of `open_url`') sleep = open_url_delay or 5 def opener(): time.sleep(sleep) webbrowser.open('http://%s:%s' % (host, port)) threading.Thread(target=opener).start() try: self.watcher._changes.append(('__livereload__', restart_delay)) LiveReloadHandler.start_tasks() add_reload_hook(lambda: IOLoop.instance().close(all_fds=True)) IOLoop.instance().start() except KeyboardInterrupt: logger.info('Shutting down...')
def main(): controller.initialize() app = TornadoApp() http_server = tornado.httpserver.HTTPServer(app) http_server.listen(options.port) print "Tornado server started on port %s" % options.port if settings["debug"]: def f(): controller.shutdown() reload(controller) autoreload.add_reload_hook(f) try: IOLoop.instance().start() finally: controller.shutdown()
def main(): import tornado.options LOG.info('Starting message bus service...') reset_sigint_handler() lock = Lock("service") # Disable all tornado logging so mycroft loglevel isn't overridden tornado.options.parse_command_line(sys.argv + ['--logging=None']) def reload_hook(): """ Hook to release lock when auto reload is triggered. """ lock.delete() autoreload.add_reload_hook(reload_hook) config = load_message_bus_config() routes = [(config.route, MessageBusEventHandler)] application = web.Application(routes, debug=True) application.listen(config.port, config.host) create_daemon(ioloop.IOLoop.instance().start) LOG.info('Message bus service started!') wait_for_exit_signal()
def make_app(): logging.info(get_ws_origins_env()) db_url = orm_utils.heroku_db_url(options.CLEARDB_DATABASE_URL) engine = db_connection.db_init(db_url) if options.DEBUG: orm_utils.create_all(orm_utils.Base, engine) if options.POOL_SIZE: micro_pool = ProcessPoolExecutor(options.POOL_SIZE) executor.pool_init(micro_pool) if options.DEBUG: add_reload_hook(micro_pool.shutdown) logging.info('Pool size: {}'.format(options.POOL_SIZE)) request_handlers = [ (r"/control(.*)", RpcHandler, { 'http_origins': options.ORIGINS, 'js_template': "api-tmpl.js", 'ws_url': options.WS_URL }), (r"/websocket/?", RpcWebsocket, { 'ws_origins': get_ws_origins_env() }), ] return Application( request_handlers, services=Service.describe(actions), micro_context=Context, template_path=resource_filename('dogooder', 'templates'), debug=options.DEBUG, allow_exception_messages=options.DEBUG, cookie_name=options.COOKIE_NAME, cookie_secret=options.COOKIE_SECRET, gzip=True, )
def main(): import tornado.options lock = Lock("service") tornado.options.parse_command_line() def reload_hook(): """ Hook to release lock when autoreload is triggered. """ lock.delete() autoreload.add_reload_hook(reload_hook) config = Configuration.get().get("websocket") host = config.get("host") port = config.get("port") route = config.get("route") validate_param(host, "websocket.host") validate_param(port, "websocket.port") validate_param(route, "websocket.route") routes = [(route, WebsocketEventHandler)] application = web.Application(routes, **settings) application.listen(port, host) ioloop.IOLoop.instance().start()
""" logging.warning("Interrupting the server ...") # Exits the process sys.exit() if __name__ == '__main__': # Logging important information logging.info('Starting server ...') # Setting the responsibility of who will receive the interruption signal signal.signal(signal.SIGINT, signal_handler) # Logs its port logging.info(f'Port: {c.PORT}') # Creates an application app = Server() # Adds an autoreload hook in order to properly shutdown the workers pool autoreload.add_reload_hook(lambda: app.shutdown()) # Servers the application on desired port app.listen(c.PORT) # Starts a IOLoop instance IOLoop.current().start()
def run(self, *args, **kwargs): """Run bot.""" self.logger.info(cactus_art) self._init_database(self.database) self.load_config(filename=self.config_file) self.load_stats(filename=self.stats_file) while self.config.get("autorestart") or not self.started: try: self.bot_data = self.login(**self.config["auth"]) self.logger.info("Authenticated as: {}.".format( self.bot_data["username"])) self.started = True self.channel = self.config["channel"] self.channel_data = self.get_channel(self.channel) self._init_commands() self.connect( self.channel_data["id"], self.bot_data["id"], silent=self.silent) self.connect_to_liveloading( self.channel_data["id"], self.channel_data["userId"]) if str(self.debug).lower() in ("true", "debug"): add_reload_hook(partial( self.send_message, "Restarting, thanks to debug mode. :spaceship" )) watch(self.config_file) start(check_time=5000) IOLoop.instance().start() except KeyboardInterrupt: print() self.logger.info("Removing thorns... done.") try: self.send_message("CactusBot deactivated! :cactus") except Exception: pass finally: exit() except Exception: self.logger.critical("Oh no, I crashed!") try: self.send_message("Oh no, I crashed! :127") except Exception: pass self.logger.error('\n\n' + format_exc()) if self.config.get("autorestart"): self.logger.info("Restarting in 10 seconds...") try: sleep(10) except KeyboardInterrupt: self.logger.info("CactusBot deactivated.") exit() else: self.logger.info("CactusBot deactivated.") exit()
def setup_tornado_rabbitmq( queue_client: TornadoQueueClient) -> None: # nocoverage # When tornado is shut down, disconnect cleanly from RabbitMQ autoreload.add_reload_hook(lambda: queue_client.close())
('/statusv2/?', StatusHandler), ('/wsclient/?', WebSocketProtocolHandler), ('/wsproxy/?', WebSocketProxyHandler), ('/wsincomingproxy/?', WebSocketIncomingProxyHandler), # ('/wstrackerproxy/?', WebSocketTrackerProxyHandler), ('/wsudpproxy/?', WebSocketUDPProxyHandler), ('/api/?', APIHandler), ('/btapp/?', BtappHandler) ] application = BTApplication(routes, **settings) Client.resume() client = Client.instances[0] add_reload_hook( lambda: Client.save_settings() ) def got_interrupt_signal(signum=None, frame=None): logging.info('got quit signal ... saving quick resume') Client.save_settings() #Torrent.save_quick_resume() sys.exit() signal.signal(signal.SIGINT, got_interrupt_signal) class BTProtocolServer(tornado.netutil.TCPServer): def __init__(self, request_callback, io_loop=None): tornado.netutil.TCPServer.__init__(self, io_loop) self.request_callback = request_callback def handle_stream(self, stream, address):
js_includes=ibcommands.js_includes ) class MainHandler(BaseHandler): def get(self): self.render('index.html') def shutdown(): if xmpp_process: xmpp_process.terminate() if __name__ == "__main__": MessagesRouter = SockJSRouter(MessagesConnection, '/messageChannel') app_settings = module_to_dict(settings) autoreload.add_reload_hook(shutdown) application = tornado.web.Application( MessagesRouter.urls + [ (r'/project/?($|[0-9a-fA-F]{24})', ProjectHandler), (r'/project/([0-9a-fA-F]{24})/deviceregistration/(.*)', DeviceRegistrationHandler), (r'/project/([0-9a-fA-F]{24})/getagent', GetAgentHandler), (r'/project/([0-9a-fA-F]{24})/messagedefinition/?($|.*)', MessageDefinitionHandler), (r'/project/([0-9a-fA-F]{24})/device/(.*)/command', DeviceCommandHandler), (r'/project/([0-9a-fA-F]{24})/device/(.*)/flashfw', DeviceFlashHandler), (r'/file', FileUploadHandler), (r'/command', CommandHandler), (r'/sms', SmsHandler), (r'/msg/?(.*)', MessageHandler), (r'/presence/?(.*)', PresenceHandler),
from tornado.httpserver import HTTPServer from tornado.ioloop import IOLoop from tornado.options import options, define, parse_command_line from tornado.wsgi import WSGIContainer __autor__ = "William Mendez" __version__ = "0.006" define('port', type= int, default=8080) server = u"El servidor de desarrollo está iniciando en la url http://127.0.0.1: " + str(options.port) def auto_reload_hook(): print server if __name__ == "__main__": parse_command_line() app = get_app('development.ini') container = WSGIContainer(app) http_server = HTTPServer(container) http_server.listen(options.port) add_reload_hook(auto_reload_hook) start() print server try: IOLoop.instance().start() except KeyboardInterrupt: print "" print "Saliendo" exit(0)
}, } from controllers.handlers import handlers application = web.Application(handlers, **settings) application.db = torndb.Connection(host=options.mysql_host, database=options.mysql_database, user=options.mysql_user, password=options.mysql_password) application.redis = redis.StrictRedis(host=options.redis_host, port=options.redis_port, db=options.redis_db) server = httpserver.HTTPServer(application) server.listen(options.app_port) # 注册程序退出处理函数 from autumn.app import register_terminal_handler register_terminal_handler(server, application) # 注册tornado检测文件变更时,自动重启服务时的处理函数 def on_reload(): logging.info('close database connection') application.db.close() autoreload.add_reload_hook(on_reload) logging.info('application started on port:%s', options.app_port) ioloop.IOLoop.instance().start()
def __init__(self, manager): self._manager = manager self._logger = logging.getLogger('Runner') install_shutdown_handlers(self._cleanup_and_stop) add_reload_hook(self._cleanup)
class ZTaskdCommand(Command): """docstring for ServerCommand""" name = 'ztaskd' ioloop = None func_cache = {} require_env = True nb_running = 0 stop_next = False @gen.engine def handle(self, replay_failed=False, max_running=1, time_between_check=600): """docstring for handle""" self.ioloop = IOLoop(ZMQPoller()) self.ioloop.install() self.max_running = max_running self.time_between_check = time_between_check context = zmq.Context() socket = context.socket(zmq.PULL) socket.bind("tcp://127.0.0.1:5000") self.db = get_db() self.periodic = tornado.ioloop.PeriodicCallback(self.run, time_between_check * 1000, io_loop=self.ioloop) def install_queue_handler(ioloop): def _queue_handler(socket, *args, **kwargs): try: function_name, args, kwargs, after = socket.recv_pyobj() if function_name == 'ztask_log': logging.warn('%s: %s' % (args[0], args[1])) return datetime.combine(date.today(), time()) + timedelta(hours=1) self.db.ztask.insert( { 'function_name': function_name, 'args': pickle.dumps(args), 'kwargs': pickle.dumps(kwargs), 'retry_count': 0, 'next_attempt': datetime.combine(date.today(), time()) + timedelta(seconds=after) }, callback=self._on_insert) except Exception, e: logging.error('Error setting up function. Details:\n%s' % e) traceback.print_exc(e) ioloop.add_handler(socket, _queue_handler, ioloop.READ) # Reload tasks if necessary cursor = None # if replay_failed: # cursor = self.db.ztask.find() # else: # cursor = self.db.ztask.find() # if cursor is not None: # cursor.loop(callback=self._on_select) for uid, task in options.scheduled_tasks.items(): if not 'schedule' in task: raise Exception('schedule is required') if isinstance(task['schedule'], timedelta): module = get_module_from_import(task['task']) func = partial(module. async, *task.get('args', []), **task.get('kwargs', {})) periodic = tornado.ioloop.PeriodicCallback( func, task['schedule'].seconds * 1000, io_loop=self.ioloop) if task.get('run_on_init', False): logging.info('run_on_init for %s enabled' % (uid, )) func() logging.info('Starting periodic (%ss) for %s' % ( task['schedule'].seconds, uid, )) periodic.start() install_queue_handler(self.ioloop) self.run() self.periodic.start() if 0: #options.debug: from tornado import autoreload autoreload.add_reload_hook( partial(install_queue_handler, self.ioloop)) autoreload.start(io_loop=self.ioloop) try: self.ioloop.start() except KeyboardInterrupt: if self.stop_next: self.ioloop.stop() if self.nb_running and not self.stop_next: self.stop_next = True
import tornado.ioloop from tornado import autoreload from pmail import application, smtpd import logging logging.basicConfig(level=logging.INFO) application.settings['debug'] = True application.listen(8000) global emlsvr emlsvr = smtpd() def arfun(): global emlsvr emlsvr.stop() print("Restarted...") ioloop = tornado.ioloop.IOLoop().instance() autoreload.add_reload_hook(arfun) autoreload.start(ioloop) try: ioloop.start() except KeyboardInterrupt: print("Caught Ctrl-C.") emlsvr.stop() print("Clean Exit.")
settings = { 'static_path': os.path.join(options.project_path, 'static'), 'xsrf_cookies': False, 'debug': options.debug, 'gzip': True, 'static_host': 'http://api1.enote.ctyun.cn/static/', } tornado.web.Application.__init__(self, [ (r'/login/oauth_redirect', 'handler.user.LoginHandler'), (r'/logout', 'handler.user.LogoutHandler'), (r'/usercheck', 'handler.user.UserHandler'), (r'/category', 'handler.note.CategoryHandler'), (r'/notebook', 'handler.note.NotebookHandler'), (r'/updatenote', 'handler.note.UpdateNoteHandler'), (r'/updatecate', 'handler.note.UpdateCateHandler'), (r'/rm', 'handler.note.RemoveHandler'), (r'/upload', 'handler.note.ImageHandler'), (r'/.*', 'handler.base.BaseHandler'), ], **settings) def free_resource(): from orm import orm orm.close() if __name__ == "__main__": add_reload_hook(free_resource) application = Application() http_server = tornado.httpserver.HTTPServer(application) http_server.listen(options.port) tornado.ioloop.IOLoop.instance().start()
def send_response(): print 'msg' if new_msg<>old_msg: print new_msg class SocketHandler(websocket.WebSocketHandler): ''' websocket handler ''' def open(self): ''' ran once an open ws connection is made''' self.send('Opened') socket=self def send(self,message): self.write_message(message) def on_close(self): ''' on close event, triggered once a connection is closed''' self.send('Closed') app = web.Application([ (r'/ws', SocketHandler), ]) if __name__ == '__main__': app.listen(8888) autoreload.add_reload_hook(send_response) autoreload.start() new_msg='boo' ioloop.IOLoop.instance().start()
def runCouchPotato(options, base_path, args, data_dir = None, log_dir = None, Env = None, desktop = None): try: locale.setlocale(locale.LC_ALL, "") encoding = locale.getpreferredencoding() except (locale.Error, IOError): encoding = None # for OSes that are poorly configured I'll just force UTF-8 if not encoding or encoding in ('ANSI_X3.4-1968', 'US-ASCII', 'ASCII'): encoding = 'UTF-8' Env.set('encoding', encoding) # Do db stuff db_path = sp(os.path.join(data_dir, 'database')) # Check if database exists db = SuperThreadSafeDatabase(db_path) db_exists = db.exists() if db_exists: # Backup before start and cleanup old backups backup_path = sp(os.path.join(data_dir, 'db_backup')) backup_count = 5 existing_backups = [] if not os.path.isdir(backup_path): os.makedirs(backup_path) for root, dirs, files in os.walk(backup_path): for backup_file in sorted(files): ints = re.findall('\d+', backup_file) # Delete non zip files if len(ints) != 1: os.remove(os.path.join(backup_path, backup_file)) else: existing_backups.append((int(ints[0]), backup_file)) # Remove all but the last 5 for eb in existing_backups[:-backup_count]: os.remove(os.path.join(backup_path, eb[1])) # Create new backup new_backup = sp(os.path.join(backup_path, '%s.tar.gz' % int(time.time()))) zipf = tarfile.open(new_backup, 'w:gz') for root, dirs, files in os.walk(db_path): for zfilename in files: zipf.add(os.path.join(root, zfilename), arcname = 'database/%s' % os.path.join(root[len(db_path) + 1:], zfilename)) zipf.close() # Open last db.open() else: db.create() # Force creation of cachedir log_dir = sp(log_dir) cache_dir = sp(os.path.join(data_dir, 'cache')) python_cache = sp(os.path.join(cache_dir, 'python')) if not os.path.exists(cache_dir): os.mkdir(cache_dir) if not os.path.exists(python_cache): os.mkdir(python_cache) # Register environment settings Env.set('app_dir', sp(base_path)) Env.set('data_dir', sp(data_dir)) Env.set('log_path', sp(os.path.join(log_dir, 'CouchPotato.log'))) Env.set('db', db) Env.set('cache_dir', cache_dir) Env.set('cache', FileSystemCache(python_cache)) Env.set('console_log', options.console_log) Env.set('quiet', options.quiet) Env.set('desktop', desktop) Env.set('daemonized', options.daemon) Env.set('args', args) Env.set('options', options) # Determine debug debug = options.debug or Env.setting('debug', default = False, type = 'bool') Env.set('debug', debug) # Development development = Env.setting('development', default = False, type = 'bool') Env.set('dev', development) # Disable logging for some modules for logger_name in ['enzyme', 'guessit', 'subliminal', 'apscheduler', 'tornado', 'requests']: logging.getLogger(logger_name).setLevel(logging.ERROR) for logger_name in ['gntp']: logging.getLogger(logger_name).setLevel(logging.WARNING) # Use reloader reloader = debug is True and development and not Env.get('desktop') and not options.daemon # Logger logger = logging.getLogger() formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s', '%m-%d %H:%M:%S') level = logging.DEBUG if debug else logging.INFO logger.setLevel(level) logging.addLevelName(19, 'INFO') # To screen if (debug or options.console_log) and not options.quiet and not options.daemon: hdlr = logging.StreamHandler(sys.stderr) hdlr.setFormatter(formatter) logger.addHandler(hdlr) # To file hdlr2 = handlers.RotatingFileHandler(Env.get('log_path'), 'a', 500000, 10, encoding = Env.get('encoding')) hdlr2.setFormatter(formatter) logger.addHandler(hdlr2) # Start logging & enable colors # noinspection PyUnresolvedReferences import color_logs from couchpotato.core.logger import CPLog log = CPLog(__name__) log.debug('Started with options %s', options) def customwarn(message, category, filename, lineno, file = None, line = None): log.warning('%s %s %s line:%s', (category, message, filename, lineno)) warnings.showwarning = customwarn # Create app from couchpotato import WebHandler web_base = ('/' + Env.setting('url_base').lstrip('/') + '/') if Env.setting('url_base') else '/' Env.set('web_base', web_base) api_key = Env.setting('api_key') if not api_key: api_key = uuid4().hex Env.setting('api_key', value = api_key) api_base = r'%sapi/%s/' % (web_base, api_key) Env.set('api_base', api_base) # Basic config host = Env.setting('host', default = '0.0.0.0') # app.debug = development config = { 'use_reloader': reloader, 'port': tryInt(Env.setting('port', default = 5050)), 'host': host if host and len(host) > 0 else '0.0.0.0', 'ssl_cert': Env.setting('ssl_cert', default = None), 'ssl_key': Env.setting('ssl_key', default = None), } # Load the app application = Application( [], log_function = lambda x: None, debug = config['use_reloader'], gzip = True, cookie_secret = api_key, login_url = '%slogin/' % web_base, ) Env.set('app', application) # Request handlers application.add_handlers(".*$", [ (r'%snonblock/(.*)(/?)' % api_base, NonBlockHandler), # API handlers (r'%s(.*)(/?)' % api_base, ApiHandler), # Main API handler (r'%sgetkey(/?)' % web_base, KeyHandler), # Get API key (r'%s' % api_base, RedirectHandler, {"url": web_base + 'docs/'}), # API docs # Login handlers (r'%slogin(/?)' % web_base, LoginHandler), (r'%slogout(/?)' % web_base, LogoutHandler), # Catch all webhandlers (r'%s(.*)(/?)' % web_base, WebHandler), (r'(.*)', WebHandler), ]) # Static paths static_path = '%sstatic/' % web_base for dir_name in ['fonts', 'images', 'scripts', 'style']: application.add_handlers(".*$", [ ('%s%s/(.*)' % (static_path, dir_name), StaticFileHandler, {'path': sp(os.path.join(base_path, 'couchpotato', 'static', dir_name))}) ]) Env.set('static_path', static_path) # Load configs & plugins loader = Env.get('loader') loader.preload(root = sp(base_path)) loader.run() # Fill database with needed stuff fireEvent('database.setup') if not db_exists: fireEvent('app.initialize', in_order = True) fireEvent('app.migrate') # Go go go! from tornado.ioloop import IOLoop from tornado.autoreload import add_reload_hook loop = IOLoop.current() # Reload hook def test(): fireEvent('app.shutdown') add_reload_hook(test) # Some logging and fire load event try: log.info('Starting server on port %(port)s', config) except: pass fireEventAsync('app.load') if config['ssl_cert'] and config['ssl_key']: server = HTTPServer(application, no_keep_alive = True, ssl_options = { 'certfile': config['ssl_cert'], 'keyfile': config['ssl_key'], }) else: server = HTTPServer(application, no_keep_alive = True) try_restart = True restart_tries = 5 while try_restart: try: server.listen(config['port'], config['host']) loop.start() except Exception as e: log.error('Failed starting: %s', traceback.format_exc()) try: nr, msg = e if nr == 48: log.info('Port (%s) needed for CouchPotato is already in use, try %s more time after few seconds', (config.get('port'), restart_tries)) time.sleep(1) restart_tries -= 1 if restart_tries > 0: continue else: return except: pass raise try_restart = False
def runCouchPotato(options, base_path, args, data_dir=None, log_dir=None, Env=None, desktop=None): try: locale.setlocale(locale.LC_ALL, "") encoding = locale.getpreferredencoding() except (locale.Error, IOError): encoding = None # for OSes that are poorly configured I'll just force UTF-8 if not encoding or encoding in ('ANSI_X3.4-1968', 'US-ASCII', 'ASCII'): encoding = 'UTF-8' Env.set('encoding', encoding) # Do db stuff db_path = sp(os.path.join(data_dir, 'database')) old_db_path = os.path.join(data_dir, 'couchpotato.db') # Remove database folder if both exists if os.path.isdir(db_path) and os.path.isfile(old_db_path): db = SuperThreadSafeDatabase(db_path) db.open() db.destroy() # Check if database exists db = SuperThreadSafeDatabase(db_path) db_exists = db.exists() if db_exists: # Backup before start and cleanup old backups backup_path = sp(os.path.join(data_dir, 'db_backup')) backup_count = 5 existing_backups = [] if not os.path.isdir(backup_path): os.makedirs(backup_path) for root, dirs, files in os.walk(backup_path): # Only consider files being a direct child of the backup_path if root == backup_path: for backup_file in sorted(files): ints = re.findall('\d+', backup_file) # Delete non zip files if len(ints) != 1: os.remove(os.path.join(root, backup_file)) else: existing_backups.append((int(ints[0]), backup_file)) else: # Delete stray directories. shutil.rmtree(root) # Remove all but the last 5 for eb in existing_backups[:-backup_count]: os.remove(os.path.join(backup_path, eb[1])) # Create new backup new_backup = sp( os.path.join(backup_path, '%s.tar.gz' % int(time.time()))) zipf = tarfile.open(new_backup, 'w:gz') for root, dirs, files in os.walk(db_path): for zfilename in files: zipf.add(os.path.join(root, zfilename), arcname='database/%s' % os.path.join(root[len(db_path) + 1:], zfilename)) zipf.close() # Open last db.open() else: db.create() # Force creation of cachedir log_dir = sp(log_dir) cache_dir = sp(os.path.join(data_dir, 'cache')) python_cache = sp(os.path.join(cache_dir, 'python')) if not os.path.exists(cache_dir): os.mkdir(cache_dir) if not os.path.exists(python_cache): os.mkdir(python_cache) # Register environment settings Env.set('app_dir', sp(base_path)) Env.set('data_dir', sp(data_dir)) Env.set('log_path', sp(os.path.join(log_dir, 'CouchPotato.log'))) Env.set('db', db) Env.set('http_opener', requests.Session()) Env.set('cache_dir', cache_dir) Env.set('cache', FileSystemCache(python_cache)) Env.set('console_log', options.console_log) Env.set('quiet', options.quiet) Env.set('desktop', desktop) Env.set('daemonized', options.daemon) Env.set('args', args) Env.set('options', options) # Determine debug debug = options.debug or Env.setting('debug', default=False, type='bool') Env.set('debug', debug) # Development development = Env.setting('development', default=False, type='bool') Env.set('dev', development) # Disable logging for some modules for logger_name in [ 'enzyme', 'guessit', 'subliminal', 'apscheduler', 'tornado', 'requests' ]: logging.getLogger(logger_name).setLevel(logging.ERROR) for logger_name in ['gntp']: logging.getLogger(logger_name).setLevel(logging.WARNING) # Disable SSL warning disable_warnings() # Use reloader reloader = debug is True and development and not Env.get( 'desktop') and not options.daemon # Logger logger = logging.getLogger() formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s', '%m-%d %H:%M:%S') level = logging.DEBUG if debug else logging.INFO logger.setLevel(level) logging.addLevelName(19, 'INFO') # To screen if (debug or options.console_log) and not options.quiet and not options.daemon: hdlr = logging.StreamHandler(sys.stderr) hdlr.setFormatter(formatter) logger.addHandler(hdlr) # To file hdlr2 = handlers.RotatingFileHandler(Env.get('log_path'), 'a', 500000, 10, encoding=Env.get('encoding')) hdlr2.setFormatter(formatter) logger.addHandler(hdlr2) # Start logging & enable colors # noinspection PyUnresolvedReferences import color_logs from couchpotato.core.logger import CPLog log = CPLog(__name__) log.debug('Started with options %s', options) # Check available space try: total_space, available_space = getFreeSpace(data_dir) if available_space < 100: log.error( 'Shutting down as CP needs some space to work. You\'ll get corrupted data otherwise. Only %sMB left', available_space) return except: log.error('Failed getting diskspace: %s', traceback.format_exc()) def customwarn(message, category, filename, lineno, file=None, line=None): log.warning('%s %s %s line:%s', (category, message, filename, lineno)) warnings.showwarning = customwarn # Create app from couchpotato import WebHandler web_base = ('/' + Env.setting('url_base').lstrip('/') + '/') if Env.setting('url_base') else '/' Env.set('web_base', web_base) api_key = Env.setting('api_key') if not api_key: api_key = uuid4().hex Env.setting('api_key', value=api_key) api_base = r'%sapi/%s/' % (web_base, api_key) Env.set('api_base', api_base) # Basic config host = Env.setting('host', default='0.0.0.0') # app.debug = development config = { 'use_reloader': reloader, 'port': tryInt(Env.setting('port', default=5050)), 'host': host if host and len(host) > 0 else '0.0.0.0', 'ssl_cert': Env.setting('ssl_cert', default=None), 'ssl_key': Env.setting('ssl_key', default=None), } # Load the app application = Application( [], log_function=lambda x: None, debug=config['use_reloader'], gzip=True, cookie_secret=api_key, login_url='%slogin/' % web_base, ) Env.set('app', application) # Request handlers application.add_handlers( ".*$", [ (r'%snonblock/(.*)(/?)' % api_base, NonBlockHandler), # API handlers (r'%s(.*)(/?)' % api_base, ApiHandler), # Main API handler (r'%sgetkey(/?)' % web_base, KeyHandler), # Get API key (r'%s' % api_base, RedirectHandler, { "url": web_base + 'docs/' }), # API docs # Login handlers (r'%slogin(/?)' % web_base, LoginHandler), (r'%slogout(/?)' % web_base, LogoutHandler), # Catch all webhandlers (r'%s(.*)(/?)' % web_base, WebHandler), (r'(.*)', WebHandler), ]) # Static paths static_path = '%sstatic/' % web_base for dir_name in ['fonts', 'images', 'scripts', 'style']: application.add_handlers( ".*$", [('%s%s/(.*)' % (static_path, dir_name), StaticFileHandler, { 'path': sp(os.path.join(base_path, 'couchpotato', 'static', dir_name)) })]) Env.set('static_path', static_path) # Load configs & plugins loader = Env.get('loader') loader.preload(root=sp(base_path)) loader.run() # Fill database with needed stuff fireEvent('database.setup') if not db_exists: fireEvent('app.initialize', in_order=True) fireEvent('app.migrate') # Go go go! from tornado.ioloop import IOLoop from tornado.autoreload import add_reload_hook loop = IOLoop.current() # Reload hook def reload_hook(): fireEvent('app.shutdown') add_reload_hook(reload_hook) # Some logging and fire load event try: log.info('Starting server on port %(port)s', config) except: pass fireEventAsync('app.load') ssl_options = None if config['ssl_cert'] and config['ssl_key']: ssl_options = { 'certfile': config['ssl_cert'], 'keyfile': config['ssl_key'], } server = HTTPServer(application, no_keep_alive=True, ssl_options=ssl_options) try_restart = True restart_tries = 5 while try_restart: try: server.listen(config['port'], config['host']) loop.start() server.close_all_connections() server.stop() loop.close(all_fds=True) except Exception as e: log.error('Failed starting: %s', traceback.format_exc()) try: nr, msg = e if nr == 48: log.info( 'Port (%s) needed for CouchPotato is already in use, try %s more time after few seconds', (config.get('port'), restart_tries)) time.sleep(1) restart_tries -= 1 if restart_tries > 0: continue else: return except ValueError: return except: pass raise try_restart = False
def runCouchPotato(options, base_path, args, data_dir=None, log_dir=None, Env=None, desktop=None): try: locale.setlocale(locale.LC_ALL, "") encoding = locale.getpreferredencoding() except (locale.Error, IOError): encoding = None # for OSes that are poorly configured I'll just force UTF-8 if not encoding or encoding in ("ANSI_X3.4-1968", "US-ASCII", "ASCII"): encoding = "UTF-8" Env.set("encoding", encoding) # Do db stuff db_path = sp(os.path.join(data_dir, "database")) old_db_path = os.path.join(data_dir, "couchpotato.db") # Remove database folder if both exists if os.path.isdir(db_path) and os.path.isfile(old_db_path): db = SuperThreadSafeDatabase(db_path) db.open() db.destroy() # Check if database exists db = SuperThreadSafeDatabase(db_path) db_exists = db.exists() if db_exists: # Backup before start and cleanup old backups backup_path = sp(os.path.join(data_dir, "db_backup")) backup_count = 5 existing_backups = [] if not os.path.isdir(backup_path): os.makedirs(backup_path) for root, dirs, files in os.walk(backup_path): # Only consider files being a direct child of the backup_path if root == backup_path: for backup_file in sorted(files): ints = re.findall("\d+", backup_file) # Delete non zip files if len(ints) != 1: try: os.remove(os.path.join(root, backup_file)) except: pass else: existing_backups.append((int(ints[0]), backup_file)) else: # Delete stray directories. shutil.rmtree(root) # Remove all but the last 5 for eb in existing_backups[:-backup_count]: os.remove(os.path.join(backup_path, eb[1])) # Create new backup new_backup = sp(os.path.join(backup_path, "%s.tar.gz" % int(time.time()))) zipf = tarfile.open(new_backup, "w:gz") for root, dirs, files in os.walk(db_path): for zfilename in files: zipf.add( os.path.join(root, zfilename), arcname="database/%s" % os.path.join(root[len(db_path) + 1 :], zfilename), ) zipf.close() # Open last db.open() else: db.create() # Force creation of cachedir log_dir = sp(log_dir) cache_dir = sp(os.path.join(data_dir, "cache")) python_cache = sp(os.path.join(cache_dir, "python")) if not os.path.exists(cache_dir): os.mkdir(cache_dir) if not os.path.exists(python_cache): os.mkdir(python_cache) session = requests.Session() session.max_redirects = 5 # Register environment settings Env.set("app_dir", sp(base_path)) Env.set("data_dir", sp(data_dir)) Env.set("log_path", sp(os.path.join(log_dir, "CouchPotato.log"))) Env.set("db", db) Env.set("http_opener", session) Env.set("cache_dir", cache_dir) Env.set("cache", FileSystemCache(python_cache)) Env.set("console_log", options.console_log) Env.set("quiet", options.quiet) Env.set("desktop", desktop) Env.set("daemonized", options.daemon) Env.set("args", args) Env.set("options", options) # Determine debug debug = options.debug or Env.setting("debug", default=False, type="bool") Env.set("debug", debug) # Development development = Env.setting("development", default=False, type="bool") Env.set("dev", development) # Disable logging for some modules for logger_name in ["enzyme", "guessit", "subliminal", "apscheduler", "tornado", "requests"]: logging.getLogger(logger_name).setLevel(logging.ERROR) for logger_name in ["gntp"]: logging.getLogger(logger_name).setLevel(logging.WARNING) # Disable SSL warning disable_warnings() # Use reloader reloader = debug is True and development and not Env.get("desktop") and not options.daemon # Logger logger = logging.getLogger() formatter = logging.Formatter("%(asctime)s %(levelname)s %(message)s", "%m-%d %H:%M:%S") level = logging.DEBUG if debug else logging.INFO logger.setLevel(level) logging.addLevelName(19, "INFO") # To screen if (debug or options.console_log) and not options.quiet and not options.daemon: hdlr = logging.StreamHandler(sys.stderr) hdlr.setFormatter(formatter) logger.addHandler(hdlr) # To file hdlr2 = handlers.RotatingFileHandler(Env.get("log_path"), "a", 500000, 10, encoding=Env.get("encoding")) hdlr2.setFormatter(formatter) logger.addHandler(hdlr2) # Start logging & enable colors # noinspection PyUnresolvedReferences import color_logs from couchpotato.core.logger import CPLog log = CPLog(__name__) log.debug("Started with options %s", options) # Check soft-chroot dir exists: try: # Load Soft-Chroot soft_chroot = Env.get("softchroot") soft_chroot_dir = Env.setting("soft_chroot", section="core", default=None, type="unicode") soft_chroot.initialize(soft_chroot_dir) except SoftChrootInitError as exc: log.error(exc) return except: log.error("Unable to check whether SOFT-CHROOT is defined") return # Check available space try: total_space, available_space = getFreeSpace(data_dir) if available_space < 100: log.error( "Shutting down as CP needs some space to work. You'll get corrupted data otherwise. Only %sMB left", available_space, ) return except: log.error("Failed getting diskspace: %s", traceback.format_exc()) def customwarn(message, category, filename, lineno, file=None, line=None): log.warning("%s %s %s line:%s", (category, message, filename, lineno)) warnings.showwarning = customwarn # Create app from couchpotato import WebHandler web_base = ("/" + Env.setting("url_base").lstrip("/") + "/") if Env.setting("url_base") else "/" Env.set("web_base", web_base) api_key = Env.setting("api_key") if not api_key: api_key = uuid4().hex Env.setting("api_key", value=api_key) api_base = r"%sapi/%s/" % (web_base, api_key) Env.set("api_base", api_base) # Basic config host = Env.setting("host", default="0.0.0.0") host6 = Env.setting("host6", default="::") config = { "use_reloader": reloader, "port": tryInt(Env.setting("port", default=5050)), "host": host if host and len(host) > 0 else "0.0.0.0", "host6": host6 if host6 and len(host6) > 0 else "::", "ssl_cert": Env.setting("ssl_cert", default=None), "ssl_key": Env.setting("ssl_key", default=None), } # Load the app application = Application( [], log_function=lambda x: None, debug=config["use_reloader"], gzip=True, cookie_secret=api_key, login_url="%slogin/" % web_base, ) Env.set("app", application) # Request handlers application.add_handlers( ".*$", [ (r"%snonblock/(.*)(/?)" % api_base, NonBlockHandler), # API handlers (r"%s(.*)(/?)" % api_base, ApiHandler), # Main API handler (r"%sgetkey(/?)" % web_base, KeyHandler), # Get API key (r"%s" % api_base, RedirectHandler, {"url": web_base + "docs/"}), # API docs # Login handlers (r"%slogin(/?)" % web_base, LoginHandler), (r"%slogout(/?)" % web_base, LogoutHandler), # Catch all webhandlers (r"%s(.*)(/?)" % web_base, WebHandler), (r"(.*)", WebHandler), ], ) # Static paths static_path = "%sstatic/" % web_base for dir_name in ["fonts", "images", "scripts", "style"]: application.add_handlers( ".*$", [ ( "%s%s/(.*)" % (static_path, dir_name), StaticFileHandler, {"path": sp(os.path.join(base_path, "couchpotato", "static", dir_name))}, ) ], ) Env.set("static_path", static_path) # Load configs & plugins loader = Env.get("loader") loader.preload(root=sp(base_path)) loader.run() # Fill database with needed stuff fireEvent("database.setup") if not db_exists: fireEvent("app.initialize", in_order=True) fireEvent("app.migrate") # Go go go! from tornado.ioloop import IOLoop from tornado.autoreload import add_reload_hook loop = IOLoop.current() # Reload hook def reload_hook(): fireEvent("app.shutdown") add_reload_hook(reload_hook) # Some logging and fire load event try: log.info("Starting server on port %(port)s", config) except: pass fireEventAsync("app.load") ssl_options = None if config["ssl_cert"] and config["ssl_key"]: ssl_options = {"certfile": config["ssl_cert"], "keyfile": config["ssl_key"]} server = HTTPServer(application, no_keep_alive=True, ssl_options=ssl_options) try_restart = True restart_tries = 5 while try_restart: try: if config["host"].startswith("unix:"): server.add_socket(bind_unix_socket(config["host"][5:])) else: server.listen(config["port"], config["host"]) if Env.setting("ipv6", default=False): try: server.listen(config["port"], config["host6"]) except: log.info2("Tried to bind to IPV6 but failed") loop.start() server.close_all_connections() server.stop() loop.close(all_fds=True) except Exception as e: log.error("Failed starting: %s", traceback.format_exc()) try: nr, msg = e if nr == 48: log.info( "Port (%s) needed for CouchPotato is already in use, try %s more time after few seconds", (config.get("port"), restart_tries), ) time.sleep(1) restart_tries -= 1 if restart_tries > 0: continue else: return except ValueError: return except: pass raise try_restart = False
load_app_options() # 加载配置 settings = { 'template_path': os.path.join(options.app_path, 'templates'), 'static_path': os.path.join(options.app_path, 'static'), 'cookie_secret': options.cookie_secret, 'debug': options.app_debug, 'login_url': '/login', 'xsrf_cookies': False, } application = web.Application(handlers, **settings) application.db = torndb.Connection( host=options.mysql_host, database=options.mysql_database, user=options.mysql_user, password=options.mysql_password) server = httpserver.HTTPServer(application) server.listen(options.app_port) # 注册程序退出处理函数 from autumn.app import register_terminal_handler register_terminal_handler(server, application) # 注册tornado检测文件变更时,自动重启服务时的处理函数 def on_reload(): logging.info('close database connection') application.db.close() autoreload.add_reload_hook(on_reload) logging.info('application started on port:%s', options.app_port) ioloop.IOLoop.instance().start()
def run(self, *args, **kwargs): """Run bot.""" self.logger.info(cactus_art) self._init_database(self.database) self.load_config(filename=self.config_file) self.load_stats(filename=self.stats_file) while self.config.get("autorestart") or not self.started: try: self.bot_data = self.login(**self.config["auth"]) self.logger.info("Authenticated as: {}.".format( self.bot_data["username"])) self.started = True self.channel = self.config["channel"] self.channel_data = self.get_channel(self.channel) self._init_commands() self.connect( self.channel_data["id"], self.bot_data["id"], silent=self.silent) self.connect_to_liveloading( self.channel_data["id"], self.channel_data["userId"]) if self.debug: add_reload_hook(partial( self.send_message, "Restarting, thanks to debug mode. :spaceship" )) start(check_time=10000) IOLoop.instance().start() except KeyboardInterrupt: print() self.logger.info("Removing thorns... done.") try: self.send_message("CactusBot deactivated! :cactus") except Exception: pass finally: exit() except Exception: self.logger.critical("Oh no, I crashed!") try: self.send_message("Oh no, I crashed! :127") except Exception: pass self.logger.error("\n\n" + format_exc()) if self.config.get("autorestart"): self.logger.info("Restarting in 10 seconds...") try: sleep(10) except KeyboardInterrupt: self.logger.info("CactusBot deactivated.") exit() else: self.logger.info("CactusBot deactivated.") exit()
def __init__(self, log): # logging if options.debug: log.setLevel(logging.DEBUG) # prepare config manager config = plugins.Config(os.path.abspath('./config.cfg')) # prepare dispatcher and listeners (plugins) dispatcher = Dispatcher() plugins.AntiFlood().register(dispatcher) plugins.Archive(os.path.abspath('./archive.%Y%m%d.csv'), \ archive_formatter).register(dispatcher) plugins.Ban(config.get('ban', {})).register(dispatcher) plugins.Colors(config.get('colors', {})).register(dispatcher) plugins.Console().register(dispatcher) config.register(dispatcher) plugins.Dice().register(dispatcher) plugins.Direct().register(dispatcher) plugins.Me().register(dispatcher) plugins.Nap(config.get('nap', [])).register(dispatcher) plugins.NoAuth().register(dispatcher) plugins.Puppet(config.get('puppet', {})).register(dispatcher) plugins.Quotations(config.get('quotations', [])).register(dispatcher) plugins.Tidy().register(dispatcher) plugins.Typing().register(dispatcher) plugins.ValidateLogin(config.get('stopwords', [])).register(dispatcher) plugins.Voices(config.get('voices', {})).register(dispatcher) plugins.Whoami().register(dispatcher) # prepare API instance and run chat api = campfire.Api(log, dispatcher) api.init() # prepare auth handler auth = AuthHelper(api, dispatcher, log) args = {'log': log, 'api': api, 'auth': auth} # handlers and settings handlers = [ (r"/chat/login", chat.AuthHandler, args), (r"/chat/logout", chat.AuthHandler, args), (r"/chat/reply", chat.HttpHandler, args), (r"/chat/poll", chat.HttpHandler, args), (r"/chat/socket", chat.SocketHandler, args), (r"/", tornado.web.RedirectHandler, {"url": \ '/example/index.html'}), (r"/(.*)", tornado.web.StaticFileHandler, {"path": \ os.path.abspath('../vendors/js-campfire/')}) ] settings = dict( cookie_secret = "6s3oEoTlzJKX^OGa=dko5gwmGgJJnuY@7Emup0XdrP13/Vka", debug = options.debug ) # signal handlers def _shutdown(signum, stack_frame): api.shutdown() sys.exit(1) signal.signal(signal.SIGTERM, _shutdown) signal.signal(signal.SIGINT, _shutdown) if options.debug: add_reload_hook(api.shutdown) # periodic callback p = tornado.ioloop.PeriodicCallback(api.periodic_notification, \ 1 * 60 * 1000) # 1 minute p.start() # start app tornado.web.Application.__init__(self, handlers, **settings)
def main(options): import logging from multiprocessing import Process from multiprocessing.queues import Queue, SimpleQueue if __debug__: from queue import Queue, Queue as SimpleQueue from threading import Thread, Thread as Process from datetime import timedelta delay = timedelta(milliseconds=300) Q0, Q1, Q2 = Queue(), SimpleQueue(), SimpleQueue() put = Q0.put get = Q1.get from .hub import hub from .log import log from .dbg import show sub_procs = {} def start(): #import bb; print(dir(bb)) # modules have been loaded at here, hub will loads the others logging.info("starting sub processes...") if any(proc.is_alive() for proc in sub_procs.values()): logging.warning("sub processes are running, failed to start") return sub_procs["hub"] = Process(target=hub, args=(Q0, Q1, Q2)) sub_procs["log"] = Process(target=log, args=(Q2, )) for name, proc in sub_procs.items(): proc.start() logging.info("%s started, pid:%d", name, getattr(proc, "pid", 0)) logging.info("start sub processes success!") def stop(): logging.info("stopping sub processes...") if all(proc.is_alive() for proc in sub_procs.values()): put(None) else: logging.warning("sub processes are not running, failed to stop") for name, proc in sub_procs.items(): proc.join() logging.info("%s stopped, pid:%d", name, getattr(proc, "pid", 0)) logging.info("stop sub processes success!") start() # main from here import collections import functools import signal import sys import time import urllib.parse import weakref from tornado import ioloop, web, autoreload from . import ui_modules from .conn import tcp, websocket, backdoor from .const import TIME_FORMAT, PING, NULL, DEBUG_OUTPUT from .oc import record, recorder ioloop._Timeout.__repr__ = lambda self: "{} ({}) {}".format( time.strftime(TIME_FORMAT, time.localtime(self.deadline)), self. deadline, self.callback and self.callback.__doc__) io_loop = ioloop.IOLoop.instance() tokens = {} staffs = weakref.WeakValueDictionary() hub_todo = collections.deque() # SIGTERM def _term(signal_number, stack_frame): logging.info("will exit") io_loop.stop() Q1.put(None) stop() signal.signal(signal.SIGTERM, _term) def msg(i, cmd, data): if i is None: hub_todo.popleft()(data) else: s = staffs.get(i) if s: s.send(cmd, data) \ if not __debug__ else io_loop.add_timeout(delay, functools.partial(s.send, cmd, data)) # this line is for debug else: logging.warning("{} is not online failed to send {}-{}".format( i, cmd, data)) if __debug__: def loop_msg(): while True: x = get() show(x) if x is None: break io_loop.add_callback(msg, *x) Thread(target=loop_msg).start() else: io_loop.add_handler(Q1._reader.fileno(), lambda _, __: msg(*get()), io_loop.READ) if __debug__: ioloop.PeriodicCallback(record, 3000).start() ioloop.PeriodicCallback( lambda: tokens.update(dict.fromkeys(range(100), "token")), 1000).start() def hub_coroutine(method): @functools.wraps(method) def wrapper(*args, **kwargs): generator = method(*args, **kwargs) try: cmd, expr = next(generator) except StopIteration: return # all is done # at here, continue iter this generator def _register(c, e): hub_todo.append(_task) put([None, c, e]) def _task(echo_from_hub): try: _register(*generator.send(echo_from_hub)) # abbreviation except StopIteration: return _register(cmd, expr) return wrapper class BaseHandler(web.RequestHandler): @property def browser(self): return self.request.host[-1].isalpha() class MainHandler(BaseHandler): def get(self): cmd = urllib.parse.unquote(self.request.query) result = repr(eval(cmd, None, sys.modules)) if cmd else "" self.render("index.html", cmd=cmd, result=result) class IOHistoryHandler(BaseHandler): def get(self): if not __debug__: return step = 25 i = self.request.query with open(DEBUG_OUTPUT) as f: hist = list(f) page = int(i) if i.isdigit() else 1 pages = int((len(hist) - 1) / step) + 1 history = hist[step * (page - 1):step * page] self.render("history.html", page=page, pages=pages, history=history) class StatusHandler(BaseHandler): def get(self): if not __debug__: return self.render("status.html", recorder=recorder) class TokenUpdateHandler(BaseHandler): def get(self): """ /token?10001.key """ i, t = self.request.query.split(".", 1) tokens[int(i)] = t class HubHandler(BaseHandler): @hub_coroutine @web.asynchronous def get(self, cmd): """ /hub_eval? /hub_show? """ expr = urllib.parse.unquote(self.request.query) if cmd == "eval": self.set_header("Content-Type", "application/json") yield cmd, expr # test multi-yield self.finish((yield cmd, expr)) elif cmd == "show": echo = yield cmd, expr if isinstance(echo, str) and echo.startswith("Traceback"): self.set_header("Content-Type", "text/plain") self.finish(echo) else: self.render("show.html", echo=echo) elif cmd == "reset": hub_todo.clear() stop() start() self.finish(cmd) else: raise web.HTTPError(404) tcp(staffs, tokens, put)().listen(options.port) backdoor(hub_coroutine)().listen(options.backdoor) #, "localhost") autoreload.add_reload_hook(stop) # i like autoreload now :) web.Application( [ (r"/", MainHandler), (r"/io", IOHistoryHandler), (r"/status", StatusHandler), (r"/hub_(.*)", HubHandler), (r"/token", TokenUpdateHandler), ], static_path="static", template_path="templates", ui_modules=ui_modules, debug=__debug__, ).listen(options.backstage) try: io_loop.start() # looping... except KeyboardInterrupt: Q1.put(None) stop()
def start(self): ioloop = IOLoop.instance() autoreload.add_reload_hook(self.reload) autoreload.start(ioloop) ioloop.start()
def install_autoreload_hook(self): if self._options.debug: from tornado.autoreload import add_reload_hook add_reload_hook(self.call_shutdown_callback)