DECOUPEZ VOTRE APPLI EN     MICRO-APIS          SOA ON RAILS :-)           by Nicolas Blanco      http://twitter.com/slain...
CODE SPAGHETTI ? USINE A GAZ ?
THIN CONTROLER / FAT MODEL         MODULES   CLASSES ACTIVE MODELVOTRE PROPRE ENGINE / GEM !
DECOUPLAGE EN SOA ?DECOUPER UNE FONCTIONNALITE EN MICRO-API              INDEPENDANCE               API REST JSON         ...
BUTREPONSE INSTANTANEE EN JSONID UNIQUE POUR CHAQUE JOB
EXEMPLE=> POST api.myapp.com/imports{ id: “42”, status: “scheduled”, ... }=> GET api.myapp.com/imports/42{ id: “42”, statu...
LES GEMS     SIDEKIQ      REDIS      GRAPEREDIS PERSISTENCE    FOREMAN
FOREMAN          web: bundle exec thin start -p $PORTworker: bundle exec sidekiq -r ./workers/import_xml.rb -c 2
MODEL IMPORTmodule Models class Import  include Redis::Persistence    property :state, default: "scheduled"    property :c...
class API < Grape::API version v1, using: :header, vendor: "pluriporter"   format :json resources :imports do  desc "Creat...
WORKERmodule Workers class Import  include Sidekiq::Worker  def perform(import_id, xml)   require File.expand_path("../../...
ALWAYS REFACTOR !
Upcoming SlideShare
Loading in …5
×

Découplez votre appli en micro-APIs

8,764 views
8,725 views

Published on

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
8,764
On SlideShare
0
From Embeds
0
Number of Embeds
7,939
Actions
Shares
0
Downloads
2
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Découplez votre appli en micro-APIs

  1. 1. DECOUPEZ VOTRE APPLI EN MICRO-APIS SOA ON RAILS :-) by Nicolas Blanco http://twitter.com/slainer68
  2. 2. CODE SPAGHETTI ? USINE A GAZ ?
  3. 3. THIN CONTROLER / FAT MODEL MODULES CLASSES ACTIVE MODELVOTRE PROPRE ENGINE / GEM !
  4. 4. DECOUPLAGE EN SOA ?DECOUPER UNE FONCTIONNALITE EN MICRO-API INDEPENDANCE API REST JSON STOCKAGE RAPIDE (REDIS ?) INCONVENIENTS
  5. 5. BUTREPONSE INSTANTANEE EN JSONID UNIQUE POUR CHAQUE JOB
  6. 6. EXEMPLE=> POST api.myapp.com/imports{ id: “42”, status: “scheduled”, ... }=> GET api.myapp.com/imports/42{ id: “42”, status: “finished”, result: { ... }, finished_at: ... }
  7. 7. LES GEMS SIDEKIQ REDIS GRAPEREDIS PERSISTENCE FOREMAN
  8. 8. FOREMAN web: bundle exec thin start -p $PORTworker: bundle exec sidekiq -r ./workers/import_xml.rb -c 2
  9. 9. MODEL IMPORTmodule Models class Import include Redis::Persistence property :state, default: "scheduled" property :created_at property :finished_at property :fail_message property :result, default: {} def start! update_attributes(state: "started") end def finish! update_attributes(state: "finished", finished_at: Time.now) end def fail_with!(message) update_attributes(state: "failed", fail_message: message) end end end
  10. 10. class API < Grape::API version v1, using: :header, vendor: "pluriporter" format :json resources :imports do desc "Creates new import from XML data" params do requires :xml, type: String, desc: "XML data" end post do Models::Import.create(created_at: Time.now).tap do |import| Workers::Import.perform_async(import.id, params[:xml]) end endend
  11. 11. WORKERmodule Workers class Import include Sidekiq::Worker def perform(import_id, xml) require File.expand_path("../../models/import", __FILE__) import = Models::Import.find(import_id) import.start! begin # ... end rescue => e import.fail_with!(e.to_s) raise e end import.finish! end
  12. 12. ALWAYS REFACTOR !

×