Sha256: 589684df11023ad7285bb44c7fcda38c7df17c5df16a27c2dd1cf741e06b851c

Contents?: true

Size: 1.92 KB

Versions: 15

Compression:

Stored size: 1.92 KB

Contents

require 'json'
require 'stringio'

# Node shim calls this class to process both controllers and jobs
class Jets::Processors::MainProcessor
  attr_reader :event, :context, :handler
  def initialize(event, context, handler)
    @event = event
    @context = context
    @handler = handler
  end

  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 = PostsController.process(event, context, "create")
      result = instance_eval(deducer.code, deducer.path)
      result = HashWithIndifferentAccess.new(result) if result.is_a?(Hash)

      Jets.increase_call_count

      if result.is_a?(Hash) && result["headers"]
        # API Gateway is okay with the header values as Integers but
        # ELBs are more strict about this and require the header values to be Strings
        result["headers"]["x-jets-call-count"] = Jets.call_count.to_s
        result["headers"]["x-jets-prewarm-count"] = Jets.prewarm_count.to_s
      end

      result

    # Additional rescue Exception as a paranoid measure.  We want to make sure
    # that we always report the exception.  This is the last line of defense.
    # Note: This only happens when the code is running in Lambda.
    rescue => exception
      Jets.report_exception(exception)
      raise(exception)
    end
  end
end

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
jets-5.0.13 lib/jets/processors/main_processor.rb
jets-5.0.12 lib/jets/processors/main_processor.rb
jets-5.0.11 lib/jets/processors/main_processor.rb
jets-5.0.10 lib/jets/processors/main_processor.rb
jets-5.0.9 lib/jets/processors/main_processor.rb
jets-5.0.8 lib/jets/processors/main_processor.rb
jets-5.0.7 lib/jets/processors/main_processor.rb
jets-5.0.6 lib/jets/processors/main_processor.rb
jets-5.0.5 lib/jets/processors/main_processor.rb
jets-5.0.4 lib/jets/processors/main_processor.rb
jets-5.0.3 lib/jets/processors/main_processor.rb
jets-5.0.2 lib/jets/processors/main_processor.rb
jets-5.0.1 lib/jets/processors/main_processor.rb
jets-5.0.0 lib/jets/processors/main_processor.rb
jets-5.0.0.beta1 lib/jets/processors/main_processor.rb