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
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# 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 }) ⇒ Object
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 |
# 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 ⇒ Object
77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# 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 ⇒ Object
56 57 58 |
# File 'lib/lambda_runner.rb', line 56 def url "http://localhost:#{@port}/" end |