Complex Made Simple                       Sleep Better With TorqueBox                                       Lance Ball    ...
Complex Made Simple                       Sleep Better With TorqueBox                                       Lance Ball    ...
Who am I?                  Lance Ball                  ๏    Senior Software Engineer @ Red Hat                  ๏    Torqu...
Scenario                  Youve got a Ruby                  application to                  deploy, for                  p...
Ruby App                         Sinatra      Rails                               Rack                         Passenger/T...
But...                  The app you deploy                  today will grow in the                  future, if you’re     ...
Eventual ComplexityFriday, April 27, 12
Ruby App                                    Sinatra      Rails                                          Rack              ...
Ruby App                                    Sinatra      Rails                                          Rack              ...
Ruby App                                    Sinatra      Rails                                          Rack              ...
Deploy ContinuumFriday, April 27, 12
Deploy ContinuumFriday, April 27, 12
Deploy ContinuumFriday, April 27, 12
What is TorqueBox?                  TorqueBox is a                  Ruby Application Server.                  Based on JRu...
TorqueBox AS            Sinatra           Rails                       Rack            Tasks   Procs            Jobs       ...
Roll your own                  ๏ Install OS                  ๏ Install packages                  ๏ Configure everything   ...
Roll your own                  ๏                       Apache httpd                  ๏                       Load balancer...
Or outsource                          some...Friday, April 27, 12
Roll your own >>                  ๏                       Apache httpd                  ๏                       Load balan...
Use a Paas                  Outsource                  everything but                  your app.Friday, April 27, 12
Heroku                  ๏                       Apache httpd Heroku                  ๏                       Load balancer...
TorqueBox                  ๏                       Apache httpd                  ๏                       Load balancer JBo...
Case Study!Friday, April 27, 12
Appalachian Sustainable                   Agriculture Project (ASAP)Friday, April 27, 12
Application Needs                  ๏ Rails 2.x web app                  ๏ Caching                  ๏ Background tasks     ...
As deployed on HerokuFriday, April 27, 12
Migration Motivation                  In this particular case, client feels                  it could save money, and have...
Install           $ rvm install jruby-1.6.7           $ rvm use jruby-1.6.7           $ gem install torquebox-serverFriday...
Templatize           $ torquebox rails myappFriday, April 27, 12
torquebox.rb template                  ๏    torquebox-* and activerecord-jdbc-                       adapter gems         ...
“Porting” the                        applicationFriday, April 27, 12
Background                          JobsFriday, April 27, 12
Delayed::Job                  App uses                  Delayed::Job                        Replace with                  ...
Delayed Job                   controller.rb         def create           @export = ExcelExport.create(...)           job =...
Delayed Job                   controller.rb         def create           @export = ExcelExport.create(...)           job =...
Delayed Job                   excel_export_job.rb         class ExcelExportJob < Struct.new(:excel_export_id)           de...
Delayed Job                   excel_export.rb         class ExcelExport < ActiveRecord::Base           def generate_report...
Delayed Job                  Mayhap you need to                  run a worker, also...Friday, April 27, 12
Delayed::Job                       Exporter < AR::Base   Database                       ExportJob < Struct     WorkerFrida...
Thats pretty explicit                  TorqueBox allows you                  to very easily run                  methods  ...
Backgroundable                   excel_export.rb         class ExcelExport < ActiveRecord::Base           always_backgroun...
Backgroundable                   excel_export.rb         class ExcelExport < ActiveRecord::Base           always_backgroun...
Backgroundable                   export_controller.rb         def create           @export = ExcelExport.create(...)      ...
Backgroundable                   export_controller.rb         def create           @export = ExcelExport.create(...)      ...
Backgroundable                   excel_export_job.rb         $ git rm excel_export_job.rb                                 ...
Backgroundable                       Implicit!magic!                                  Queue           Processor           ...
CachingFriday, April 27, 12
Caching                  ๏    template.rb does the work                       ๏ Web sessions                       ๏ Fragm...
No more                       memcached!Friday, April 27, 12
Cache         cache = TorqueBox::Infinispan::Cache.new         cache.put(“something”, anything)         cache.get(“somethi...
cron                       0 0 2 * * ?/WTF??Friday, April 27, 12
Heroku Cron                  ๏ Free version runs 1x daily                  ๏ Implemented as a rake task                  ๏...
Heroku Cron                   Rakefile         desc "This task is called by the heroku cron add-on"         task :cron => ...
Scheduled Jobs                  In TorqueBox, a scheduled job                  is a component of your                  app...
Straight-forward                   refresh_trip_planner.rb         class RefreshTripPlanner           def run()           ...
Configuration                   torquebox.yml         jobs:           cache.updater:             job:         RefreshTripPl...
But...                  Doing a huge query                  once-a-day isnt                  ideal, it was just           ...
ServicesFriday, April 27, 12
TorqueBox Services                  ๏Avoid the huge query                  ๏Keep data more fresherFriday, April 27, 12
Keep it fresher                   trip_planner_service.rb          class TripPlannerService           def initialize( opti...
Keep it fresher                   trip_planner_service.rb          TripPlannerService           def initialize( options={}...
Keep it fresher                   trip_planner_service.rb          TripPlannerService           def initialize( options={}...
Keep it fresher                   app/models/business.rb         require torquebox-messaging         class Business < Acti...
Keep it fresher                   app/models/business.rb         require torquebox-messaging         class Business < Acti...
Queue, Service, Cache                                             Queue                       Non-blocking                ...
Production!Friday, April 27, 12
(Remember, I work for Red Hat)Friday, April 27, 12
Set up the server           $ yum install                java-1.6.0-openjdk                httpd                mod_cluste...
Launch on                         bootFriday, April 27, 12
init.d                  Install                  /etc/init.d/jboss-as                  from the stock distributionFriday, ...
JBoss configuration                   /etc/jboss-as/jboss-as.conf         #      General configuration for the init.d scrip...
Load Balance                   with mod_clusterFriday, April 27, 12
mod_cluster                   httpd.conf         LoadModule     slotmem_module         modules/mod_slotmem.so         Load...
mod_cluster                   httpd.conf (continued)         <VirtualHost torquebox-balancer:6666>                     <Di...
Deploy with                       CapistranoFriday, April 27, 12
Capistrano                  Regular capistrano deployments                  are supported given the recipes               ...
Deployed           [root@torquebox opt]#   ls -l apps/buyappalachian.org/           total 8           lrwxrwxrwx 1   23 Ma...
And then it grows...                  As your app evolves, you                  might add stuff like                  WebS...
Can we go                        further?Friday, April 27, 12
Whats a PaaS?                       Cache                     Jobs                                Application             ...
OPENSHIFT                       http://openshift.redhat.com     https://github.com/torquebox/torquebox-openshift-expressFr...
Roadmap & Status                  ๏ Current 2.0.1                  ๏ Continued Red Hat investment                  ๏ Inter...
Resources           http://torquebox.org/           @torquebox on Twitter           #torquebox on IRCFriday, April 27, 12
Question                       everything.Friday, April 27, 12
Creative Commons BY-SA 3.0Friday, April 27, 12
Upcoming SlideShare
Loading in...5
×

Complex Made Simple: Sleep Better With TorqueBox

5,401

Published on

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

No Downloads
Views
Total Views
5,401
On Slideshare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
51
Comments
0
Likes
10
Embeds 0
No embeds

No notes for slide

Complex Made Simple: Sleep Better With TorqueBox

  1. 1. Complex Made Simple Sleep Better With TorqueBox Lance Ball Senior Software Engineer Red Hat Creative Commons BY-SA 3.0Friday, April 27, 12
  2. 2. Complex Made Simple Sleep Better With TorqueBox Lance Ball Senior Software Engineer Red Hat Creative Commons BY-SA 3.0Friday, April 27, 12
  3. 3. Who am I? Lance Ball ๏ Senior Software Engineer @ Red Hat ๏ TorqueBox core developer ๏ Part of the project:odd team ๏ Open source contributorFriday, April 27, 12
  4. 4. Scenario Youve got a Ruby application to deploy, for production.Friday, April 27, 12
  5. 5. Ruby App Sinatra Rails Rack Passenger/Thin Apache/NginxFriday, April 27, 12
  6. 6. But... The app you deploy today will grow in the future, if you’re successful.Friday, April 27, 12
  7. 7. Eventual ComplexityFriday, April 27, 12
  8. 8. Ruby App Sinatra Rails Rack Tasks Passenger/Thin Resque/ DelayedJob Apache/NginxFriday, April 27, 12
  9. 9. Ruby App Sinatra Rails Rack Tasks Passenger/Thin Jobs Resque/ DelayedJob Apache/Nginx crondFriday, April 27, 12
  10. 10. Ruby App Sinatra Rails Rack Tasks Passenger/Thin Jobs Resque/ DelayedJob Apache/Nginx crond Daemons god/monitFriday, April 27, 12
  11. 11. Deploy ContinuumFriday, April 27, 12
  12. 12. Deploy ContinuumFriday, April 27, 12
  13. 13. Deploy ContinuumFriday, April 27, 12
  14. 14. What is TorqueBox? TorqueBox is a Ruby Application Server. Based on JRuby and JBoss AS7.Friday, April 27, 12
  15. 15. TorqueBox AS Sinatra Rails Rack Tasks Procs Jobs Daemons Web Messaging Scheduling Services JBoss AS Clustering Load Balancing HAFriday, April 27, 12
  16. 16. Roll your own ๏ Install OS ๏ Install packages ๏ Configure everything ๏ Manage everythingFriday, April 27, 12
  17. 17. Roll your own ๏ Apache httpd ๏ Load balancer ๏ Unicorn, pack of Mongrels ๏ crontab ๏ SMTP ๏ memcached ๏ Database ๏ deployment ๏ monitoringFriday, April 27, 12
  18. 18. Or outsource some...Friday, April 27, 12
  19. 19. Roll your own >> ๏ Apache httpd ๏ Load balancer ๏ Unicorn, pack of Mongrels ๏ crontab ๏ SMTP Amazon SES, SendGrid ๏ memcached ๏ Database ๏ deployment Capistrano ๏ monitoring New RelicFriday, April 27, 12
  20. 20. Use a Paas Outsource everything but your app.Friday, April 27, 12
  21. 21. Heroku ๏ Apache httpd Heroku ๏ Load balancer Heroku ๏ Unicorn, pack of Mongrels Heroku ๏ crontab Heroku ๏ SMTP SendGrid ๏ memcached Heroku ๏ Database Heroku ๏ deployment Heroku ๏ monitoring New RelicFriday, April 27, 12
  22. 22. 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 NewRelicFriday, April 27, 12
  23. 23. Case Study!Friday, April 27, 12
  24. 24. Appalachian Sustainable Agriculture Project (ASAP)Friday, April 27, 12
  25. 25. Application Needs ๏ Rails 2.x web app ๏ Caching ๏ Background tasks ๏ Cron jobs ๏ Deployment from SCM ๏ Sending email ๏ Database queries ๏ Monitoring & metricsFriday, April 27, 12
  26. 26. As deployed on HerokuFriday, April 27, 12
  27. 27. Migration Motivation In 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.Friday, April 27, 12
  28. 28. Install $ rvm install jruby-1.6.7 $ rvm use jruby-1.6.7 $ gem install torquebox-serverFriday, April 27, 12
  29. 29. Templatize $ torquebox rails myappFriday, April 27, 12
  30. 30. torquebox.rb template ๏ torquebox-* and activerecord-jdbc- adapter gems ๏ torquebox-rake-support ๏ Directories for services, jobs, etc ๏ Web session store ๏ Rails.cache with TorqueBox ๏ Background jobsFriday, April 27, 12
  31. 31. “Porting” the applicationFriday, April 27, 12
  32. 32. Background JobsFriday, April 27, 12
  33. 33. Delayed::Job App uses Delayed::Job Replace with TorqueBox BackgroundableFriday, April 27, 12
  34. 34. Delayed Job controller.rb def 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! endFriday, April 27, 12
  35. 35. Delayed Job controller.rb def create   @export = ExcelExport.create(...)   job = ExcelExportJob.new   job.excel_export_id = @export.id job = ExcelExportJob.new   Delayed::Job.enqueue job job.excel_export_id = @export.id   @export.set_status "Queued for Export" Delayed::Job.enqueue job   @export.save! endFriday, April 27, 12
  36. 36. Delayed Job excel_export_job.rb class ExcelExportJob < Struct.new(:excel_export_id)   def perform     ExcelExport.find(self.excel_export_id).generate_report   end endFriday, April 27, 12
  37. 37. Delayed Job excel_export.rb class ExcelExport < ActiveRecord::Base   def generate_report     begin       set_status Processing       spreadsheet = Spreadsheet.new(self.businesses)        spreadsheet.write_workbook self.workbook_file       set_status Storing on S3       File.open(self.workbook_file) {|out|self.output = out}           File.delete(self.workbook_file)           set_status Complete     rescue       set_status Export Error     end   end endFriday, April 27, 12
  38. 38. Delayed Job Mayhap you need to run a worker, also...Friday, April 27, 12
  39. 39. Delayed::Job Exporter < AR::Base Database ExportJob < Struct WorkerFriday, April 27, 12
  40. 40. Thats pretty explicit TorqueBox allows you to very easily run methods asynchronously.Friday, April 27, 12
  41. 41. Backgroundable excel_export.rb class ExcelExport < ActiveRecord::Base   always_background :generate_report   def generate_report     begin       set_status Processing ...     rescue       set_status Export Error     end   end end  Friday, April 27, 12
  42. 42. Backgroundable excel_export.rb class ExcelExport < ActiveRecord::Base   always_background :generate_report   always_background :generate_report   def generate_report     begin       set_status Processing ...     rescue       set_status Export Error     end   end end  Friday, April 27, 12
  43. 43. Backgroundable export_controller.rb def create   @export = ExcelExport.create(...)   @export.set_status "Queued for Export"   @export.save @export.generate_report endFriday, April 27, 12
  44. 44. Backgroundable export_controller.rb def create   @export = ExcelExport.create(...)   @export.set_status "Queued for Export"   @export.save @export.generate_report @export.generate_report endFriday, April 27, 12
  45. 45. Backgroundable excel_export_job.rb $ git rm excel_export_job.rb i t ! i l l KFriday, April 27, 12
  46. 46. Backgroundable Implicit!magic! Queue Processor Exporter < AR::Base Database ExportJob < Struct WorkerFriday, April 27, 12
  47. 47. CachingFriday, April 27, 12
  48. 48. Caching ๏ template.rb does the work ๏ Web sessions ๏ Fragment and other cachingFriday, April 27, 12
  49. 49. No more memcached!Friday, April 27, 12
  50. 50. Cache cache = TorqueBox::Infinispan::Cache.new cache.put(“something”, anything) cache.get(“something”)Friday, April 27, 12
  51. 51. cron 0 0 2 * * ?/WTF??Friday, April 27, 12
  52. 52. Heroku Cron ๏ Free version runs 1x daily ๏ Implemented as a rake task ๏ Includes app environmentFriday, April 27, 12
  53. 53. Heroku Cron Rakefile desc "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 # endFriday, April 27, 12
  54. 54. Scheduled Jobs In TorqueBox, a scheduled job is a component of your application.Friday, April 27, 12
  55. 55. Straight-forward refresh_trip_planner.rb class RefreshTripPlanner   def run() #     # update the GIS cache #   end endFriday, April 27, 12
  56. 56. Configuration torquebox.yml jobs:   cache.updater:     job: RefreshTripPlanner     cron: 0 0 2 * * ?     description: Refresh trip cacheFriday, April 27, 12
  57. 57. But... Doing a huge query once-a-day isnt ideal, it was just "free".Friday, April 27, 12
  58. 58. ServicesFriday, April 27, 12
  59. 59. TorqueBox Services ๏Avoid the huge query ๏Keep data more fresherFriday, April 27, 12
  60. 60. Keep it fresher trip_planner_service.rb class 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(), etc endFriday, April 27, 12
  61. 61. 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(), etc endFriday, April 27, 12
  62. 62. 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     Thread.new do     initialize_cache     while should_run       initialize_cache       while should_run       update_cache(@queue.receive)         update_cache @queue.receive       end     end     end   end   end   def end update_cache( business_id )     TripPlanner.insert_or_update business_id   end # initialize_cache(), stop(), etc endFriday, April 27, 12
  63. 63. Keep it fresher app/models/business.rb require torquebox-messaging class Business < ActiveRecord::Base   after_save :update_trip_planner   def update_trip_planner     queue = TorqueBox::Messaging::Queue.new(/queues/trip_planner)     queue.publish( self.id )   end endFriday, April 27, 12
  64. 64. Keep it fresher app/models/business.rb require torquebox-messaging class 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 ) endFriday, April 27, 12
  65. 65. Queue, Service, Cache Queue Non-blocking ActiveRecord Business Service CacheFriday, April 27, 12
  66. 66. Production!Friday, April 27, 12
  67. 67. (Remember, I work for Red Hat)Friday, April 27, 12
  68. 68. Set up the server $ yum install java-1.6.0-openjdk httpd mod_cluster git postgresql-serverFriday, April 27, 12
  69. 69. Launch on bootFriday, April 27, 12
  70. 70. init.d Install /etc/init.d/jboss-as from the stock distributionFriday, April 27, 12
  71. 71. 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-standalone JBOSS_USER=torquebox JBOSS_HOME=/opt/torquebox/current/jboss JBOSS_PIDFILE=/var/run/torquebox/torquebox.pid JBOSS_CONSOLE_LOG=/var/log/torquebox/console.log JBOSS_CONFIG=standalone-ha.xmlFriday, April 27, 12
  72. 72. Load Balance with mod_clusterFriday, April 27, 12
  73. 73. mod_cluster httpd.conf LoadModule slotmem_module modules/mod_slotmem.so LoadModule proxy_cluster_module modules/mod_proxy_cluster.so LoadModule advertise_module modules/mod_advertise.so LoadModule manager_module modules/mod_manager.so <Location /mod_cluster_manager>     SetHandler mod_cluster-manager     AllowDisplay On </Location> Listen torquebox-balancer:6666Friday, April 27, 12
  74. 74. 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>Friday, April 27, 12
  75. 75. Deploy with CapistranoFriday, April 27, 12
  76. 76. Capistrano Regular capistrano deployments are supported given the recipes provided by torquebox-capistrano- support.gemFriday, April 27, 12
  77. 77. Deployed [root@torquebox opt]# ls -l apps/buyappalachian.org/ total 8 lrwxrwxrwx 1 23 Mar 16 15:10 current -> releases/20120315230553 drwxrwxr-x 5 4096 Mar 15 19:05 releases drwxrwxr-x 6 4096 Mar 15 17:29 sharedFriday, April 27, 12
  78. 78. And then it grows... As your app evolves, you might add stuff like WebSockets/STOMP, or HA or other facilities already in the tin.Friday, April 27, 12
  79. 79. Can we go further?Friday, April 27, 12
  80. 80. Whats a PaaS? Cache Jobs Application DB Email QueuesFriday, April 27, 12
  81. 81. OPENSHIFT http://openshift.redhat.com https://github.com/torquebox/torquebox-openshift-expressFriday, April 27, 12
  82. 82. Roadmap & Status ๏ Current 2.0.1 ๏ Continued Red Hat investment ๏ Interop with other languages, such as Clojure via ImmutantFriday, April 27, 12
  83. 83. Resources http://torquebox.org/ @torquebox on Twitter #torquebox on IRCFriday, April 27, 12
  84. 84. Question everything.Friday, April 27, 12
  85. 85. Creative Commons BY-SA 3.0Friday, April 27, 12
  1. A particular slide catching your eye?

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

×