Class: Jets::Processors::MainProcessor

Inherits:
Object
  • Object
show all
Defined in:
lib/jets/processors/main_processor.rb

Overview

Node shim calls this class to process both controllers and jobs

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(event, context, handler) ⇒ MainProcessor

Returns a new instance of MainProcessor.



7
8
9
10
11
12
# File 'lib/jets/processors/main_processor.rb', line 7

def initialize(event, context, handler)
  # assume valid json from Lambda
  @event = JSON.parse(event)
  @context = JSON.parse(context)
  @handler = handler
end

Instance Attribute Details

#contextObject (readonly)

Returns the value of attribute context.



6
7
8
# File 'lib/jets/processors/main_processor.rb', line 6

def context
  @context
end

#eventObject (readonly)

Returns the value of attribute event.



6
7
8
# File 'lib/jets/processors/main_processor.rb', line 6

def event
  @event
end

#handlerObject (readonly)

Returns the value of attribute handler.



6
7
8
# File 'lib/jets/processors/main_processor.rb', line 6

def handler
  @handler
end

Instance Method Details

#runObject



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
# File 'lib/jets/processors/main_processor.rb', line 14

def run
  # Use the handler to deduce app code to run.
  # Example handlers: handlers/controllers/posts.create, handlers/jobs/sleep.perform
  #
  #   deducer = Jets::Processors::Deducer.new("handlers/controllers/posts.create")
  #
  deducer = Jets::Processors::Deducer.new(handler)
  begin
    # Examples:
    #   deducer.code => PostsController.process(event, context, "show")
    #   deducer.path => app/controllers/posts_controller.rb
    #
    #   deducer.code => HardJob.process(event, context, "dig")
    #   deducer.path => app/jobs/hard_job.rb
    #
    #   deducer.code => HelloFunction.process(event, context, "world")
    #   deducer.path => app/functions/hello.rb
    deducer.load_class
    result = instance_eval(deducer.code, deducer.path)
    # result = PostsController.process(event, context, "create")

    Jets.increase_call_count
    if result.is_a?(Hash) && result["headers"]
      result["headers"]["x-jets-call-count"] = Jets.call_count
      result["headers"]["x-jets-prewarm-count"] = Jets.prewarm_count
    end

    # Puts the return value of project code to stdout because this is
    # what eventually gets used by API Gateway.
    # Explicitly using $stdout since puts has been redirected to $stderr.
    #
    # JSON.dump is pretty robust.  If it cannot dump the structure into a
    # json string, it just dumps it to a plain text string.
    Jets::Util.normalize_result(result) # resp is a String
  rescue Exception => e
    # Customize error message slightly so nodejs shim can process the
    # returned error message.
    # The "RubyError: " is a marker that the javascript shim scans for.
    $stderr.puts("RubyError: #{e.class}: #{e.message}") # js needs this as the first line
    backtrace = e.backtrace.map {|l| "  #{l}" }
    $stderr.puts(backtrace)
    # No need to having error in stderr above anymore because errors are handled in memory
    # at ruby_server.rb but keeping around for posterity.

    raise # raise error to ruby_server.rb to rescue and handle

    # $stderr.puts("END OF RUBY OUTPUT") # uncomment for debugging
  end
end