-
Notifications
You must be signed in to change notification settings - Fork 0
/
runtime.rb
executable file
·78 lines (73 loc) · 2.49 KB
/
runtime.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#!/usr/bin/env ruby
require_relative 'lambda_errors'
require_relative 'lambda_server'
require_relative 'lambda_handler'
require_relative 'lambda_context'
require_relative 'lambda_logger'
require_relative 'aws_lambda_marshaller'
@env_handler = ENV["_HANDLER"]
@lambda_server = LambdaServer.new
STDOUT.sync = true # Ensures that logs are flushed promptly.
runtime_loop_active = true # if false, we will exit the program
exit_code = 0
begin
@lambda_handler = LambdaHandler.new(env_handler: @env_handler)
require @lambda_handler.handler_file_name
rescue Exception => e # which includes LoadError or any exception within static user code
runtime_loop_active = false
exit_code = -4
ex = LambdaErrors::LambdaRuntimeInitError.new(e)
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
while runtime_loop_active
begin
request_id, raw_request = @lambda_server.next_invocation
if trace_id = raw_request['Lambda-Runtime-Trace-Id']
ENV["_X_AMZN_TRACE_ID"] = trace_id
end
request = AwsLambda::Marshaller.marshall_request(raw_request)
rescue LambdaErrors::InvocationError => e
runtime_loop_active = false # ends the loop
raise e # ends the process
end
begin
context = LambdaContext.new(raw_request) # pass in opts
# start of user code
handler_response, content_type = @lambda_handler.call_handler(
request: request,
context: context
)
# end of user code
@lambda_server.send_response(
request_id: 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")
@lambda_server.send_error_response(
request_id: request_id,
error_object: e.to_lambda_response,
error: e
)
rescue LambdaErrors::LambdaHandlerCriticalException => e
LambdaLogger.log_error(exception: e, message: "Critical exception from handler")
@lambda_server.send_error_response(
request_id: request_id,
error_object: e.to_lambda_response,
error: e
)
runtime_loop_active = false
exit_code = -1
rescue LambdaErrors::LambdaRuntimeError => e
@lambda_server.send_error_response(
request_id: request_id,
error_object: e.to_lambda_response,
error: e
)
runtime_loop_active = false
exit_code = -2
end
end
exit(exit_code)