Esempio n. 1
0
def load_handler_failed_handler(e, modname):
    if isinstance(e, ImportError):
        return make_fault_handler(
            wsgi.FaultException("Unable to import module '{}'".format(modname),
                                str(e), None))
    elif isinstance(e, SyntaxError):
        trace = "File \"%s\" Line %s\n\t%s" % (e.filename, e.lineno, e.text)
        fault = wsgi.FaultException(
            "Syntax error in module '{}'".format(modname), str(e), trace)
    else:
        exc_info = sys.exc_info()
        trace = traceback.format_list(traceback.extract_tb(exc_info[2]))
        fault = wsgi.FaultException("module initialization error", str(e),
                                    trace[1:])
    return make_fault_handler(fault)
Esempio n. 2
0
def _get_handlers(handler, mode):
    lambda_runtime.report_user_init_start()
    init_handler = lambda: None
    """
    This is the old way we were loading modules.
    It was causing intermittent build failures for unknown reasons.
    Using the imp module seems to remove these failures.
    The imp module appears to be more extreme in that it reloads
    the module if it is already loaded, and it likely doesn't use any caches when
    searching for the module but does a full directory search, which is what we want.
    """
    # m = imp.load_module(modname, globals(), locals(), [])

    try:
        (modname, fname) = handler.rsplit('.', 1)
    except ValueError as e:
        fault = wsgi.FaultException("Bad handler '{}'".format(handler), str(e),
                                    None)
        request_handler = make_fault_handler(fault)
        lambda_runtime.report_user_init_end()
        return init_handler, request_handler

    file_handle, pathname, desc = None, None, None
    try:
        # Recursively loading handler in nested directories
        for segment in modname.split('.'):
            if pathname is not None:
                pathname = [pathname]
            file_handle, pathname, desc = imp.find_module(segment, pathname)
        if file_handle is None:
            module_type = desc[2]
            if module_type == imp.C_BUILTIN:
                request_handler = make_fault_handler(
                    wsgi.FaultException(
                        "Cannot use built-in module {} as a handler module".
                        format(modname), None, None))
                lambda_runtime.report_user_init_end()
                return init_handler, request_handler
        m = imp.load_module(modname, file_handle, pathname, desc)
    except Exception as e:
        request_handler = load_handler_failed_handler(e, modname)
        lambda_runtime.report_user_init_end()
        return init_handler, request_handler
    finally:
        if file_handle is not None:
            file_handle.close()

    try:
        init_handler = getattr(m, 'init')
    except AttributeError as e:
        pass

    try:
        request_handler = make_final_handler(getattr(m, fname), mode)
    except AttributeError as e:
        fault = wsgi.FaultException(
            "Handler '{}' missing on module '{}'".format(fname, modname),
            str(e), None)
        request_handler = make_fault_handler(fault)
    lambda_runtime.report_user_init_end()
    return init_handler, request_handler
Esempio n. 3
0
def invoke_http(handlerfn, sockfd):
    fault_data = wsgi.handle_one(sockfd, ('localhost', 80), handlerfn)
    if fault_data:
        raise wsgi.FaultException(fault_data.msg, fault_data.except_value,
                                  fault_data.trace)
Esempio n. 4
0
 def result(sockfd):
     raise wsgi.FaultException("specified mode is invalid: " + mode)
Esempio n. 5
0
def _get_handlers(handler, mode, invokeid, throttled=False):
    lambda_runtime.report_user_init_start()
    init_handler = lambda: None
    """
    This is the old way we were loading modules.
    It was causing intermittent build failures for unknown reasons.
    Using the imp module seems to remove these failures.
    The imp module appears to be more extreme in that it reloads
    the module if it is already loaded, and it likely doesn't use any caches when
    searching for the module but does a full directory search, which is what we want.
    """
    # m = imp.load_module(modname, globals(), locals(), [])

    try:
        (modname, fname) = handler.rsplit('.', 1)
    except ValueError as e:
        fault = wsgi.FaultException("Bad handler '{}'".format(handler), str(e),
                                    None)
        request_handler = make_fault_handler(fault)
        lambda_runtime.report_user_init_end()
        return init_handler, request_handler

    file_handle, pathname, desc = None, None, None
    try:
        # Recursively loading handler in nested directories
        for segment in modname.split('.'):
            if pathname is not None:
                pathname = [pathname]
            file_handle, pathname, desc = imp.find_module(segment, pathname)
        if file_handle is None:
            module_type = desc[2]
            if module_type == imp.C_BUILTIN:
                request_handler = make_fault_handler(
                    wsgi.FaultException(
                        "Cannot use built-in module {} as a handler module".
                        format(modname), None, None))
                lambda_runtime.report_user_init_end()
                return init_handler, request_handler
        m = imp.load_module(modname, file_handle, pathname, desc)
    except Exception as e:
        request_handler = load_handler_failed_handler(e, modname)
        lambda_runtime.report_user_init_end()
        # if the module load failed, usually we'd defer the error to the first INVOKE
        # if the throttled flag is set, there's another service tracking error states,
        # so we'll report the fault and exit to fail the INIT.
        #
        # request_handler constructed by load_handler_failed handler should always throw wsgi.FaultException.
        # This exception type has a .fatal field which signals if the runtime believes the error is not rety-able.
        # the report_fault helper returns this as it's 3rd return value.
        if throttled:
            try:
                request_handler()
            except Exception as e:
                errortype, result, fatal = report_fault(invokeid, e)
                if fatal:
                    lambda_runtime.report_done(invokeid, errortype, result, 1)
                    sys.exit(1)
        return init_handler, request_handler
    finally:
        if file_handle is not None:
            file_handle.close()

    try:
        init_handler = getattr(m, 'init')
    except AttributeError as e:
        pass

    try:
        request_handler = make_final_handler(getattr(m, fname), mode)
    except AttributeError as e:
        fault = wsgi.FaultException(
            "Handler '{}' missing on module '{}'".format(fname, modname),
            str(e), None)
        request_handler = make_fault_handler(fault)
    lambda_runtime.report_user_init_end()
    return init_handler, request_handler