Exemplo n.º 1
0
    def _execute(self,
                 func_arn,
                 func_details,
                 event,
                 context=None,
                 version=None):
        lambda_cwd = func_details.cwd
        environment = func_details.envvars.copy()

        # execute the Lambda function in a forked sub-process, sync result via queue
        queue = Queue()

        lambda_function = func_details.function(version)

        def do_execute():
            # now we're executing in the child process, safe to change CWD and ENV
            if lambda_cwd:
                os.chdir(lambda_cwd)
            if environment:
                os.environ.update(environment)
            result = lambda_function(event, context)
            queue.put(result)

        process = Process(target=do_execute)
        with CaptureOutput() as c:
            process.run()
        result = queue.get()
        # TODO: Interweaving stdout/stderr currently not supported
        log_output = ''
        for stream in (c.stdout(), c.stderr()):
            if stream:
                log_output += ('\n' if log_output else '') + stream
        return result, log_output
Exemplo n.º 2
0
    def _execute(self, func_arn, func_details, event, context=None, version=None):
        lambda_cwd = func_details.cwd
        environment = func_details.envvars.copy()

        # execute the Lambda function in a forked sub-process, sync result via queue
        queue = Queue()

        lambda_function = func_details.function(version)

        def do_execute():
            # now we're executing in the child process, safe to change CWD and ENV
            if lambda_cwd:
                os.chdir(lambda_cwd)
            if environment:
                os.environ.update(environment)
            result = lambda_function(event, context)
            queue.put(result)

        process = Process(target=do_execute)
        with CaptureOutput() as c:
            process.run()
        result = queue.get()

        # Make sure to keep the log line below, to ensure the log stream gets created
        log_output = 'START: Lambda %s started via "local" executor ...' % func_arn
        # TODO: Interweaving stdout/stderr currently not supported
        for stream in (c.stdout(), c.stderr()):
            if stream:
                log_output += ('\n' if log_output else '') + stream

        # store logs to CloudWatch
        _store_logs(func_details, log_output)

        return result
Exemplo n.º 3
0
    def _execute(self,
                 func_arn,
                 func_details,
                 event,
                 context=None,
                 version=None):
        lambda_cwd = func_details.cwd
        environment = self._prepare_environment(func_details)

        # execute the Lambda function in a forked sub-process, sync result via queue
        queue = Queue()

        lambda_function = func_details.function(version)

        def do_execute():
            # now we're executing in the child process, safe to change CWD and ENV
            path_before = sys.path
            try:
                if lambda_cwd:
                    os.chdir(lambda_cwd)
                    sys.path = [lambda_cwd] + sys.path
                if environment:
                    os.environ.update(environment)
                result = lambda_function(event, context)
                queue.put(result)
            finally:
                sys.path = path_before

        process = Process(target=do_execute)
        start_time = now(millis=True)
        with CaptureOutput() as c:
            process.run()
        result = queue.get()
        end_time = now(millis=True)

        # Make sure to keep the log line below, to ensure the log stream gets created
        request_id = long_uid()
        log_output = 'START %s: Lambda %s started via "local" executor ...' % (
            request_id, func_arn)
        # TODO: Interweaving stdout/stderr currently not supported
        for stream in (c.stdout(), c.stderr()):
            if stream:
                log_output += ('\n' if log_output else '') + stream
        log_output += '\nEND RequestId: %s' % request_id
        log_output += '\nREPORT RequestId: %s Duration: %s ms' % (
            request_id, int((end_time - start_time) * 1000))

        # store logs to CloudWatch
        _store_logs(func_details, log_output)

        result = result.result if isinstance(result,
                                             InvocationResult) else result
        invocation_result = InvocationResult(result, log_output=log_output)
        return invocation_result
Exemplo n.º 4
0
    def _execute(self,
                 func_arn,
                 func_details,
                 event,
                 context=None,
                 version=None):
        lambda_cwd = func_details.cwd
        environment = self._prepare_environment(func_details)

        # execute the Lambda function in a forked sub-process, sync result via queue
        queue = Queue()

        lambda_function = func_details.function(version)

        def do_execute():
            # now we're executing in the child process, safe to change CWD and ENV
            result = None
            try:
                if lambda_cwd:
                    os.chdir(lambda_cwd)
                    sys.path.insert(0, '')
                if environment:
                    os.environ.update(environment)
                result = lambda_function(event, context)
            except Exception as e:
                result = str(e)
                sys.stderr.write('%s %s' % (e, traceback.format_exc()))
                raise
            finally:
                queue.put(result)

        process = Process(target=do_execute)
        start_time = now(millis=True)
        error = None
        with CaptureOutput() as c:
            try:
                process.run()
            except Exception as e:
                error = e
        result = queue.get()
        end_time = now(millis=True)

        # Make sure to keep the log line below, to ensure the log stream gets created
        request_id = long_uid()
        log_output = 'START %s: Lambda %s started via "local" executor ...' % (
            request_id, func_arn)
        # TODO: Interweaving stdout/stderr currently not supported
        for stream in (c.stdout(), c.stderr()):
            if stream:
                log_output += ('\n' if log_output else '') + stream
        log_output += '\nEND RequestId: %s' % request_id
        log_output += '\nREPORT RequestId: %s Duration: %s ms' % (
            request_id, int((end_time - start_time) * 1000))

        # store logs to CloudWatch
        _store_logs(func_details, log_output)

        result = result.result if isinstance(result,
                                             InvocationResult) else result

        if error:
            LOG.info('Error executing Lambda "%s": %s %s' %
                     (func_arn, error, ''.join(
                         traceback.format_tb(error.__traceback__))))
            raise InvocationException(result, log_output)

        invocation_result = InvocationResult(result, log_output=log_output)
        return invocation_result