Class: LambdaContext

Inherits:
Object
  • Object
show all
Defined in:
lib/aws_lambda_ric/lambda_context.rb

Overview

frozen_string_literal: true

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(request) ⇒ LambdaContext

Returns a new instance of LambdaContext.



10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/aws_lambda_ric/lambda_context.rb', line 10

def initialize(request)
  @clock_diff = Process.clock_gettime(Process::CLOCK_REALTIME, :millisecond) - Process.clock_gettime(Process::CLOCK_MONOTONIC, :millisecond)
  @deadline_ms = request['Lambda-Runtime-Deadline-Ms'].to_i
  @aws_request_id = request['Lambda-Runtime-Aws-Request-Id']
  @invoked_function_arn = request['Lambda-Runtime-Invoked-Function-Arn']
  @log_group_name = ENV['AWS_LAMBDA_LOG_GROUP_NAME']
  @log_stream_name = ENV['AWS_LAMBDA_LOG_STREAM_NAME']
  @function_name = ENV['AWS_LAMBDA_FUNCTION_NAME']
  @memory_limit_in_mb = ENV['AWS_LAMBDA_FUNCTION_MEMORY_SIZE']
  @function_version = ENV['AWS_LAMBDA_FUNCTION_VERSION']
  @identity = JSON.parse(request['Lambda-Runtime-Cognito-Identity']) if request['Lambda-Runtime-Cognito-Identity']
  @client_context = JSON.parse(request['Lambda-Runtime-Client-Context']) if request['Lambda-Runtime-Client-Context']
end

Instance Attribute Details

#aws_request_idObject (readonly)

Returns the value of attribute aws_request_id.



6
7
8
# File 'lib/aws_lambda_ric/lambda_context.rb', line 6

def aws_request_id
  @aws_request_id
end

#client_contextObject (readonly)

Returns the value of attribute client_context.



6
7
8
# File 'lib/aws_lambda_ric/lambda_context.rb', line 6

def client_context
  @client_context
end

#deadline_msObject (readonly)

Returns the value of attribute deadline_ms.



6
7
8
# File 'lib/aws_lambda_ric/lambda_context.rb', line 6

def deadline_ms
  @deadline_ms
end

#function_nameObject (readonly)

Returns the value of attribute function_name.



6
7
8
# File 'lib/aws_lambda_ric/lambda_context.rb', line 6

def function_name
  @function_name
end

#function_versionObject (readonly)

Returns the value of attribute function_version.



6
7
8
# File 'lib/aws_lambda_ric/lambda_context.rb', line 6

def function_version
  @function_version
end

#identityObject (readonly)

Returns the value of attribute identity.



6
7
8
# File 'lib/aws_lambda_ric/lambda_context.rb', line 6

def identity
  @identity
end

#invoked_function_arnObject (readonly)

Returns the value of attribute invoked_function_arn.



6
7
8
# File 'lib/aws_lambda_ric/lambda_context.rb', line 6

def invoked_function_arn
  @invoked_function_arn
end

#log_group_nameObject (readonly)

Returns the value of attribute log_group_name.



6
7
8
# File 'lib/aws_lambda_ric/lambda_context.rb', line 6

def log_group_name
  @log_group_name
end

#log_stream_nameObject (readonly)

Returns the value of attribute log_stream_name.



6
7
8
# File 'lib/aws_lambda_ric/lambda_context.rb', line 6

def log_stream_name
  @log_stream_name
end

#memory_limit_in_mbObject (readonly)

Returns the value of attribute memory_limit_in_mb.



6
7
8
# File 'lib/aws_lambda_ric/lambda_context.rb', line 6

def memory_limit_in_mb
  @memory_limit_in_mb
end

Instance Method Details

#get_remaining_time_in_millisObject



24
25
26
27
28
# File 'lib/aws_lambda_ric/lambda_context.rb', line 24

def get_remaining_time_in_millis
  now = Process.clock_gettime(Process::CLOCK_MONOTONIC, :millisecond) + @clock_diff
  remaining = @deadline_ms - now
  remaining.positive? ? remaining : 0
end