class LambdaRunner::Runner
abstract for running the program
Public Class Methods
new(module_path, name, port = 8897)
click to toggle source
# File lib/lambda_runner.rb, line 11 def initialize(module_path, name, port = 8897) @module_path = module_path @name = name @port = port end
Public Instance Methods
add_aws_sdk()
click to toggle source
# 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.expand_path('../../js/node_modules/aws-sdk', __FILE__), "#{File.dirname(@module_path)}/node_modules" end
install_deps()
click to toggle source
# File lib/lambda_runner.rb, line 17 def install_deps @npm_cwd = File.expand_path('../../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 = {})
click to toggle source
# File lib/lambda_runner.rb, line 60 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 504 then fail 'timeout' else fail "unknown response #{response.code}" end end end
start(opts = { cover: true })
click to toggle source
# File lib/lambda_runner.rb, line 30 def start(opts = { cover: true }) if opts[:timeout] == nil opts[:timeout] = '30000' end @cover = opts[:cover] 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/nyc'), '--cwd ', File.dirname(@module_path), '--reporter=lcov', '--report-dir ', File.join(Dir.pwd, 'coverage'), '--temp-dir ', File.join(Dir.pwd, 'coverage', 'temp'), 'node' ] 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) puts cmd.join(' ') @proc.start(cmd, 'Server running at http') end
stop()
click to toggle source
# File lib/lambda_runner.rb, line 77 def stop puts "Stopping Lambda Server" RestClient.delete(url) @proc.kill # TODO currently coverage is not working on other projects cmd = [ File.join(@npm_cwd, 'node_modules/.bin/nyc'), 'report', '--report-dir ', File.join(Dir.pwd, 'coverage'), '--temp-dir ', File.join(Dir.pwd, 'coverage', 'temp'), ] system(cmd.join(' ')) if @cover end
url()
click to toggle source
# File lib/lambda_runner.rb, line 56 def url "http://localhost:#{@port}/" end