Module: SignalWire::Runtime

Defined in:
lib/signalwire/runtime.rb

Overview

Runtime environment detection.

Detects the execution environment (plain server, AWS Lambda, CGI, Google Cloud Functions, Azure Functions) by inspecting well-known environment variables set by each platform.

This is the Ruby counterpart to the Python SDK’s signalwire.core.logging_config.get_execution_mode.

Constant Summary collapse

MODES =
%i[server lambda cgi google_cloud_function azure_function unknown].freeze

Class Method Summary collapse

Class Method Details

.execution_modeSymbol

Determine the current execution mode.

Returns one of:

  • :cgi - running under a CGI gateway (GATEWAY_INTERFACE is set)

  • :lambda - running under AWS Lambda

  • :google_cloud_function - Google Cloud Functions / Cloud Run

  • :azure_function - Azure Functions

  • :server - long-running HTTP server (the default)

Detection order matters: CGI is checked before Lambda because a Lambda function invoked through an emulator that also sets GATEWAY_INTERFACE should still be treated as CGI.

Returns:

  • (Symbol)

    one of the values in MODES



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
# File 'lib/signalwire/runtime.rb', line 34

def self.execution_mode
  # CGI environment (e.g. Apache mod_cgi)
  return :cgi if ENV['GATEWAY_INTERFACE'] && !ENV['GATEWAY_INTERFACE'].empty?

  # AWS Lambda
  if (ENV['AWS_LAMBDA_FUNCTION_NAME'] && !ENV['AWS_LAMBDA_FUNCTION_NAME'].empty?) ||
     (ENV['LAMBDA_TASK_ROOT'] && !ENV['LAMBDA_TASK_ROOT'].empty?)
    return :lambda
  end

  # Google Cloud Functions / Cloud Run
  if (ENV['FUNCTION_TARGET'] && !ENV['FUNCTION_TARGET'].empty?) ||
     (ENV['K_SERVICE'] && !ENV['K_SERVICE'].empty?) ||
     (ENV['GOOGLE_CLOUD_PROJECT'] && !ENV['GOOGLE_CLOUD_PROJECT'].empty?)
    return :google_cloud_function
  end

  # Azure Functions
  if (ENV['AZURE_FUNCTIONS_ENVIRONMENT'] && !ENV['AZURE_FUNCTIONS_ENVIRONMENT'].empty?) ||
     (ENV['FUNCTIONS_WORKER_RUNTIME'] && !ENV['FUNCTIONS_WORKER_RUNTIME'].empty?) ||
     (ENV['AzureWebJobsStorage'] && !ENV['AzureWebJobsStorage'].empty?)
    return :azure_function
  end

  :server
end

.lambda?Boolean

True when the SDK is running inside AWS Lambda.

Returns:

  • (Boolean)


63
64
65
# File 'lib/signalwire/runtime.rb', line 63

def self.lambda?
  execution_mode == :lambda
end

.lambda_base_urlString?

Construct the base URL for the current Lambda function.

Prefers AWS_LAMBDA_FUNCTION_URL when set; otherwise falls back to the standard Function URL shape built from AWS_LAMBDA_FUNCTION_NAME and AWS_REGION. Returns nil when neither signal is present.

The returned URL never has a trailing slash and never contains a path component, so callers must append the agent’s route themselves.

Returns:

  • (String, nil)


85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/signalwire/runtime.rb', line 85

def self.lambda_base_url
  explicit = ENV['AWS_LAMBDA_FUNCTION_URL']
  return explicit.chomp('/') if explicit && !explicit.empty?

  function_name = ENV['AWS_LAMBDA_FUNCTION_NAME']
  return nil if function_name.nil? || function_name.empty?

  region = ENV['AWS_REGION']
  region = 'us-east-1' if region.nil? || region.empty?

  "https://#{function_name}.lambda-url.#{region}.on.aws"
end

.serverless?Boolean

True when the SDK is running inside any serverless platform.

Returns:

  • (Boolean)


69
70
71
72
73
# File 'lib/signalwire/runtime.rb', line 69

def self.serverless?
  mode = execution_mode
  mode == :lambda || mode == :cgi ||
    mode == :google_cloud_function || mode == :azure_function
end