Complex  Made  Simple Sleep Better With TorqueBox                 Bob  McWhirter                 Director  of  Polyglot   ...
Complex  Made  Simple Sleep Better With TorqueBox                 Bob  McWhirter                 Director  of  Polyglot   ...
Who  am  I?Bob  McWhirter๏   Director  of  Polyglot,  JBoss  Fellow  @  Red  Hat๏   TorqueBox  project  founder๏   Part  o...
ScenarioYouve  got  a  Rails  application  to  deploy,  for  production.
But...The  app  you  deploy  today  will  grow  in  the  future,  if  you’re  successful.
Deploy  Continuum            TorqueBox   Roll!                  Platform your!own               as!a!Service
What  is  TorqueBox?TorqueBox  is  a  Ruby  Application  Server.Based  on  JRuby  and  JBoss  AS7.
What  is  an  app-­server?If  you’re  not  from  the  Java  world...An  application  server  is  a  process  that  hosts  ...
App-­servertastic!     Cache                   Jobs              Application        Web                 HA               Q...
Anyhow...
Roll  your  own๏ Install  OS๏ Install  packages๏ Configure  everything๏ Manage  everything
Roll  your  own๏   Apache  httpd๏   Load  balancer๏   Unicorn,  pack  of  Mongrels๏   crontab๏   SMTP๏   memcached๏   Data...
Or  outsource      some...
Roll  your  own  >>๏ Install  OS๏ Install  packages๏ Configure  most  things๏ Manage  most  things๏ Outsource  some  things
Roll  your  own  >>๏   Apache  httpd๏   Load  balancer๏   Unicorn,  pack  of  Mongrels๏   crontab๏   SMTP  Amazon  SES,  S...
Use  a  Paas  (Heroku)Outsource  everything  but  your  app.
Heroku๏   Apache  httpd  Heroku๏   Load  balancer  Heroku๏   Unicorn,  pack  of  Mongrels  Heroku๏   crontab  Heroku๏   SM...
Or  middle-­ground๏ Decrease  number  of  things  to  manage๏ Increase  scalability๏ Increase  availability  (optionally)
TorqueBox๏   Apache  httpd๏   Load  balancer  JBoss  mod_cluster๏   Unicorn,  pack  of  Mongrels  TorqueBox๏   crontab  To...
Case  Study!
Appalachian  Sustainable  Agriculture  Project  (ASAP)
Application  Needs๏ Rails  2.x  web  app๏ Caching๏ Background  tasks๏ Cron  jobs๏ Deployment  from  SCM๏ Sending  email๏ D...
As  deployed  on  Heroku
Migration  MotivationIn  this  particular  case,  client  feels  it  could  save  money,  and  have  a  better  system  by...
Migrating  to  TorqueBox$  rvm  install  jruby-­1.6.7
Migrating  to  TorqueBox$  gem  install  torquebox-­server      -­-­pre  -­-­version=2.0.0.cr1     Simpler  once  we’re  n...
Using  torquebox.rb  template$  cd  myapp$  torquebox  rails
torquebox.rb  template๏   Add  torquebox  gems  to  Gemfile๏   Add  activerecord-­jdbc-­adapter๏   Add  torquebox-­rake-­s...
“Porting”  the   application
Delayed::JobApp  uses  Delayed::Job        Replace  with  TorqueBox        Backgroundable
Delayed  Job  controller.rbdef  create    @export  =  ExcelExport.create(...)    job  =  ExcelExportJob.new      job.excel...
Delayed  Job   excel_export_job.rbclass  ExcelExportJob  <  Struct.new(:excel_export_id)    def  perform        ExcelExpor...
Delayed  Job    excel_export.rbclass  ExcelExport  <  ActiveRecord::Base    def  generate_report        begin            s...
Delayed  JobMayhap  you  need  to  run  a  worker,  also...
Delayed::Job  Exporter < AR::Base   Database  ExportJob < Struct     Worker
Thats  pretty  explicitTorqueBox  allows  you  to  very  easily  run  methods  asynchronously.
Backgroundable   excel_export.rbclass  ExcelExport  <  ActiveRecord::Base    always_background  :generate_report    def  g...
Backgroundable   excel_export.rbclass  ExcelExport  <  ActiveRecord::Base    always_background  :generate_report    def  g...
Backgroundable  export_controller.rbdef  create    @export  =  ExcelExport.create(...)    @export.set_status  "Queued  for...
Backgroundable excel_export_job.rb$  rm  excel_export_job.rb                              it !                       K ill
Trade!Trade  some  code  and  a  process  you  have  to  manage  for  less  code  and  an  in-­container  service.
Backgroundable Implicit!magic!            Queue           Processor      Exporter < AR::Base   Database      ExportJob < S...
Caching๏   template.rb  does  most  of  the  work    ๏ Web  sessions    ๏ Fragment  and  other  caching
No  more  memcached!
Scheduled  Jobs๏ Replaces  cron  jobs๏ Deploys  with  your  application๏ Includes  application  environment๏ Cron-­like  s...
Heroku  Cron๏ Free  version  runs  1x  daily๏ Implemented  as  a  rake  task๏ Includes  application  environment
Roll  your  own๏ crontab๏ script/runner  (boots  rails  every  time)๏ deployment  strategy๏ source  control๏ etc.
On  Heroku   Rakefiledesc  "This  task  is  called  by  the  heroku  cron  add-­on"task  :cron  =>  [:environment,        ...
Scheduled  JobsIn  TorqueBox,  a  scheduled  job  is  a  component  of  your  application.
Straight-­forward  refresh_trip_planner.rbclass  RefreshTripPlanner    def  run()        #        #  update  the  GIS  cac...
Configuration  torquebox.ymljobs:    cache.updater:        job:                  RefreshTripPlanner        cron:          ...
But...Doing  a  huge  query  once-­a-­day  isnt  ideal,  it  was  just  "free".
TorqueBox  Services๏Avoid  the  huge  query๏Keep  data  more  fresher
Keep  it  fresher    trip_planner_service.rb  TripPlannerService    def  initialize(  options={}  )        @queue  =  Torq...
Keep  it  fresher    trip_planner_service.rb  TripPlannerService    def  initialize(  options={}  )  def  initialize(  opt...
Keep  it  fresher    trip_planner_service.rb  TripPlannerService    def  initialize(  options={}  )                def  st...
Keep  it  fresher      app/models/business.rbrequire  torquebox-­messagingclass  Business  <  ActiveRecord::Base    after_...
Keep  it  fresher      app/models/business.rbrequire  torquebox-­messagingclass  Business  <  ActiveRecord::Base    after_...
Queue,  Service,  Cache                       Queue Non-blocking        ActiveRecord       Business                Service...
Production!
(Remember,  we  work  for  Red  Hat)
Set  up  the  server$  yum  install          java-­1.6.0-­openjdk          httpd          mod_cluster          git        ...
CapistranoRegular  capistrano  deployments  are  supported  given  the  recipes  provided  by  torquebox-­capistrano-­supp...
Capistrano   deploy.rbrequire torquebox-capistrano-supportrequire bundler/capistrano# source codeset :application,       "...
Capistrano   deploy.rb  (continued)# Production serverset :deploy_to,           "/opt/apps/buyappalachian.org"set :torqueb...
Launch  on  boot
init.dInstall  /etc/init.d/jboss-­asfrom  the  stock  distribution
JBoss  configuration    /etc/jboss-­as/jboss-­as.conf#   General configuration for the init.d script#   Place this file in...
Load  Balance  with    mod_cluster
mod_cluster   httpd.confLoadModule  slotmem_module              modules/mod_slotmem.soLoadModule  proxy_cluster_module  mo...
mod_cluster   httpd.conf  (continued)<VirtualHost  torquebox-­balancer:6666>      <Directory  />        Order  deny,allow ...
Deployed[root@torquebox  opt]#  ls  -­l  apps/buyappalachian.org/total  8lrwxrwxrwx  1      23  Mar  16  15:10  current  -...
And  then  it  grows...As  your  app  evolves,  you  might  add  stuff  like  WebSockets/STOMP,  or  HA  or  other  facili...
Can  we  go   further?
Whats  a  PaaS?     Cache                     Jobs              Application         DB                 Email              ...
TorqueBox  on  OpenShift     openshift.redhat.com
Roadmap  &  Status๏ Current  2.0.0.CR1๏ 2.0.0.Final  Real  Flippin’  Soon๏ Continuing  investment  from  Red  Hat๏ Interop...
Resourceshttp://torquebox.org/@torquebox  on  Twitter#torquebox  on  IRC
Question  everything.
Creative  Commons  BY-­SA  3.0
Complex Made Simple: Sleep Better with TorqueBox
Upcoming SlideShare
Loading in...5
×

Complex Made Simple: Sleep Better with TorqueBox

2,836

Published on

The TorqueBox presentation by Bob McWhirter as presented at RubyNation 2012 in Reston, VA.

Published in: Technology
0 Comments
3 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
2,836
On Slideshare
0
From Embeds
0
Number of Embeds
4
Actions
Shares
0
Downloads
23
Comments
0
Likes
3
Embeds 0
No embeds

No notes for slide

Complex Made Simple: Sleep Better with TorqueBox

  1. 1. Complex  Made  Simple Sleep Better With TorqueBox Bob  McWhirter Director  of  Polyglot Red  Hat Creative  Commons  BY-­SA  3.0
  2. 2. Complex  Made  Simple Sleep Better With TorqueBox Bob  McWhirter Director  of  Polyglot Red  Hat Creative  Commons  BY-­SA  3.0
  3. 3. Who  am  I?Bob  McWhirter๏ Director  of  Polyglot,  JBoss  Fellow  @  Red  Hat๏ TorqueBox  project  founder๏ Part  of  the  project:odd  team๏ The  Codehaus,  Drools,  Jaxen,  Groovy
  4. 4. ScenarioYouve  got  a  Rails  application  to  deploy,  for  production.
  5. 5. But...The  app  you  deploy  today  will  grow  in  the  future,  if  you’re  successful.
  6. 6. Deploy  Continuum TorqueBox Roll! Platform your!own as!a!Service
  7. 7. What  is  TorqueBox?TorqueBox  is  a  Ruby  Application  Server.Based  on  JRuby  and  JBoss  AS7.
  8. 8. What  is  an  app-­server?If  you’re  not  from  the  Java  world...An  application  server  is  a  process  that  hosts  your  app  and  provides  a  multitude  of  services  and  facilities.
  9. 9. App-­servertastic! Cache Jobs Application Web HA Queues
  10. 10. Anyhow...
  11. 11. Roll  your  own๏ Install  OS๏ Install  packages๏ Configure  everything๏ Manage  everything
  12. 12. Roll  your  own๏ Apache  httpd๏ Load  balancer๏ Unicorn,  pack  of  Mongrels๏ crontab๏ SMTP๏ memcached๏ Database๏ deployment๏ monitoring
  13. 13. Or  outsource   some...
  14. 14. Roll  your  own  >>๏ Install  OS๏ Install  packages๏ Configure  most  things๏ Manage  most  things๏ Outsource  some  things
  15. 15. Roll  your  own  >>๏ Apache  httpd๏ Load  balancer๏ Unicorn,  pack  of  Mongrels๏ crontab๏ SMTP  Amazon  SES,  SendGrid๏ memcached๏ Database๏ deployment  Capistrano๏ monitoring  New  Relic
  16. 16. Use  a  Paas  (Heroku)Outsource  everything  but  your  app.
  17. 17. Heroku๏ Apache  httpd  Heroku๏ Load  balancer  Heroku๏ Unicorn,  pack  of  Mongrels  Heroku๏ crontab  Heroku๏ SMTP  SendGrid๏ memcached  Heroku๏ Database  Heroku๏ deployment  Heroku๏ monitoring  New  Relic
  18. 18. Or  middle-­ground๏ Decrease  number  of  things  to  manage๏ Increase  scalability๏ Increase  availability  (optionally)
  19. 19. TorqueBox๏ Apache  httpd๏ Load  balancer  JBoss  mod_cluster๏ Unicorn,  pack  of  Mongrels  TorqueBox๏ crontab  TorqueBox๏ SMTP  Amazon  SES,  SendGrid๏ memcached  TorqueBox๏ Database๏ deployment  Capistrano๏ monitoring  NewRelic
  20. 20. Case  Study!
  21. 21. Appalachian  Sustainable  Agriculture  Project  (ASAP)
  22. 22. Application  Needs๏ Rails  2.x  web  app๏ Caching๏ Background  tasks๏ Cron  jobs๏ Deployment  from  SCM๏ Sending  email๏ Database  queries๏ Monitoring  &  metrics
  23. 23. As  deployed  on  Heroku
  24. 24. Migration  MotivationIn  this  particular  case,  client  feels  it  could  save  money,  and  have  a  better  system  by  moving  to  an  affordable  VPS,  letting  TorqueBox  absorb  some  of  the  roll-­your-­own  complexity.
  25. 25. Migrating  to  TorqueBox$  rvm  install  jruby-­1.6.7
  26. 26. Migrating  to  TorqueBox$  gem  install  torquebox-­server      -­-­pre  -­-­version=2.0.0.cr1 Simpler  once  we’re  no  longer  -­-­pre
  27. 27. Using  torquebox.rb  template$  cd  myapp$  torquebox  rails
  28. 28. torquebox.rb  template๏ Add  torquebox  gems  to  Gemfile๏ Add  activerecord-­jdbc-­adapter๏ Add  torquebox-­rake-­support๏ Create  directories  for  services,  jobs,  etc๏ Initialize  web  session  store๏ Initialize  Rails.cache  with  TorqueBox๏ Initialize  ActiveRecord  for  background  jobs
  29. 29. “Porting”  the   application
  30. 30. Delayed::JobApp  uses  Delayed::Job   Replace  with  TorqueBox   Backgroundable
  31. 31. Delayed  Job controller.rbdef  create    @export  =  ExcelExport.create(...)    job  =  ExcelExportJob.new      job.excel_export_id  =  @export.id    Delayed::Job.enqueue  job    @export.set_status  "Queued  for  Export"    @export.save!end
  32. 32. Delayed  Job excel_export_job.rbclass  ExcelExportJob  <  Struct.new(:excel_export_id)    def  perform        ExcelExport.find(self.excel_export_id).generate_report    endend
  33. 33. Delayed  Job excel_export.rbclass  ExcelExport  <  ActiveRecord::Base    def  generate_report        begin            set_status  Processing            spreadsheet  =  Spreadsheet.new(self.businesses)                    set_status  Writing            spreadsheet.write_workbook  self.workbook_file            set_status  Storing  on  Amazon  S3            File.open(self.workbook_file)  {|out|self.output  =  out}                    set_status  Cleaning  Up            File.delete(self.workbook_file)                    set_status  Complete        rescue            set_status  Export  Error        end    end
  34. 34. Delayed  JobMayhap  you  need  to  run  a  worker,  also...
  35. 35. Delayed::Job Exporter < AR::Base Database ExportJob < Struct Worker
  36. 36. Thats  pretty  explicitTorqueBox  allows  you  to  very  easily  run  methods  asynchronously.
  37. 37. Backgroundable excel_export.rbclass  ExcelExport  <  ActiveRecord::Base    always_background  :generate_report    def  generate_report        begin            set_status  Processing            ...        rescue            set_status  Export  Error        end    endend    
  38. 38. Backgroundable excel_export.rbclass  ExcelExport  <  ActiveRecord::Base    always_background  :generate_report    def  generate_report        begin            set_status  Processing            ...        rescue            set_status  Export  Error        end    endend    
  39. 39. Backgroundable export_controller.rbdef  create    @export  =  ExcelExport.create(...)    @export.set_status  "Queued  for  Export"    @export.save    @export.generate_reportend
  40. 40. Backgroundable excel_export_job.rb$  rm  excel_export_job.rb  it ! K ill
  41. 41. Trade!Trade  some  code  and  a  process  you  have  to  manage  for  less  code  and  an  in-­container  service.
  42. 42. Backgroundable Implicit!magic! Queue Processor Exporter < AR::Base Database ExportJob < Struct Worker
  43. 43. Caching๏ template.rb  does  most  of  the  work ๏ Web  sessions ๏ Fragment  and  other  caching
  44. 44. No  more  memcached!
  45. 45. Scheduled  Jobs๏ Replaces  cron  jobs๏ Deploys  with  your  application๏ Includes  application  environment๏ Cron-­like  scheduling๏ HA-­clustering  (optionally)
  46. 46. Heroku  Cron๏ Free  version  runs  1x  daily๏ Implemented  as  a  rake  task๏ Includes  application  environment
  47. 47. Roll  your  own๏ crontab๏ script/runner  (boots  rails  every  time)๏ deployment  strategy๏ source  control๏ etc.
  48. 48. On  Heroku Rakefiledesc  "This  task  is  called  by  the  heroku  cron  add-­on"task  :cron  =>  [:environment,                                :heroku_backup,                                :refresh_trip_planner]  desc  "Refresh  the  trip  planner  business  data  cache"task  :refresh_trip_planner  =>  :environment  do    #    #  update  GIS  cache    #end
  49. 49. Scheduled  JobsIn  TorqueBox,  a  scheduled  job  is  a  component  of  your  application.
  50. 50. Straight-­forward refresh_trip_planner.rbclass  RefreshTripPlanner    def  run()        #        #  update  the  GIS  cache        #    endend
  51. 51. Configuration torquebox.ymljobs:    cache.updater:        job:                  RefreshTripPlanner        cron:                0  0  2  *  *  ?        description:  Refresh  trip  cache
  52. 52. But...Doing  a  huge  query  once-­a-­day  isnt  ideal,  it  was  just  "free".
  53. 53. TorqueBox  Services๏Avoid  the  huge  query๏Keep  data  more  fresher
  54. 54. Keep  it  fresher trip_planner_service.rb  TripPlannerService    def  initialize(  options={}  )        @queue  =  TorqueBox::Messaging::Queue.new(  /queues/trip_planner  )    end        def  start        Thread.new  do            initialize_cache            while  should_run                update_cache  @queue.receive            end        end    end    def  update_cache(  business_id  )        TripPlanner.insert_or_update  business_id    end    #  initialize_cache(),  stop(),  etcend
  55. 55. Keep  it  fresher trip_planner_service.rb  TripPlannerService    def  initialize(  options={}  ) def  initialize(  options={}  )        @queue  =  TorqueBox::Messaging::Queue.new(  /queues/trip_planner  )    @queue  =  TorqueBox::Messaging::Queue.new(  ...  )    end end        def  start        Thread.new  do            initialize_cache            while  should_run                update_cache  @queue.receive            end        end    end    def  update_cache(  business_id  )        TripPlanner.insert_or_update  business_id    end    #  initialize_cache(),  stop(),  etcend
  56. 56. Keep  it  fresher trip_planner_service.rb  TripPlannerService    def  initialize(  options={}  ) def  start        @queue  =  TorqueBox::Messaging::Queue.new(  /queues/trip_planner  )    end        Thread.new  do    def  start        initialize_cache        Thread.new  do        while  should_run            initialize_cache            while  should_run            update_cache  @queue.receive                update_cache  @queue.receive            end        end        end    end    end end    def  update_cache(  business_id  )        TripPlanner.insert_or_update  business_id    end    #  initialize_cache(),  stop(),  etcend
  57. 57. Keep  it  fresher app/models/business.rbrequire  torquebox-­messagingclass  Business  <  ActiveRecord::Base    after_save  :update_trip_planner    def  update_trip_planner        queue  =  TorqueBox::Messaging::Queue.new(/queues/trip_planner)        queue.publish(  self.id  )    endend
  58. 58. Keep  it  fresher app/models/business.rbrequire  torquebox-­messagingclass  Business  <  ActiveRecord::Base    after_save  :update_trip_planner    def  update_trip_planner queue  =  TorqueBox::Messaging::Queue.new(...)        queue  =  TorqueBox::Messaging::Queue.new(/queues/trip_planner)        queue.publish(  self.id  )    end queue.publish(  self.id  )end
  59. 59. Queue,  Service,  Cache Queue Non-blocking ActiveRecord Business Service Cache
  60. 60. Production!
  61. 61. (Remember,  we  work  for  Red  Hat)
  62. 62. Set  up  the  server$  yum  install          java-­1.6.0-­openjdk          httpd          mod_cluster          git          postgresql-­server
  63. 63. CapistranoRegular  capistrano  deployments  are  supported  given  the  recipes  provided  by  torquebox-­capistrano-­support.gem
  64. 64. Capistrano deploy.rbrequire torquebox-capistrano-supportrequire bundler/capistrano# source codeset :application, "buyappalachian.org"set :repository, "git@github.com:account/repo.git"set :branch, "torquebox-2.0"set :user, "torquebox"set :scm, :gitset :scm_verbose, trueset :use_sudo, false
  65. 65. Capistrano deploy.rb  (continued)# Production serverset :deploy_to, "/opt/apps/buyappalachian.org"set :torquebox_home, "/opt/torquebox/current"set :jboss_init_script, "/etc/init.d/jboss-as-standalone"set :app_environment, "RAILS_ENV: production"set :app_context, "/"ssh_options[:forward_agent] = falserole :web, "torquebox.buyappalachian.org"role :app, "torquebox.buyappalachian.org"role :db, "torquebox.buyappalachian.org", :primary => true
  66. 66. Launch  on  boot
  67. 67. init.dInstall  /etc/init.d/jboss-­asfrom  the  stock  distribution
  68. 68. JBoss  configuration /etc/jboss-­as/jboss-­as.conf# General configuration for the init.d script# Place this file in /etc/jboss-as/jboss-as.conf# and copy $JBOSS_HOME/bin/init.d/jboss-as-standalone.sh# to /etc/init.d/jboss-as-standaloneJBOSS_USER=torqueboxJBOSS_HOME=/opt/torquebox/current/jbossJBOSS_PIDFILE=/var/run/torquebox/torquebox.pidJBOSS_CONSOLE_LOG=/var/log/torquebox/console.logJBOSS_CONFIG=standalone-ha.xml
  69. 69. Load  Balance  with   mod_cluster
  70. 70. mod_cluster httpd.confLoadModule  slotmem_module              modules/mod_slotmem.soLoadModule  proxy_cluster_module  modules/mod_proxy_cluster.soLoadModule  advertise_module          modules/mod_advertise.soLoadModule  manager_module              modules/mod_manager.so<Location  /mod_cluster_manager>        SetHandler  mod_cluster-­manager        AllowDisplay  On</Location>Listen  torquebox-­balancer:6666
  71. 71. mod_cluster httpd.conf  (continued)<VirtualHost  torquebox-­balancer:6666>      <Directory  />        Order  deny,allow        Deny  from  all        Allow  from  all    </Directory>      KeepAliveTimeout  60    MaxKeepAliveRequests  0    EnableMCPMReceive      ManagerBalancerName  torquebox-­balancer    AllowDisplay  On    AdvertiseFrequency  5    AdvertiseSecurityKey  secret  </VirtualHost>
  72. 72. Deployed[root@torquebox  opt]#  ls  -­l  apps/buyappalachian.org/total  8lrwxrwxrwx  1      23  Mar  16  15:10  current  -­>  releases/20120315230553drwxrwxr-­x  5  4096  Mar  15  19:05  releasesdrwxrwxr-­x  6  4096  Mar  15  17:29  shared
  73. 73. And  then  it  grows...As  your  app  evolves,  you  might  add  stuff  like  WebSockets/STOMP,  or  HA  or  other  facilities  already  in  the  tin.
  74. 74. Can  we  go   further?
  75. 75. Whats  a  PaaS? Cache Jobs Application DB Email Queues
  76. 76. TorqueBox  on  OpenShift openshift.redhat.com
  77. 77. Roadmap  &  Status๏ Current  2.0.0.CR1๏ 2.0.0.Final  Real  Flippin’  Soon๏ Continuing  investment  from  Red  Hat๏ Interop  with  other  languages,  such   as  Clojure  via  Immutant
  78. 78. Resourceshttp://torquebox.org/@torquebox  on  Twitter#torquebox  on  IRC
  79. 79. Question  everything.
  80. 80. Creative  Commons  BY-­SA  3.0
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×