Class: LambdaRunner::Runner
- Inherits:
-
Object
- Object
- LambdaRunner::Runner
- Defined in:
- lib/lambda_runner.rb
Overview
abstract for running the program
Instance Method Summary collapse
- #add_aws_sdk ⇒ Object
-
#initialize(module_path, name, port = 8897) ⇒ Runner
constructor
A new instance of Runner.
- #install_deps ⇒ Object
- #process_event(event, context = {}) ⇒ Object
- #start(opts = { cover: true }) ⇒ Object
- #stop ⇒ Object
- #url ⇒ Object
Constructor Details
#initialize(module_path, name, port = 8897) ⇒ Runner
Returns a new instance of Runner.
11 12 13 14 15 |
# File 'lib/lambda_runner.rb', line 11 def initialize(module_path, name, port = 8897) @module_path = module_path @name = name @port = port end |
Instance Method Details
#add_aws_sdk ⇒ Object
25 26 27 28 |
# File 'lib/lambda_runner.rb', line 25 def add_aws_sdk STDOUT.puts("Copying aws-sdk into the lambda function's node_modules.") FileUtils.cp_r File.('../../js/node_modules/aws-sdk', __FILE__), "#{File.dirname(@module_path)}/node_modules" end |
#install_deps ⇒ Object
17 18 19 20 21 22 23 |
# File 'lib/lambda_runner.rb', line 17 def install_deps @npm_cwd = File.('../../js/', __FILE__) STDOUT.puts("trying to use npm install in #{@npm_cwd}") npm_install_pid = spawn('npm', 'install', chdir: @npm_cwd) Process.wait(npm_install_pid) fail 'failed to install the lambda startup' if ($CHILD_STATUS.exitstatus != 0) end |
#process_event(event, context = {}) ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/lambda_runner.rb', line 51 def process_event(event, context = {}) payload = JSON.generate({ event: event, context: context }) id = RestClient.post(url, payload, content_type: :json).to_str loop do response = RestClient.get(url, params: { id: id }) {|response, request, res| response} data = JSON.parse('['+response.body+']').first case response.code when 200 then sleep(0.1) when 201 then return data when 500 then fail data when 502 then fail data when 504 then fail 'timeout' else fail "unknown response #{response.code}" end end end |
#start(opts = { cover: true }) ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/lambda_runner.rb', line 30 def start(opts = { cover: true }) if opts[:timeout] == nil opts[:timeout] = '30000' end install_deps #copy over aws sdk only if it is not already there if !File.directory?("#{File.dirname(@module_path)}/node_modules/aws-sdk") add_aws_sdk end # start node in a way that emulates how it's run in production cmd = ['node'] cmd = [File.join(@npm_cwd, 'node_modules/.bin/istanbul'), 'cover', '--root', File.dirname(@module_path), '--'] if opts[:cover] cmd += [File.join(@npm_cwd, 'startup.js'), '-p', @port.to_s, '-m', @module_path, '-h', @name, '-t', opts[:timeout]] @proc = ProcessHelper::ProcessHelper.new(print_lines: true) @proc.start(cmd, 'Server running at http') end |
#stop ⇒ Object
69 70 71 |
# File 'lib/lambda_runner.rb', line 69 def stop RestClient.delete(url) end |
#url ⇒ Object
47 48 49 |
# File 'lib/lambda_runner.rb', line 47 def url "http://localhost:#{@port}/" end |