Environment

The Environment is a container for request handlers, managed objects and other runtime settings as well as the tornado.web.Application settings.

There are two cases where you will need to work with the it: during the bootstrapping phase you may change paths to look for configuration files and you will add the request handlers to the environment. In addition to that you can also use it from within a request handler in and access managed objects, such as HTTP clients that can be used accross a number of client libraries for connection pooling, e.g.

class supercell.api.environment.Environment[source]

Environment for supercell processes.

add_handler(path, handler_class, init_dict=None, name=None, host_pattern='.*$', cache=None, expires=None)[source]

Add a handler to the tornado.web.Application.

The environment will manage the available request handlers and managed objects. So in the Service.run() method you will add the handlers:

class MyService(s.Service):

    def run():
        self.environment.add_handler('/', Handler)
Parameters:
  • path (str or re.pattern) – The regular expression for the URL path the handler should be bound to.
  • handler_class (supercell.api.RequestHandler) – The request handler class
  • init_dict (dict) – The initialization dict that is passed to the RequestHandler.initialize() method.
  • name (str) – If set the handler and its URL will be available in the RequestHandler.reverse_url() method.
  • host_pattern (str) – A regular expression for matching the hostname the handler will be bound to. By default this will match all hosts (‘.*$’)
  • cache (supercell.api.cache.CacheConfig) – Cache info for GET and HEAD requests to this handler defined by supercell.api.cache.CacheConfig.
  • expires (datetime.timedelta) – Set the Expires header according to the provided timedelta
add_health_check(name, check)[source]

Add a health check to the API.

Parameters:
  • name (str) – The name for the health check to add
  • check (A supercell.api.RequestHandler) – The request handler performing the health check
add_managed_object(name, instance)[source]

Add a managed instance to the environment.

A managed object is identified by a name and you can then access it from the environment as an attribute, so in your request handler you may:

class MyService(s.Service):

    def run(self):
        managed = HeavyObjectFactory.get_heavy_object()
        self.environment.add_managed_object('managed', managed)

class MyHandler(s.RequestHandler):

    def get(self):
        self.environment.managed
Parameters:
  • name (str) – The managed object identifier
  • instance (object) – Some arbitrary instance
config_file_paths[source]

The list containing all paths to look for config files.

In order to manipulate the paths looked for configuration files just manipulate this list:

class MyService(s.Service):

    def bootstrap(self):
        self.environment.config_file_paths.append('/etc/myservice/')
        self.environment.config_file_paths.append('./etc/')
config_name[source]

Determine the configuration file name for the machine this application is running on.

The filenames are generated using a combination of username and machine name. If you deploy the application as user webapp on host fe01.local.dev the configuration name would be webapp_fe01.local.dev.cfg.

get_application(config=None)[source]

Create the tornado application.

Parameters:config – The configuration that will be added to the app
get_cache_info(handler)[source]

Return the supercell.api.cache.CacheConfig for a certain handler.

get_expires_info(handler)[source]

Return the timedelta for a specific handler that should define the Expires header for GET and HEAD requests.

health_checks[source]

Simple property access for health checks.

tornado_settings[source]

The dictionary passed to the tornado.web.Application containing all relevant tornado server settings.

Read the Docs v: v0.4.0
Versions
latest
v0.4.0
v0.3.0
Downloads
On Read the Docs
Project Home
Builds

Free document hosting provided by Read the Docs.