|
| 1 | +# Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | + |
| 3 | +class LambdaContext |
| 4 | + attr_reader :aws_request_id, :invoked_function_arn, :log_group_name, |
| 5 | + :log_stream_name, :function_name, :memory_limit_in_mb, :function_version, |
| 6 | + :identity, :client_context, :ce, :deadline_ms |
| 7 | + |
| 8 | + def initialize(request) |
| 9 | + @clock_diff = Process.clock_gettime(Process::CLOCK_REALTIME, :millisecond) - Process.clock_gettime(Process::CLOCK_MONOTONIC, :millisecond) |
| 10 | + @deadline_ms = request['Lambda-Runtime-Deadline-Ms'].to_i |
| 11 | + @aws_request_id = request['Lambda-Runtime-Aws-Request-Id'] |
| 12 | + @invoked_function_arn = request['Lambda-Runtime-Invoked-Function-Arn'] |
| 13 | + @log_group_name = ENV['AWS_LAMBDA_LOG_GROUP_NAME'] |
| 14 | + @log_stream_name = ENV['AWS_LAMBDA_LOG_STREAM_NAME'] |
| 15 | + @function_name = ENV["AWS_LAMBDA_FUNCTION_NAME"] |
| 16 | + @memory_limit_in_mb = ENV['AWS_LAMBDA_FUNCTION_MEMORY_SIZE'] |
| 17 | + @function_version = ENV['AWS_LAMBDA_FUNCTION_VERSION'] |
| 18 | + if request['Lambda-Runtime-Cognito-Identity'] |
| 19 | + @identity = JSON.parse(request['Lambda-Runtime-Cognito-Identity']) |
| 20 | + end |
| 21 | + if request['Lambda-Runtime-Client-Context'] |
| 22 | + @client_context = JSON.parse(request['Lambda-Runtime-Client-Context']) |
| 23 | + end |
| 24 | + if request['Lambda-Runtime-Cloudevents-Context'] |
| 25 | + @ce = JSON.parse(request['Lambda-Runtime-Cloudevents-Context']) |
| 26 | + end |
| 27 | + end |
| 28 | + |
| 29 | + def get_remaining_time_in_millis |
| 30 | + now = Process.clock_gettime(Process::CLOCK_MONOTONIC, :millisecond) + @clock_diff |
| 31 | + remaining = @deadline_ms - now |
| 32 | + remaining > 0 ? remaining : 0 |
| 33 | + end |
| 34 | +end |
0 commit comments