Learn more  » Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Bower components Debian packages RPM packages NuGet packages

edgify / rook   python

Repository URL to install this package:

Version: 0.1.176 

/ serverless.py

import os
from functools import wraps

import rook


def on_lambda():
    return 'LAMBDA_TASK_ROOT' in os.environ


def serverless_rook(f):

    if on_lambda():
        @wraps(f)
        def handler(*args, **kwargs):
            rook.start()

            ret = f(*args, **kwargs)

            rook.flush()
            return ret
    else:
        handler = f

    return handler


def setup_zappa_support():

    if on_lambda():
        try:
            from zappa.middleware import ZappaWSGIMiddleware

            original_handler = ZappaWSGIMiddleware.__call__

            @serverless_rook
            def zappa_hook(self, *args, **kwargs):
                return original_handler(self, *args, **kwargs)

            ZappaWSGIMiddleware.__call__ = zappa_hook

        except ImportError:
            # can happen if its not zappa
            pass
    else:
        pass


try:
    from chalice import Chalice

    class RookoutChalice(Chalice):
        @serverless_rook
        def __call__(self, *args, **kwargs):
            return super(RookoutChalice, self).__call__(*args, **kwargs)

except ImportError: # If Chalice is not availale, we just won't have a wrapper for it
    pass