Class: Jets::CLI::Exec::Command

Inherits:
Call
  • Object
show all
Defined in:
lib/jets/cli/exec/command.rb

Instance Attribute Summary

Attributes inherited from Call

#event

Instance Method Summary collapse

Methods inherited from Call

#check_valid_json!, #function_name, #initialize, #invocation_type, #invoke, #load_event_from_file, #log_last_4kb, #verbose?

Methods included from Util::Logging

#log

Methods included from AwsServices

#apigateway, #aws_options, #cfn, #codebuild, #dynamodb, #lambda_client, #logs, #s3, #s3_resource, #sns, #sqs, #ssm, #sts, #wafv2

Methods included from AwsServices::StackStatus

#output_value, #stack_exists?

Methods included from AwsServices::GlobalMemoist

included, #reset_cache!

Constructor Details

This class inherits a constructor from Jets::CLI::Call

Instance Method Details

#friendly_function_nameObject



52
53
54
# File 'lib/jets/cli/exec/command.rb', line 52

def friendly_function_name
  function_name.sub("#{Jets.project.namespace}-", "")
end

#payloadObject

interface method



57
58
59
# File 'lib/jets/cli/exec/command.rb', line 57

def payload
  {command: @options[:command]}.to_json
end

#runObject

override behavior



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
# File 'lib/jets/cli/exec/command.rb', line 4

def run
  result = invoke
  if result["errorMessage"]
    # Note:
    # errorType is Function<Errno::ENOENT> and not useful
    # stackTrace is also not useful. IE: [{}, {}, {}, {}, {}, {}, {}]
    # Actual stackTrace only shows up in the logs
    log.error "ERROR: #{result["errorMessage"]}".color(:red)
    useless_stacktrace = result["stackTrace"]&.all? { |line| line == {} }
    if useless_stacktrace
      # Logs can come from:
      #
      #   1. Lambda invoke host log: shows cold-start and Jets.boot errors
      #   2. Lambda runtime container log: shows errors inside handler
      #
      # The result seems to hide the cold-start/Jets.boot errors.
      # Guessing AWS does this for security reasons and hides it with {}.
      #
      # Since result["stackTrace"] since only shows errors within the handler.
      # Errors that outside the handler at cold-start/Jets.boot time are not in
      # result["stackTrace"]. They show up in the logs though.
      # So we show the logs that are available from the header.
      log_last_4kb(<<~EOL)
        Showing last 4KB of logs from x-amz-log-result header for errors.

        You can check for more logs with.

            jets logs -n #{friendly_function_name}

        Last 4KB of logs:
      EOL
    elsif result["stackTrace"]
      log.error "Stack Trace:"
      result["stackTrace"].each do |line|
        log.error line
      end
    else # fallback to errorMessage
      # No stack trace available.
      # Example: result: {"errorMessage"=>"2024-04-18T19:42:51.819Z cdbcd9f2-6d25-4672-8a83-676643698fa0 Task timed out after 3.05 seconds"}
      log.error "errorMessage: #{result["errorMessage"]}"
    end
  else
    $stdout.print result["stdout"]
    $stderr.print result["stderr"] # same as $stderr.puts
  end
  result
end