Mais conteúdo relacionado Semelhante a Asynchronous Processing with Ruby on Rails (RailsConf 2008) (20) Mais de Jonathan Dahl (9) Asynchronous Processing with Ruby on Rails (RailsConf 2008)2. I.
What is it, and
why should I care? 4. Wife: What are you talking about at
RailsConf this year?
Jon: Asynchronous Processing
Wife: [changes subject] 16. Image Upload
Browser
( 3 seconds)
Response
Send to S3 ( who cares?) 18. Image Upload
Generate 3 Thumbnails
Send Thumbnail A to S3
Send Thumbnail B to S3
Send Thumbnail C to S3
Send Original to S3
Browser Response 19. Image Upload
Generate 3 Thumbnails
Send Thumbnail A to S3
Send Thumbnail B to S3
Send Thumbnail C to S3
Send Original to S3
Browser Response 20. Image Upload
Generate 3 Thumbnails
Send Thumbnail A to S3
Send Thumbnail B to S3
Send Thumbnail C to S3
Send Original to S3
Browser Response 21. Image Upload
Generate 3 Thumbnails
Send Thumbnail A to S3
Send Thumbnail B to S3
Send Thumbnail C to S3
Send Original to S3
Browser Response 22. Image Upload
Generate 3 Thumbnails
Send Thumbnail A to S3
Send Thumbnail B to S3
Send Thumbnail C to S3
Send Original to S3
Browser Response 23. Image Upload
Generate 3 Thumbnails
Send Thumbnail A to S3
Send Thumbnail B to S3
Send Thumbnail C to S3
Send Original to S3
Browser Response 24. Image Upload
Generate 3 Thumbnails
Send Thumbnail A to S3
Send Thumbnail B to S3
Send Thumbnail C to S3
Send Original to S3
Browser Response 34. HTTP trigger - API request
GET /photos/1.xml HTTP/1.1
Host: example.com:80 37. Human trigger - console
$ script/console production
Loading production environment
(Rails 2.0.2)
>> Photo.destroy_all 38. No trigger?
- Send email in 2 hours
- Sync data at 3:00am PST
- Notify admin when disk is 90% full
- Expire sessions that are inactive
- Archive records that exceed quota 40. Concrete examples
• Sending mail
• Transcoding video/audio
• Storing images on S3
• Receiving email
• Synching with outside database
• Complex computations 45. Zencoder
User
Video Sharing
Website
Data Storage
(Amazon S3)
Zencoder
Manager
Worker Worker
Worker Worker 47. class Photo < ActiveRecord::Base
after_create :background_s3_upload
def background_s3_upload
Bj.submit quot;./script/runner ./jobs/send_to_s3.rb #{self.id}quot;
end
end 51. III.
So how do you
decide what to use? 74. Task Storage Task Trigger
Database daemon
Message Queue cron 75. Task Storage Task Trigger
Database daemon
Message Queue cron 76. create_table quot;jobsquot; do |t|
t.text quot;commandquot;
t.integer quot;priorityquot;
t.integer quot;pidquot;
t.datetime quot;submitted_atquot;
t.datetime quot;started_atquot;
t.datetime quot;finished_atquot;
t.text quot;resultquot;
end 77. create_table quot;photosquot; do |t|
t.string quot;filenamequot;
t.datetime quot;created_atquot;
t.datetime quot;processed_atquot;
end 78. create_table quot;photosquot; do |t|
t.string quot;filenamequot;
t.datetime quot;created_atquot;
t.datetime quot;processed_atquot;
end 79. Task Storage Task Trigger
Database daemon
Message Queue cron 80. Task Storage Task Trigger
Database daemon
Message Queue cron 86. require 'memcache'
starling = MemCache.new('192.168.1.1:22122')
# Put messages onto a queue:
starling.set('my_queue', 12345)
# Get message from the queue:
starling.get('my_queue') 87. Task Storage Task Trigger
Database daemon
Message Queue cron 89. Task Storage Task Trigger
Database daemon
Message Queue cron 92. Task Storage Task Trigger
Database daemon
Message Queue cron 93. 0 6 * * * script/runner jobs/send_emails.rb 97. BackgroundDRb
class BillingWorker < BackgrounDRb::MetaWorker
set_worker_name :billing_worker
def create(args = nil)
# this method is called when worker is loaded for the first time
end
def charge_customer(customer_id = nil)
logger.info 'charging customer now'
end
end
MiddleMan.worker(:billing_worker).charge_customer(current_customer.id) 98. :backgroundrb:
:ip: 0.0.0.0
:development:
:backgroundrb:
:port: 11111
:log: foreground
:production:
:backgroundrb:
:port: 22222
:lazy_load: true
:debug_log: false
./script/backgroundrb start 99. AP4R
def MyController
def queue
ap4r.async_to({:action => 'download'}, {:story =>
story.id, :url => params[:url]})
end
def download
# long-running task
end
end 101. create_table quot;bj_jobquot;, :primary_key => quot;bj
t.text quot;commandquot;
t.text quot;statequot;
t.integer quot;priorityquot;
t.text quot;tagquot;
t.integer quot;is_restartablequot;
t.text quot;submitterquot;
t.text quot;runnerquot;
t.integer quot;pidquot;
t.datetime quot;submitted_atquot;
t.datetime quot;started_atquot;
t.datetime quot;finished_atquot;
t.text quot;envquot; 106. # task class
class ImageWorker < Workling::Base
def send_to_s3(options = {})
# put file to S3
end
end 112. VI.
some
recommendations