class AwsLambdaRuntimeInterfaceClient::LambdaRunner
Loads the user code and runs it upon invocation
Constants
- ENV_VAR_RUNTIME_API
Public Class Methods
new(runtime_server_addr, user_agent)
click to toggle source
# File lib/aws_lambda_ric.rb, line 29 def initialize(runtime_server_addr, user_agent) @lambda_server = LambdaServer.new(runtime_server_addr, user_agent) @runtime_loop_active = true # if false, we will exit the program @exit_code = 0 end
Public Instance Methods
run(app_root, handler)
click to toggle source
# File lib/aws_lambda_ric.rb, line 35 def run(app_root, handler) $LOAD_PATH.unshift(app_root) unless $LOAD_PATH.include?(app_root) begin @lambda_handler = LambdaHandler.new(env_handler: handler) require @lambda_handler.handler_file_name start_runtime_loop rescue Exception => e # which includes LoadError or any exception within static user code @runtime_loop_active = false @exit_code = -4 send_init_error_to_server(e) ensure TelemetryLoggingHelper.close end exit(@exit_code) end
Private Instance Methods
run_user_code(lambda_invocation_request)
click to toggle source
# File lib/aws_lambda_ric.rb, line 77 def run_user_code(lambda_invocation_request) context = LambdaContext.new(lambda_invocation_request.raw_request) # pass in opts # start of user code handler_response, content_type = @lambda_handler.call_handler( request: lambda_invocation_request.request, context: context ) # end of user code @lambda_server.send_response( request_id: lambda_invocation_request.request_id, response_object: handler_response, content_type: content_type ) rescue LambdaErrors::LambdaHandlerError => e LambdaLogger.log_error(exception: e, message: 'Error raised from handler method') send_error_response(lambda_invocation_request, e) rescue LambdaErrors::LambdaHandlerCriticalException => e LambdaLogger.log_error(exception: e, message: 'Critical exception from handler') send_error_response(lambda_invocation_request, e, -1, false) rescue LambdaErrors::LambdaRuntimeError => e send_error_response(lambda_invocation_request, e, -2, false) end
send_error_response(lambda_invocation, err, exit_code = nil, runtime_loop_active: true)
click to toggle source
# File lib/aws_lambda_ric.rb, line 109 def send_error_response(lambda_invocation, err, exit_code = nil, runtime_loop_active: true) error_object = err.to_lambda_response @lambda_server.send_error_response( request_id: lambda_invocation.request_id, error_object: error_object, error: err, xray_cause: XRayCause.new(error_object).as_json ) @exit_code = exit_code unless exit_code.nil? @runtime_loop_active = runtime_loop_active end
send_init_error_to_server(err)
click to toggle source
# File lib/aws_lambda_ric.rb, line 103 def send_init_error_to_server(err) ex = LambdaErrors::LambdaRuntimeInitError.new(err) LambdaLogger.log_error(exception: ex, message: "Init error when loading handler #{@env_handler}") @lambda_server.send_init_error(error_object: ex.to_lambda_response, error: ex) end
start_runtime_loop()
click to toggle source
# File lib/aws_lambda_ric.rb, line 56 def start_runtime_loop while @runtime_loop_active lambda_invocation_request = wait_for_invocation run_user_code(lambda_invocation_request) end end
wait_for_invocation()
click to toggle source
# File lib/aws_lambda_ric.rb, line 63 def wait_for_invocation request_id, raw_request = @lambda_server.next_invocation $_global_aws_request_id = request_id if (trace_id = raw_request['Lambda-Runtime-Trace-Id']) ENV['_X_AMZN_TRACE_ID'] = trace_id end request = AwsLambda::Marshaller.marshall_request(raw_request) LambdaInvocationRequest.new(request_id, raw_request, request, trace_id) rescue LambdaErrors::InvocationError => e @runtime_loop_active = false # ends the loop raise e # ends the process end