Class: Jets::PolyFun

Inherits:
Object
  • Object
show all
Extended by:
Memoist
Defined in:
lib/jets/poly_fun.rb,
lib/jets/poly_fun/node_error.rb,
lib/jets/poly_fun/python_error.rb,
lib/jets/poly_fun/base_executor.rb,
lib/jets/poly_fun/node_executor.rb,
lib/jets/poly_fun/lambda_executor.rb,
lib/jets/poly_fun/python_executor.rb

Defined Under Namespace

Classes: BaseExecutor, LambdaExecutor, NodeError, NodeExecutor, PythonError, PythonExecutor

Instance Method Summary collapse

Constructor Details

#initialize(app_class, app_meth) ⇒ PolyFun

Returns a new instance of PolyFun.



5
6
7
8
# File 'lib/jets/poly_fun.rb', line 5

def initialize(app_class, app_meth)
  @app_class = app_class # already a Constant, IE: PostController
  @app_meth = app_meth.to_sym
end

Instance Method Details

#check_private_method!Object



66
67
68
69
70
71
# File 'lib/jets/poly_fun.rb', line 66

def check_private_method!
  private_detected = @app_class.all_private_tasks.keys.include?(@app_meth)
  return unless private_detected # Ok to continue

  raise "The #{@app_class}##{@app_meth} is a private method.  Unable to call it unless it is public"
end

#raise_error(resp) ⇒ Object

Raises:

  • (error_class)


34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/jets/poly_fun.rb', line 34

def raise_error(resp)
  backtrace = resp["stackTrace"] + caller
  backtrace = backtrace.map { |l| l.sub(/^\s+/,'') }
  # Adjust the paths from the tmp path to the app path to improve user debugging
  # experience. Example:
  # From:
  #   File "/tmp/jets/lite/executor/20180917-16777-43a9e48/app/controllers/jets/public_controller/python/show.py", line 32
  # To:
  #   File "app/controllers/jets/public_controller/python/show.py", line 32      backtrace =
  backtrace = backtrace.map do |l|
    if l.include?(Jets.build_root) && !l.include?("lambda_executor.")
      l.sub(/\/tmp\/jets.*executor\/\d{8}-+.*?\//, '')
    else
      l
    end
  end

  # IE: Jets::PolyFun::PythonError
  error_class = "Jets::PolyFun::#{task.lang.to_s.camelize}Error".constantize
  raise error_class.new(resp["errorMessage"], backtrace)
end

#run(event, context = {}) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/jets/poly_fun.rb', line 10

def run(event, context={})
  check_private_method!

  if task.lang == :ruby
    # controller = PostsController.new(event, content)
    # resp = controller.edit
    run_ruby_code(event, context)
  else
    executor = LambdaExecutor.new(task)
    resp = executor.run(event, context)
    if resp["errorMessage"]
      raise_error(resp)
    end
    resp
  end
end

#run_ruby_code(event, context) ⇒ Object



27
28
29
30
31
32
# File 'lib/jets/poly_fun.rb', line 27

def run_ruby_code(event, context)
  @app_class.process(event, context, @app_meth)
rescue Exception => e
  Jets.on_exception(e)
  raise(e)
end

#taskObject



56
57
58
59
60
61
62
63
# File 'lib/jets/poly_fun.rb', line 56

def task
  task = @app_class.all_public_tasks[@app_meth]
  # Provider user a better error message to user than a nil failure.
  unless task
    raise "Unable to find #{@app_class}##{@app_meth}"
  end
  task
end