Slide 1

Slide 1 text

MEIOSIS IN RAILS APPS @yonbergman

Slide 2

Slide 2 text

Yonatan Bergman @yonbergman

Slide 3

Slide 3 text

Meiosis

Slide 4

Slide 4 text

Meiosis

Slide 5

Slide 5 text

Tech Lead eBay Israel Innovation Center

Slide 6

Slide 6 text

eBay Valet

Slide 7

Slide 7 text

No content

Slide 8

Slide 8 text

$ GET PRICE ESTIMATION SEND ITEM FOR FREE WE SELL IT, YOU GET PAID

Slide 9

Slide 9 text

No content

Slide 10

Slide 10 text

SERVER

Slide 11

Slide 11 text

SERVER

Slide 12

Slide 12 text

HISTORY

Slide 13

Slide 13 text

SERVER

Slide 14

Slide 14 text

! SERVER APP SERVERS DATABASE REDIS SIDEKIQ UTILITY SERVERS

Slide 15

Slide 15 text

SERVER

Slide 16

Slide 16 text

SERVER

Slide 17

Slide 17 text

SERVER EBAY SERVICES

Slide 18

Slide 18 text

SERVER ANALYTICS

Slide 19

Slide 19 text

EBAY SERVICES ANALYTICS SERVER

Slide 20

Slide 20 text

EBAY SERVICES ANALYTICS REPORTING AUDITING QA TOOLS CUSTOMER SUPPORT A/B TESTING SERVER PACKAGE TRACKING

Slide 21

Slide 21 text

PACKAGE TRACKING EBAY SERVICES ANALYTICS REPORTING AUDITING QA TOOLS CUSTOMER SUPPORT A/B TESTING SERVER

Slide 22

Slide 22 text

No content

Slide 23

Slide 23 text

SAD PANDA

Slide 24

Slide 24 text

LAUNCH

Slide 25

Slide 25 text

No content

Slide 26

Slide 26 text

DATABASE LOAD SERVER LOAD DEPLOYMENTS USER RESPONSIBILITIES COMPLEX MODEL TREE STAGING <-> PRODUCTION

Slide 27

Slide 27 text

DATABASE LOAD SERVER LOAD DEPLOYMENTS USER RESPONSIBILITIES COMPLEX MODEL TREE STAGING <-> PRODUCTION

Slide 28

Slide 28 text

DATABASE LOAD SERVER LOAD DEPLOYMENTS USER RESPONSIBILITIES COMPLEX MODEL TREE STAGING <-> PRODUCTION

Slide 29

Slide 29 text

DATABASE LOAD SERVER LOAD DEPLOYMENTS USER RESPONSIBILITIES COMPLEX MODEL TREE STAGING <-> PRODUCTION

Slide 30

Slide 30 text

6 LEVELS OF SEPARATION

Slide 31

Slide 31 text

6 LEVELS OF SEPARATION

Slide 32

Slide 32 text

6 5 4 3 2 MONSTER APP 1

Slide 33

Slide 33 text

6 5 4 3 2 1 MONSTER APP /LIB

Slide 34

Slide 34 text

6 5 4 3 MONSTER APP /LIB NAMESPACING 2 1

Slide 35

Slide 35 text

6 5 4 MONSTER APP /LIB NAMESPACING ENGINES 3 2 1

Slide 36

Slide 36 text

6 5 MONSTER APP /LIB NAMESPACING ENGINES GEMS 4 3 2 1

Slide 37

Slide 37 text

6 MONSTER APP /LIB NAMESPACING ENGINES GEMS SERVICES & SERVERS 5 4 3 2 1

Slide 38

Slide 38 text

6 MONSTER APP /LIB NAMESPACING ENGINES GEMS SOA 5 4 3 2 1

Slide 39

Slide 39 text

SEPARATION

Slide 40

Slide 40 text

SERVER

Slide 41

Slide 41 text

EBAY GEM SERVER PRICE ESTIMATION SERVER

Slide 42

Slide 42 text

EBAY GEM SERVER PRICE ESTIMATION SERVER GEM

Slide 43

Slide 43 text

EBAY GEM SERVER PRICE ESTIMATION SERVER GEM

Slide 44

Slide 44 text

EBAY GEM SERVER PRICE ESTIMATION SERVER EBAY SERVICES GEM

Slide 45

Slide 45 text

require 'ebayr'
 require 'rebay'
 require_relative ‘services/super_secret_api' ! ! module EbayServices class Shopping def initialize @shopping = Rebay::Shopping.new end end end ! !

Slide 46

Slide 46 text

EBAY GEM SERVER PRICE ESTIMATION SERVER EBAY SERVICES GEM

Slide 47

Slide 47 text

API DESIGN

Slide 48

Slide 48 text

AUTH VERSIONING SERIALIZING PARSING ERRORS

Slide 49

Slide 49 text

AUTH VERSIONING SERIALIZING PARSING ERRORS

Slide 50

Slide 50 text

$ curl -H “TOKEN: FOO” https://example.com { ‘status’: ‘ok’ }

Slide 51

Slide 51 text

$ curl -H “TOKEN: FOO” https://example.com { ‘status’: ‘ok’ } ! $ curl https://example.com?token=FOO { ‘status’: ‘ok’ }

Slide 52

Slide 52 text

AUTH VERSIONING SERIALIZING PARSING ERRORS

Slide 53

Slide 53 text

$ curl https://example.com/api/v1/status { ‘status’: ‘ok’ }

Slide 54

Slide 54 text

$ curl https://example.com/api/v1/status { ‘status’: ‘ok’ } ! $ curl -H ‘FOO-VERSION: 1’ 
 https://example.com/api/status { ‘status’: ‘ok’ }

Slide 55

Slide 55 text

$ curl https://example.com/api/v1/status { ‘status’: ‘ok’ } ! $ curl -H ‘FOO-VERSION: 1’ 
 https://example.com/api/status { ‘status’: ‘ok’ } ! $ curl
 -H ‘Accept: application/vnd.foo.v1+json’
 https://example.com/api/status { ‘status’: ‘ok’ }

Slide 56

Slide 56 text

gem ‘versionist’

Slide 57

Slide 57 text

AUTH VERSIONING SERIALIZING PARSING ERRORS

Slide 58

Slide 58 text

gem ‘active_model_serializers’

Slide 59

Slide 59 text

class ItemSerializer < ActiveModel::Serializer attributes :uid, :title, :description, :category has_one :response 
 end

Slide 60

Slide 60 text

class API::ItemSerializer < ActiveModel::Serializer attributes :uid, :title, :description, :category has_one :response, 
 serializer: API::ResponseSerializer 
 def include_category?
 scope.api_version > 1
 end 
 end

Slide 61

Slide 61 text

class API::ItemSerializer < ActiveModel::Serializer attributes :uid, :title, :description, :category has_one :response, 
 serializer: API::ResponseSerializer 
 def include_category?
 scope.api_version > 1
 end 
 end

Slide 62

Slide 62 text

class API::ItemSerializer < ActiveModel::Serializer attributes :uid, :title, :description, :category has_one :response, 
 serializer: API::ResponseSerializer 
 def include_category?
 scope.api_version > 1
 end 
 end

Slide 63

Slide 63 text

class API::ItemSerializer < ActiveModel::Serializer attributes :uid, :title, :description, :category has_one :response, 
 serializer: API::ResponseSerializer 
 def include_category?
 scope.api_version > 1
 end 
 end

Slide 64

Slide 64 text

AUTH VERSIONING SERIALIZING PARSING ERRORS

Slide 65

Slide 65 text

SERIALIZING OBJ JSON PARSING OBJ JSON

Slide 66

Slide 66 text

gem ‘hashie’

Slide 67

Slide 67 text

class API::ItemCreator def create(params)
 Item.create(parsed_params(params))
 end def parsed_params(params)
 ItemParser.new(params).to_hash
 end class ItemParser < Hashie::Trash
 property :title, required: true
 property :description
 end end

Slide 68

Slide 68 text

class API::ItemCreator def create(params)
 Item.create(parsed_params(params))
 end def parsed_params(params)
 ItemParser.new(params).to_hash
 end class ItemParser < Hashie::Trash
 property :title, required: true
 property :description
 end end

Slide 69

Slide 69 text

class API::ItemCreator def create(params)
 Item.create(parsed_params(params))
 end def parsed_params(params)
 ItemParser.new(params).to_hash
 end class ItemParser < Hashie::Trash
 property :title, required: true
 property :description
 end end

Slide 70

Slide 70 text

class ItemParser < Hashie::Trash property :title, required: true
 property :description
 property :category_id, from: ‘category’
 property :price_cents, with: -> (price) {
 (price * 100).to_i
 } end

Slide 71

Slide 71 text

class ItemParser < Hashie::Trash property :title, required: true
 property :description
 property :category_id, from: ‘category’
 property :price_cents, with: -> (price) {
 (price * 100).to_i
 } end

Slide 72

Slide 72 text

class ItemParser < Hashie::Trash property :title, required: true
 property :description
 property :category_id, from: ‘category’
 property :price_cents, with: -> (price) {
 (price * 100).to_i
 } end

Slide 73

Slide 73 text

class ItemParser < Hashie::Trash property :title, required: true
 property :description
 property :category_id, from: ‘category’
 property :price_cents, with: -> (price) {
 (price * 100).to_i
 } end

Slide 74

Slide 74 text

class ItemParser < Hashie::Trash property :user_attributes, 
 from: ‘seller’,
 with: -> (hash) { 
 ItemParser::UserParser.new(hash) 
 } class UserParser < Hashie::Trash
 property :name, required: true
 end end

Slide 75

Slide 75 text

class ItemParser < Hashie::Trash property :user_attributes, 
 from: ‘seller’,
 with: -> (hash) { 
 ItemParser::UserParser.new(hash) 
 } class UserParser < Hashie::Trash
 property :name, required: true
 end end

Slide 76

Slide 76 text

class ItemParser < Hashie::Trash property :user_attributes, 
 from: ‘seller’,
 with: -> (hash) { 
 ItemParser::UserParser.new(hash) 
 } class UserParser < Hashie::Trash
 property :name, required: true
 end end

Slide 77

Slide 77 text

class ItemParser < Hashie::Trash property :user_attributes, 
 from: ‘seller’,
 with: -> (hash) { 
 ItemParser::UserParser.new(hash) 
 } class UserParser < Hashie::Trash
 property :name, required: true
 end end

Slide 78

Slide 78 text

class ItemParser < Hashie::Trash property :user_attributes, 
 from: ‘seller’,
 with: -> (hash) { 
 ItemParser::UserParser.new(hash) 
 } class UserParser < Hashie::Trash
 property :name, required: true
 end end

Slide 79

Slide 79 text

gem ‘representable’
 gem ‘roar’

Slide 80

Slide 80 text

AUTH VERSIONING SERIALIZING PARSING ERRORS

Slide 81

Slide 81 text

ERROR ERROR CODE ERROR MESSAGE HTTP CODE record not found 1003 The object you are looking for could not be found 404 unauthorized 1004 You are not authorized to do the following action 403 invalid param 1005 You are missing a required parameter 400 invalid value 1006 Why are you reading this slide? 400

Slide 82

Slide 82 text

module API::ErrorSupport rescue_from Exception, with: :respond_with_error def respond_with_error(e)
 error = translate_exception(e)
 render json: error, 
 status: error.status_code
 end end

Slide 83

Slide 83 text

module API::ErrorSupport rescue_from Exception, with: :respond_with_error def respond_with_error(e)
 error = translate_exception(e)
 render json: error, 
 status: error.status_code
 end end

Slide 84

Slide 84 text

module API::ErrorSupport rescue_from Exception, with: :respond_with_error def respond_with_error(e)
 error = translate_exception(e)
 render json: error, 
 status: error.status_code
 end end

Slide 85

Slide 85 text

module API::ErrorSupport def translate_exception(e)
 case e
 when ActiveRecord::RecordNotFound
 ApiError.new(1003,
 I18n.t(‘errors.api.not_found'),
 :not_found)
 ...
 end

Slide 86

Slide 86 text

module API::ErrorSupport def translate_exception(e)
 case e
 when ActiveRecord::RecordNotFound
 ApiError.new(1003,
 I18n.t(‘errors.api.not_found'),
 :not_found)
 ...
 end

Slide 87

Slide 87 text

module API::ErrorSupport def translate_exception(e)
 case e
 when ActiveRecord::RecordNotFound
 ApiError.new(1003,
 I18n.t(‘errors.api.not_found'),
 :not_found)
 ...
 end

Slide 88

Slide 88 text

module API::ErrorSupport def translate_exception(e)
 case e
 when ActiveRecord::RecordNotFound
 ApiError.new(1003,
 I18n.t(‘errors.api.not_found'),
 :not_found)
 ...
 end

Slide 89

Slide 89 text

LONG RUNNING TASKS

Slide 90

Slide 90 text

No content

Slide 91

Slide 91 text

EBAY GEM SERVER PRICE ESTIMATION SERVER

Slide 92

Slide 92 text

EBAY GEM SERVER PRICE ESTIMATION SERVER

Slide 93

Slide 93 text

EBAY GEM SERVER PRICE ESTIMATION SERVER

Slide 94

Slide 94 text

EBAY GEM SERVER PRICE ESTIMATION SERVER

Slide 95

Slide 95 text

POST /api/estimations { 'estimation': { ... },
 'callback': ‘https://example.com/callback' } ! RESPONSE { 'uid': 'Y2z2Q3fOTENx4w',
 'status': ‘added’, 'estimation': { ... }

Slide 96

Slide 96 text

GET /api/estimations/:uid { 'uid': 'Y2z2Q3fOTENx4w',
 'status': 'pending' 'estimation': { ... } }

Slide 97

Slide 97 text

POST /callback { 'notification': { 'uid': 'Y2z2Q3fOTENx4w', 'event': ‘estimation_completed', 'estimation': {...} } }

Slide 98

Slide 98 text

RESPONSE 500 // Wait 1s
 RESPONSE 500 // Wait 2s
 RESPONSE 500 // Wait 3s
 RESPONSE 500 // Wait 5s
 RESPONSE 500 // Wait 8s
 RESPONSE 200

Slide 99

Slide 99 text

DATA MIGRATION

Slide 100

Slide 100 text

No content

Slide 101

Slide 101 text

gem ‘trucker’

Slide 102

Slide 102 text

No content

Slide 103

Slide 103 text

SERVER PRICE ESTIMATION SERVER DB DB DB

Slide 104

Slide 104 text

SERVER PRICE ESTIMATION SERVER DB DB DB

Slide 105

Slide 105 text

development: adapter: 'postgresql' encoding: 'unicode' database: 'legacy_database' host: 'localhost' pool: 30

Slide 106

Slide 106 text

class LegacyModel < ActiveRecord::Base ! YAML_FILE = ‘config/legacy_db.yml’ self.abstract_class = true ! establish_connection( YAML::load_file(YAML_FILE)[Rails.env] ) ! end

Slide 107

Slide 107 text

class LegacyItem < LegacyModel ! self.table_name = ‘items’ ! has_one :estimation, class_name: ‘LegacyEstimation’ foreign_key: ‘item_id’ ! end

Slide 108

Slide 108 text

class LegacyMigration ! def run @output = MigrationOutput.new(@file) scope = LegacyItem.from_id(@starting_id) scope.find_each do |legacy_item| migrator = ItemMigrator.new(legacy_item) migrator.run @output.add(migrator.output) end end ! end

Slide 109

Slide 109 text

JSON DB SERVER PRICE ESTIMATION SERVER DB DB

Slide 110

Slide 110 text

JSON DB SERVER PRICE ESTIMATION SERVER DB DB

Slide 111

Slide 111 text

JSON DB SERVER PRICE ESTIMATION SERVER DB DB

Slide 112

Slide 112 text

JSON DB SERVER PRICE ESTIMATION SERVER DB DB

Slide 113

Slide 113 text

4h ~300k

Slide 114

Slide 114 text

EBAY GEM SERVER PRICE ESTIMATION SERVER EBAY SERVICES GEM

Slide 115

Slide 115 text

EBAY GEM SERVER PRICE ESTIMATION SERVER EBAY SERVICES GEM

Slide 116

Slide 116 text

No content

Slide 117

Slide 117 text

Slide 118

Slide 118 text

Slide 119

Slide 119 text

@yonbergman