def main(arguments=None): '''Runs thumbor server with the specified arguments.''' if arguments is None: arguments = sys.argv[1:] server_parameters = get_server_parameters(arguments) config = get_config(server_parameters.config_path) configure_log(config, server_parameters.log_level.upper()) importer = get_importer(config) validate_config(config, server_parameters) context = get_context(server_parameters, config, importer) application = get_application(context) run_server(application, context) try: logging.debug('thumbor running at %s:%d' % (context.server.ip, context.server.port)) tornado.ioloop.IOLoop.instance().start() except KeyboardInterrupt: sys.stdout.write('\n') sys.stdout.write("-- thumbor closed by user interruption --\n") finally: context.thread_pool.cleanup()
def main(arguments=None): """Runs thumbor server with the specified arguments.""" server_parameters = get_server_parameters(arguments) logging.basicConfig(level=getattr(logging, server_parameters.log_level.upper())) lookup_paths = [os.curdir, expanduser("~"), "/etc/", dirname(__file__)] config = Config.load(server_parameters.config_path, conf_name="thumbor.conf", lookup_paths=lookup_paths) importer = Importer(config) importer.import_modules() if server_parameters.security_key is None: server_parameters.security_key = config.SECURITY_KEY if not isinstance(server_parameters.security_key, basestring): raise RuntimeError( "No security key was found for this instance of thumbor. Please provide one using the conf file or a security key file." ) context = Context(server=server_parameters, config=config, importer=importer) application = ThumborServiceApp(context) server = HTTPServer(application) server.bind(context.server.port, context.server.ip) server.start(1) try: logging.debug("thumbor running at %s:%d" % (context.server.ip, context.server.port)) tornado.ioloop.IOLoop.instance().start() except KeyboardInterrupt: print print "-- thumbor closed by user interruption --"
def main(arguments=None): '''Runs thumbor server with the specified arguments.''' server_parameters = get_server_parameters(arguments) logging.basicConfig( level=getattr(logging, server_parameters.log_level.upper())) config = Config.load(server_parameters.config_path) importer = Importer(config) importer.import_modules() if server_parameters.security_key is not None: config.SECURITY_KEY = server_parameters.security_key if not isinstance(config.SECURITY_KEY, basestring): raise RuntimeError( 'No security key was found for this instance of thumbor. Please provide one using the conf file or a security key file.' ) context = Context(server=server_parameters, config=config, importer=importer) application = ThumborServiceApp(context) server = HTTPServer(application) server.bind(context.server.port, context.server.ip) server.start(1) try: tornado.ioloop.IOLoop.instance().start() except KeyboardInterrupt: print print "-- thumbor closed by user interruption --"
def main(arguments=None): """Runs thumbor server with the specified arguments.""" server_parameters = get_server_parameters(arguments) lookup_paths = [os.curdir, expanduser("~"), "/etc/", dirname(__file__)] config = Config.load( server_parameters.config_path, conf_name="thumbor.conf", lookup_paths=lookup_paths, ) logging.basicConfig( level=getattr(logging, server_parameters.log_level.upper()), format=config.THUMBOR_LOG_FORMAT, datefmt=config.THUMBOR_LOG_DATE_FORMAT, ) importer = Importer(config) importer.import_modules() if importer.error_handler_class is not None: importer.error_handler = importer.error_handler_class(config) if server_parameters.security_key is None: server_parameters.security_key = config.SECURITY_KEY if not isinstance(server_parameters.security_key, basestring): raise RuntimeError( "No security key was found for this instance of thumbor. " + "Please provide one using the conf file or a security key file." ) context = Context(server=server_parameters, config=config, importer=importer) application = importer.import_class(server_parameters.app_class)(context) server = HTTPServer(application) if context.server.fd is not None: fd_number = get_as_integer(context.server.fd) if fd_number is None: with open(context.server.fd, "r") as sock: fd_number = sock.fileno() sock = socket.fromfd(fd_number, socket.AF_UNIX, socket.SOCK_STREAM) server.add_socket(sock) else: server.bind(context.server.port, context.server.ip) server.start(1) try: logging.debug( "thumbor running at %s:%d" % (context.server.ip, context.server.port) ) tornado.ioloop.IOLoop.instance().start() except KeyboardInterrupt: print("-- thumbor closed by user interruption --")
def main(arguments=None): '''Runs thumbor server with the specified arguments.''' if arguments is None: arguments = sys.argv[1:] server_parameters = get_server_parameters(arguments) config = get_config(server_parameters.config_path) configure_log(config, server_parameters.log_level.upper()) validate_config(config, server_parameters) importer = get_importer(config) with get_context(server_parameters, config, importer) as context: application = get_application(context) run_server(application, context) if (config.GC_INTERVAL and config.GC_INTERVAL > 0): schedule.every(config.GC_INTERVAL).seconds.do(gc_collect) try: logging.debug('thumbor running at %s:%d' % (context.server.ip, context.server.port)) tornado.ioloop.IOLoop.instance().start() except KeyboardInterrupt: sys.stdout.write('\n') sys.stdout.write("-- thumbor closed by user interruption --\n")
def main(arguments=None): '''Runs thumbor server with the specified arguments.''' server_parameters = get_server_parameters(arguments) logging.basicConfig(level=getattr(logging, server_parameters.log_level.upper())) config = Config.load(server_parameters.config_path) importer = Importer(config) importer.import_modules() if server_parameters.security_key is not None: config.SECURITY_KEY = server_parameters.security_key if not isinstance(config.SECURITY_KEY, basestring): raise RuntimeError('No security key was found for this instance of thumbor. Please provide one using the conf file or a security key file.') context = Context(server=server_parameters, config=config, importer=importer) application = ThumborServiceApp(context) server = HTTPServer(application) server.bind(context.server.port, context.server.ip) server.start(1) try: tornado.ioloop.IOLoop.instance().start() except KeyboardInterrupt: print print "-- thumbor closed by user interruption --"
def main(arguments=None): '''Runs thumbor server with the specified arguments.''' server_parameters = get_server_parameters(arguments) lookup_paths = [os.curdir, expanduser('~'), '/etc/', dirname(__file__)] config = Config.load(server_parameters.config_path, conf_name='thumbor.conf', lookup_paths=lookup_paths) logging.basicConfig(level=getattr(logging, server_parameters.log_level.upper()), format=config.THUMBOR_LOG_FORMAT, datefmt=config.THUMBOR_LOG_DATE_FORMAT) importer = Importer(config) importer.import_modules() if importer.error_handler_class is not None: importer.error_handler = importer.error_handler_class(config) if server_parameters.security_key is None: server_parameters.security_key = config.SECURITY_KEY if not isinstance(server_parameters.security_key, basestring): raise RuntimeError( 'No security key was found for this instance of thumbor. ' + 'Please provide one using the conf file or a security key file.') context = Context(server=server_parameters, config=config, importer=importer) application = importer.import_class(server_parameters.app_class)(context) server = HTTPServer(application) if context.server.fd is not None: fd_number = get_as_integer(context.server.fd) if fd_number is None: with open(context.server.fd, 'r') as sock: fd_number = sock.fileno() sock = socket.fromfd(fd_number, socket.AF_INET | socket.AF_INET6, socket.SOCK_STREAM) server.add_socket(sock) else: server.bind(context.server.port, context.server.ip) server.start(1) try: logging.debug('thumbor running at %s:%d' % (context.server.ip, context.server.port)) tornado.ioloop.IOLoop.instance().start() except KeyboardInterrupt: print print "-- thumbor closed by user interruption --"
def test_can_get_default_server_parameters(self): params = get_server_parameters() expect(params.port).to_equal(8888) expect(params.ip).to_equal("0.0.0.0") expect(params.config_path).to_be_null() expect(params.keyfile).to_be_null() expect(params.log_level).to_equal("warning") expect(params.app_class).to_equal("thumbor.app.ThumborServiceApp") expect(params.fd).to_be_null()
def test_can_get_default_server_parameters(self): params = get_server_parameters() expect(params.port).to_equal(8888) expect(params.ip).to_equal('0.0.0.0') expect(params.config_path).to_be_null() expect(params.keyfile).to_be_null() expect(params.log_level).to_equal('warning') expect(params.app_class).to_equal('thumbor.app.ThumborServiceApp') expect(params.fd).to_be_null()
def main(arguments=None): '''Runs thumbor server with the specified arguments.''' server_parameters = get_server_parameters(arguments) lookup_paths = [os.curdir, expanduser('~'), '/etc/', dirname(__file__)] config = Config.load(server_parameters.config_path, conf_name='thumbor.conf', lookup_paths=lookup_paths) logging.basicConfig( level=getattr(logging, server_parameters.log_level.upper()), format=config.THUMBOR_LOG_FORMAT, datefmt=config.THUMBOR_LOG_DATE_FORMAT ) importer = Importer(config) importer.import_modules() if importer.error_handler_class is not None: importer.error_handler = importer.error_handler_class(config) if server_parameters.security_key is None: server_parameters.security_key = config.SECURITY_KEY if not isinstance(server_parameters.security_key, basestring): raise RuntimeError( 'No security key was found for this instance of thumbor. ' + 'Please provide one using the conf file or a security key file.') context = Context(server=server_parameters, config=config, importer=importer) application = importer.import_class(server_parameters.app_class)(context) server = HTTPServer(application) if context.server.fd is not None: sock = socket.fromfd(context.server.fd, socket.AF_INET | socket.AF_INET6, socket.SOCK_STREAM) server.add_socket(sock) else: server.bind(context.server.port, context.server.ip) server.start(1) try: logging.debug('thumbor running at %s:%d' % (context.server.ip, context.server.port)) tornado.ioloop.IOLoop.instance().start() except KeyboardInterrupt: print print "-- thumbor closed by user interruption --"
def test_can_get_custom_server_parameters(self): params = get_server_parameters([ '--port=9999', '--ip=127.0.0.1', '--conf=/tmp/conf.conf', '--keyfile=./tests/fixtures/thumbor.key', '--log-level=debug', '--app=custom.app', '--fd=/tmp/fd', ]) expect(params.port).to_equal(9999) expect(params.ip).to_equal('127.0.0.1') expect(params.config_path).to_equal('/tmp/conf.conf') expect(params.keyfile).to_equal('./tests/fixtures/thumbor.key') expect(params.log_level).to_equal('debug') expect(params.app_class).to_equal('custom.app') expect(params.fd).to_equal('/tmp/fd')
def test_can_get_custom_server_parameters(self): params = get_server_parameters([ "--port=9999", "--ip=127.0.0.1", "--conf=/tmp/conf.conf", "--keyfile=./tests/fixtures/thumbor.key", "--log-level=debug", "--app=custom.app", "--fd=/tmp/fd", ]) expect(params.port).to_equal(9999) expect(params.ip).to_equal("127.0.0.1") expect(params.config_path).to_equal("/tmp/conf.conf") expect(params.keyfile).to_equal("./tests/fixtures/thumbor.key") expect(params.log_level).to_equal("debug") expect(params.app_class).to_equal("custom.app") expect(params.fd).to_equal("/tmp/fd")
def main(arguments=None): '''Runs thumbor server with the specified arguments.''' if arguments is None: arguments = sys.argv[1:] server_parameters = get_server_parameters(arguments) config = get_config(server_parameters.config_path, server_parameters.use_environment) configure_log(config, server_parameters.log_level.upper()) validate_config(config, server_parameters) importer = get_importer(config) with get_context(server_parameters, config, importer) as context: application = get_application(context) server = run_server(application, context) setup_signal_handler(server, config) logging.debug('thumbor running at %s:%d' % (context.server.ip, context.server.port)) tornado.ioloop.IOLoop.instance().start()
def main(arguments=None): """Runs thumbor server with the specified arguments.""" if arguments is None: arguments = sys.argv[1:] server_parameters = get_server_parameters(arguments) config = get_config(server_parameters.config_path, server_parameters.use_environment) configure_log(config, server_parameters.log_level.upper()) validate_config(config, server_parameters) importer = get_importer(config) with get_context(server_parameters, config, importer) as context: application = get_application(context) server = run_server(application, context) setup_signal_handler(server, config) tornado.ioloop.IOLoop.instance().start()
def main(arguments=None): '''Runs thumbor server with the specified arguments.''' server_parameters = get_server_parameters(arguments) logging.basicConfig( level=getattr(logging, server_parameters.log_level.upper())) lookup_paths = [os.curdir, expanduser('~'), '/etc/', dirname(__file__)] config = Config.load(server_parameters.config_path, conf_name='thumbor.conf', lookup_paths=lookup_paths) importer = Importer(config) importer.import_modules() if server_parameters.security_key is None: server_parameters.security_key = config.SECURITY_KEY if not isinstance(server_parameters.security_key, basestring): raise RuntimeError( 'No security key was found for this instance of thumbor. Please provide one using the conf file or a security key file.' ) context = Context(server=server_parameters, config=config, importer=importer) application = importer.import_class(server_parameters.app_class)(context) server = HTTPServer(application) server.bind(context.server.port, context.server.ip) server.start(1) try: logging.debug('thumbor running at %s:%d' % (context.server.ip, context.server.port)) tornado.ioloop.IOLoop.instance().start() except KeyboardInterrupt: print print "-- thumbor closed by user interruption --"
def main(): server_parameters = get_server_parameters() logging.basicConfig(level=getattr(logging, server_parameters.log_level.upper())) config = Config.load(server_parameters.config_path) importer = Importer(config) importer.import_modules() context = Context(server=server_parameters, config=config, importer=importer) handlers = [ (r'/(?P<url>.+)', DumpHandler, { 'context': context }) ] application = tornado.web.Application(handlers) server = HTTPServer(application) server.bind(context.server.port, context.server.ip) server.start(1) tornado.ioloop.IOLoop.instance().start()
def main(arguments=None): # NOQA '''Runs thumbor server with the specified arguments.''' server_parameters = get_server_parameters(arguments) lookup_paths = [os.curdir, expanduser('~'), '/etc/', dirname(__file__)] config = Config.load(server_parameters.config_path, conf_name='thumbor.conf', lookup_paths=lookup_paths) if (config.THUMBOR_LOG_CONFIG and config.THUMBOR_LOG_CONFIG != ''): logging.config.dictConfig(config.THUMBOR_LOG_CONFIG) else: logging.basicConfig( level=getattr(logging, server_parameters.log_level.upper()), format=config.THUMBOR_LOG_FORMAT, datefmt=config.THUMBOR_LOG_DATE_FORMAT ) importer = Importer(config) importer.import_modules() if importer.error_handler_class is not None: importer.error_handler = importer.error_handler_class(config) if server_parameters.security_key is None: server_parameters.security_key = config.SECURITY_KEY if not isinstance(server_parameters.security_key, basestring): raise RuntimeError( 'No security key was found for this instance of thumbor. ' + 'Please provide one using the conf file or a security key file.') if config.USE_GIFSICLE_ENGINE: server_parameters.gifsicle_path = which('gifsicle') if server_parameters.gifsicle_path is None: raise RuntimeError( 'If using USE_GIFSICLE_ENGINE configuration to True, the `gifsicle` binary must be in the PATH ' 'and must be an executable.' ) context = Context( server=server_parameters, config=config, importer=importer ) application = importer.import_class(server_parameters.app_class)(context) server = HTTPServer(application) if context.server.fd is not None: fd_number = get_as_integer(context.server.fd) if fd_number is None: with open(context.server.fd, 'r') as sock: fd_number = sock.fileno() sock = socket.fromfd(fd_number, socket.AF_INET | socket.AF_INET6, socket.SOCK_STREAM) server.add_socket(sock) else: server.bind(context.server.port, context.server.ip) server.start(1) try: logging.debug('thumbor running at %s:%d' % (context.server.ip, context.server.port)) tornado.ioloop.IOLoop.instance().start() except KeyboardInterrupt: print print "-- thumbor closed by user interruption --" finally: context.thread_pool.cleanup()
def main(arguments=None): '''Runs thumbor server with the specified arguments.''' server_parameters = get_server_parameters(arguments) lookup_paths = [os.curdir, expanduser('~'), '/etc/', dirname(__file__)] config = Config.load(server_parameters.config_path, conf_name='thumbor.conf', lookup_paths=lookup_paths) if (config.THUMBOR_LOG_CONFIG and config.THUMBOR_LOG_CONFIG != ''): logging.config.dictConfig(config.THUMBOR_LOG_CONFIG) else: logging.basicConfig(level=getattr(logging, server_parameters.log_level.upper()), format=config.THUMBOR_LOG_FORMAT, datefmt=config.THUMBOR_LOG_DATE_FORMAT, filename=server_parameters.log_file) importer = Importer(config) importer.import_modules() if importer.error_handler_class is not None: importer.error_handler = importer.error_handler_class(config) if server_parameters.security_key is None: server_parameters.security_key = config.SECURITY_KEY if not isinstance(server_parameters.security_key, basestring): raise RuntimeError( 'No security key was found for this instance of thumbor. ' + 'Please provide one using the conf file or a security key file.') if config.USE_GIFSICLE_ENGINE: server_parameters.gifsicle_path = which('gifsicle') if server_parameters.gifsicle_path is None: raise RuntimeError( 'If using USE_GIFSICLE_ENGINE configuration to True, the `gifsicle` binary must be in the PATH and must be an executable.' ) context = Context(server=server_parameters, config=config, importer=importer) application = importer.import_class(server_parameters.app_class)(context) server = HTTPServer(application) if context.server.fd is not None: fd_number = get_as_integer(context.server.fd) if fd_number is None: with open(context.server.fd, 'r') as sock: fd_number = sock.fileno() sock = socket.fromfd(fd_number, socket.AF_INET | socket.AF_INET6, socket.SOCK_STREAM) server.add_socket(sock) else: server.bind(context.server.port, context.server.ip) server.start(1) # Adapted from gist.github.com/mywaiting/4643396. Note: This function is # only ever executed as a callback by the main IO loop. Therefore all # calls to it are guaranteed to be serialized, so it doesn't have to be # either thread-safe or reentrant. global shutting_down shutting_down = False def shutdown(): global shutting_down if shutting_down: return shutting_down = True logging.critical('Stopping server. No longer accepting connections') server.stop() logging.critical('Shutdown in at most %d seconds', config.MAX_WAIT_BEFORE_SHUTDOWN) io_loop = tornado.ioloop.IOLoop.instance() deadline = time.time() + config.MAX_WAIT_BEFORE_SHUTDOWN def stop_loop(): now = time.time() if now < deadline and (io_loop._callbacks or io_loop._timeouts): io_loop.add_timeout(min(now + 1, deadline), stop_loop) else: logging.critical('Stopping IO loop and exiting') io_loop.stop() stop_loop() def sig_handler(sig, frame): # Stdlib Logging functions are not reentrant. # logging.warning('Caught signal: %s', sig) tornado.ioloop.IOLoop.instance().add_callback_from_signal(shutdown) signal.signal(signal.SIGTERM, sig_handler) signal.signal(signal.SIGINT, sig_handler) try: logging.debug('thumbor running at %s:%d' % (context.server.ip, context.server.port)) tornado.ioloop.IOLoop.instance().start() except KeyboardInterrupt: print print "-- thumbor closed by user interruption --" finally: context.thread_pool.cleanup()
#!/usr/bin/python # -*- coding: utf-8 -*- import os from thumbor.console import get_server_parameters from thumbor.server import get_config, configure_log from thumbor.server import get_importer, validate_config from thumbor.server import get_context, get_application thumbor_port = os.environ.get('THUMBOR_PORT') or '8000' log_level = os.environ.get('LOG_LEVEL') or 'info' arguments = [ '--conf=/app/thumbor.conf', '--port=' + thumbor_port, '--log-level=' + log_level ] server_parameters = get_server_parameters(arguments) config = get_config(server_parameters.config_path) configure_log(config, server_parameters.log_level.upper()) importer = get_importer(config) validate_config(config, server_parameters) context = get_context(server_parameters, config, importer) application = get_application(context)
cachefile = storage.path_on_filesystem(quote(url)) print "\t" + cachefile if os.path.isfile(cachefile): os.unlink(cachefile) print "\tRemoved" else: print "\tNot in cache" if (len(sys.argv) != 2): print 'Usage: ' + sys.argv[0] + ' url/file' exit(1) server_parameters = get_server_parameters() lookup_paths = ['/etc/'] config = Config.load(server_parameters.config_path, conf_name='thumbor.conf', lookup_paths=lookup_paths) importer = None context = Context(server=server_parameters, config=config, importer=importer) storage = Storage(context) if os.path.exists(sys.argv[1]): with open(sys.argv[1]) as f: for url in f: flush(url.rstrip())
def main(arguments=None): '''Runs thumbor server with the specified arguments.''' server_parameters = get_server_parameters(arguments) lookup_paths = [os.curdir, expanduser('~'), '/etc/', dirname(__file__)] config = Config.load(server_parameters.config_path, conf_name='thumbor.conf', lookup_paths=lookup_paths) if (config.THUMBOR_LOG_CONFIG and config.THUMBOR_LOG_CONFIG != '') : logging.config.dictConfig(config.THUMBOR_LOG_CONFIG) else: logging.basicConfig( level=getattr(logging, server_parameters.log_level.upper()), format=config.THUMBOR_LOG_FORMAT, datefmt=config.THUMBOR_LOG_DATE_FORMAT, filename=server_parameters.log_file ) importer = Importer(config) importer.import_modules() if importer.error_handler_class is not None: importer.error_handler = importer.error_handler_class(config) if server_parameters.security_key is None: server_parameters.security_key = config.SECURITY_KEY if not isinstance(server_parameters.security_key, basestring): raise RuntimeError( 'No security key was found for this instance of thumbor. ' + 'Please provide one using the conf file or a security key file.') if config.USE_GIFSICLE_ENGINE: server_parameters.gifsicle_path = which('gifsicle') if server_parameters.gifsicle_path is None: raise RuntimeError('If using USE_GIFSICLE_ENGINE configuration to True, the `gifsicle` binary must be in the PATH and must be an executable.') context = Context( server=server_parameters, config=config, importer=importer ) application = importer.import_class(server_parameters.app_class)(context) server = HTTPServer(application) if context.server.fd is not None: fd_number = get_as_integer(context.server.fd) if fd_number is None: with open(context.server.fd, 'r') as sock: fd_number = sock.fileno() sock = socket.fromfd(fd_number, socket.AF_INET | socket.AF_INET6, socket.SOCK_STREAM) server.add_socket(sock) else: server.bind(context.server.port, context.server.ip) server.start(1) # Adapted from gist.github.com/mywaiting/4643396. Note: This function is # only ever executed as a callback by the main IO loop. Therefore all # calls to it are guaranteed to be serialized, so it doesn't have to be # either thread-safe or reentrant. global shutting_down shutting_down = False def shutdown(): global shutting_down if shutting_down: return shutting_down = True logging.critical('Stopping server. No longer accepting connections') server.stop() logging.critical('Shutdown in at most %d seconds', config.MAX_WAIT_BEFORE_SHUTDOWN) io_loop = tornado.ioloop.IOLoop.instance() deadline = time.time() + config.MAX_WAIT_BEFORE_SHUTDOWN def stop_loop(): now = time.time() if now < deadline and (io_loop._callbacks or io_loop._timeouts): io_loop.add_timeout(min(now + 1, deadline), stop_loop) else: logging.critical('Stopping IO loop and exiting') io_loop.stop() stop_loop() def sig_handler(sig, frame): # Stdlib Logging functions are not reentrant. # logging.warning('Caught signal: %s', sig) tornado.ioloop.IOLoop.instance().add_callback_from_signal(shutdown) signal.signal(signal.SIGTERM, sig_handler) signal.signal(signal.SIGINT, sig_handler) try: logging.debug('thumbor running at %s:%d' % (context.server.ip, context.server.port)) tornado.ioloop.IOLoop.instance().start() except KeyboardInterrupt: print print "-- thumbor closed by user interruption --" finally: context.thread_pool.cleanup()