Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Building Serverless Ruby Bots @ Ruby Conf 2018

DamirSvrtan
November 15, 2018

Building Serverless Ruby Bots @ Ruby Conf 2018

Want to build a Bot without the hassle of provisioning and managing servers? Amazon’s got a service for that and it’s called AWS Lambda - it executes your code only when needed, scales automatically and you pay only for the compute time you consume.

There’s one problem with Lambda - it doesn’t support Ruby! 
Let’s checkout multiple options on how to build a Ruby Bot and package it into an executable which you can run on any machine without the need to actually have Ruby installed.

DamirSvrtan

November 15, 2018
Tweet

More Decks by DamirSvrtan

Other Decks in Technology

Transcript

  1. # main.rb require 'net/http' require 'time' class NewApartments attr_reader :url

    def initialize(url) @url = url @last_scrapped_at = Time.now - 5 * 60 # 5 minutes ago. end def any? response_body .scan(/datetime\="(.*)" /) .any? { |published_at| Time.parse(published_at) > last_scrapped_at } end private def response_body Net::HTTP.get(URI(url)) end end
  2. DOWNLOAD THE RUNTIMES OS X FOR OUR DEV MACHINE LINUX

    X86_64 COMPATIBLE BINARY FOR AWS LAMBDA
  3. traveling-ruby-bot - ruby-linux-x86_64 - bin/ - ruby - ... -

    ... - ruby-os-x - bin/ - ruby - ... - ...
  4. // lambda-function-wrapper.js var spawn = require('child_process').spawn; exports.handler = function(event, context)

    { var child = spawn('ruby-linux-x86_64/bin/ruby main.rb'); child.stdout.on('data', function (data) { console.log("stdout:\n"+data); }); child.stderr.on('data', function (data) { console.log("stderr:\n"+data); }); child.on('close', function (code) { context.done(); }); }
  5. TWO WAYS TO BUILD APPS WITH MRUBY: - DOWNLOAD MRUBY

    SOURCE DIRECTLY FROM GITHUB - MRUBY-CLI - PLATFORM FOR BUILDING NATIVE COMMAND LINE APPLICATIONS FOR LINUX, WINDOWS, AND OS X.
  6. jruby-bot - lib/ - jruby.jar - src/ - main/ -

    resources/ - main.rb - gradlew
  7. AWS LAMBDA PRICING THE NUMBER OF REQUESTS (I.E. NUMBER OF

    TIMES OUR FUNCTION IS INVOKED) THE SUM OF DURATIONS OUR FUNCTIONS TOOK TO EXECUTE (EXPRESSED IN GB-SECONDS)
  8. # serverless.yml service: ruby_bot provider: name: openwhisk runtime: ruby functions:

    ruby_bot: handler: handler.main plugins: - serverless-openwhisk
  9. Jets.application.routes.draw do get 'posts', to: 'posts#index' end class PostsController <

    ApplicationController def index render json: { hello: 'world', action: 'index' } end end
  10. DAMIR SVRTAN SENIOR SOFTWARE ENGINEER @ Netflix HIT ME UP

    @DamirSvrtan TINYURL.COM/NETFLIX-RUBY