Permalink
Browse files

adding queuing

  • Loading branch information...
1 parent 6c801d1 commit bcf6326693247f8dbeca7e467fa58b8ab046840d @cyx cyx committed Mar 20, 2010
Showing with 146 additions and 58 deletions.
  1. +1 −0 .gitignore
  2. +0 −54 Rakefile
  3. +2 −1 app/models/item.rb
  4. +7 −0 app/models/purchase.rb
  5. +18 −0 config/initializers/connection_fix.rb
  6. +53 −0 config/resque.god
  7. +1 −0 init.rb
  8. +55 −0 lib/tasks/heroku.rake
  9. +5 −0 lib/tasks/resque.rake
  10. +4 −3 spec/models/item_spec.rb
View
@@ -14,3 +14,4 @@
/config/database.yml
/uploads
/config/deployed_at
+/dump.rdb
View
@@ -3,57 +3,3 @@ task :environment do
end
Dir['lib/tasks/*.rake'].each { |f| load f }
-
-desc "Generate a .gems manifest file for use with heroku"
-task :create_heroku_gems_manifest do
- skip = %w(factory_girl database_cleaner daemons eventmachine faker
- fakeweb fastthread gemcutter json_pure linecache19
- mysql sqlite3-ruby nokogiri rspec-core rspec rspec-expectations
- rspec-mocks rspec-rails-matchers ruby-debug-base19 ruby-debug19
- thin timecop webrat ZenTest treetop cucumber term-ansicolor
- polyglot image_science RubyInline heroku configuration
- launchy)
-
- deps = %w(tzinfo builder memcache-client rack rack-test rack-mount
- erubis mail text-format thor bundler i18n)
-
- puts "\n-----> Starting extraction of config/rvm.gems"
-
- rvm_gems = File.read("config/rvm.gems").split("\n")
-
- puts " Read #{rvm_gems.size - 1} gem entries..."
-
- print "-----> Sorting through the different gems... "
- gems = []
- rvm_gems.each do |gem|
- name, version = gem.split(" -v")
-
- next if gem.index('#') == 0
- next if skip.include?(name)
-
- if deps.include?(name)
- gems.unshift([name, version])
- else
- gems << [ name, version ]
- end
- end
- puts "Done!"
- print "-----> Writing the .gems file now... "
-
- File.open('.gems', 'w') do |f|
- gems.each do |name, version|
- f.write "#{name} --version #{version}\n"
- end
- f.write "pg"
- end
-
- puts "Done!"
- puts " Wrote #{gems.size + 1} gems to the manifest!"
-end
-
-desc "Deploy to heroku"
-task :deploy => [ :create_heroku_gems_manifest, "asset:packager:build_all" ] do
- `git add . && git commit -m "updated .gems manifest on #{Time.now.utc}" && git push heroku-staging master`
-
- `heroku rake asset:upload:s3`
-end
View
@@ -51,7 +51,8 @@ def price_in_dollars
end
def broadcast_to_twitter
- update_attribute(:twitter_status_id, Purchase.post(self, user))
+ Resque.enqueue(Purchase, self.id, user_id)
+ # update_attribute(:twitter_status_id, Purchase.post(self, user))
end
def when=( date_time_or_string )
View
@@ -2,4 +2,11 @@ class Purchase
include StatusUpdateConcerns
template "just bought %.64s :url #boughtstuff"
+
+ @queue = :twitter
+
+ def self.perform( item_id, user_id )
+ item, user = Item.find(item_id), User.find(user_id)
+ item.update_attribute :twitter_status_id, post( item, user )
+ end
end
@@ -0,0 +1,18 @@
+module ActiveRecord::ConnectionAdapters
+ class MysqlAdapter
+ alias_method :execute_without_retry, :execute
+
+ def execute(*args)
+ execute_without_retry(*args)
+ rescue ActiveRecord::StatementInvalid => e
+ if e.message =~ /server has gone away/i
+ warn "Server timed out, retrying"
+ reconnect!
+ retry
+ else
+ raise e
+ end
+ end
+ end
+end
+
View
@@ -0,0 +1,53 @@
+rack_env = ENV['RACK_ENV'] || "production"
+root_dir = ENV['ROOT_DIR'] || "/u/apps/boughtstuff/current"
+num_workers = rack_env == 'production' ? 1 : 2
+
+num_workers.times do |num|
+ God.watch do |w|
+ w.name = "resque-#{num}"
+ w.group = 'resque'
+ w.interval = 30.seconds
+ w.env = {"QUEUE"=>"twitter", "RACK_ENV"=>rack_env}
+ w.start = "/opt/mri-1.9.1-boughtstuff/bin/rake -f #{root_dir}/Rakefile resque:work"
+
+ w.uid = 'ubuntu'
+ w.gid = 'ubuntu'
+
+ # retart if memory gets too high
+ w.transition(:up, :restart) do |on|
+ on.condition(:memory_usage) do |c|
+ c.above = 350.megabytes
+ c.times = 2
+ end
+ end
+
+ # determine the state on startup
+ w.transition(:init, { true => :up, false => :start }) do |on|
+ on.condition(:process_running) do |c|
+ c.running = true
+ end
+ end
+
+ # determine when process has finished starting
+ w.transition([:start, :restart], :up) do |on|
+ on.condition(:process_running) do |c|
+ c.running = true
+ c.interval = 5.seconds
+ end
+
+ # failsafe
+ on.condition(:tries) do |c|
+ c.times = 5
+ c.transition = :start
+ c.interval = 5.seconds
+ end
+ end
+
+ # start if process is not running
+ w.transition(:up, :start) do |on|
+ on.condition(:process_running) do |c|
+ c.running = false
+ end
+ end
+ end
+end
View
@@ -14,6 +14,7 @@
require 'carrierwave/orm/activerecord'
require 'lilypad'
require 'bebop'
+require 'resque'
begin
require 'mysql'
View
@@ -0,0 +1,55 @@
+namespace :heroku do
+ desc "Generate a .gems manifest file for use with heroku"
+ task :create_gems_manifest do
+ skip = %w(factory_girl database_cleaner daemons eventmachine faker
+ fakeweb fastthread gemcutter json_pure linecache19
+ mysql sqlite3-ruby nokogiri rspec-core rspec rspec-expectations
+ rspec-mocks rspec-rails-matchers ruby-debug-base19 ruby-debug19
+ thin timecop webrat ZenTest treetop cucumber term-ansicolor
+ polyglot image_science RubyInline heroku configuration
+ launchy)
+
+ deps = %w(tzinfo builder memcache-client rack rack-test rack-mount
+ erubis mail text-format thor bundler i18n)
+
+ puts "\n-----> Starting extraction of config/rvm.gems"
+
+ rvm_gems = File.read("config/rvm.gems").split("\n")
+
+ puts " Read #{rvm_gems.size - 1} gem entries..."
+
+ print "-----> Sorting through the different gems... "
+ gems = []
+ rvm_gems.each do |gem|
+ name, version = gem.split(" -v")
+
+ next if gem.index('#') == 0
+ next if skip.include?(name)
+
+ if deps.include?(name)
+ gems.unshift([name, version])
+ else
+ gems << [ name, version ]
+ end
+ end
+ puts "Done!"
+ print "-----> Writing the .gems file now... "
+
+ File.open('.gems', 'w') do |f|
+ gems.each do |name, version|
+ f.write "#{name} --version #{version}\n"
+ end
+ f.write "pg"
+ end
+
+ puts "Done!"
+ puts " Wrote #{gems.size + 1} gems to the manifest!"
+ end
+
+ desc "Deploy to heroku"
+ task :deploy => [ :create_heroku_gems_manifest, "asset:packager:build_all" ] do
+ `git add . && git commit -m "updated .gems manifest on #{Time.now.utc}" && git push heroku-staging master`
+
+ `heroku rake asset:upload:s3`
+ end
+end
View
@@ -0,0 +1,5 @@
+require 'resque/tasks'
+
+namespace :resque do
+ task :setup => :environment
+end
View
@@ -1,4 +1,5 @@
require 'spec_helper'
+require 'resque'
describe Item do
it { should validate_presence_of(:name) }
@@ -109,17 +110,17 @@
describe Item, "#broadcast_to_twitter" do
before(:each) do
+ Resque.stub!(:enqueue)
FakeWeb.register_uri(:post, "http://twitter.com/statuses/update.json",
:body => {:id => 123145}.to_json)
@item = Factory(:item, :user => Factory(:user))
end
describe "on success" do
it "saves the twitter status id to the item" do
+ Resque.should_receive(:enqueue).with(Purchase, @item.id, @item.user_id)
@item.twitter_status_id = nil
- lambda {
- @item.broadcast_to_twitter
- }.should change(@item, :twitter_status_id).to("123145")
+ @item.broadcast_to_twitter
end
end

0 comments on commit bcf6326

Please sign in to comment.