Skip to content

Commit

Permalink
adding queuing
Browse files Browse the repository at this point in the history
  • Loading branch information
Cyril David committed Mar 20, 2010
1 parent 6c801d1 commit bcf6326
Show file tree
Hide file tree
Showing 10 changed files with 146 additions and 58 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -14,3 +14,4 @@
/config/database.yml
/uploads
/config/deployed_at
/dump.rdb
54 changes: 0 additions & 54 deletions Rakefile
Expand Up @@ -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
3 changes: 2 additions & 1 deletion app/models/item.rb
Expand Up @@ -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 )
Expand Down
7 changes: 7 additions & 0 deletions app/models/purchase.rb
Expand Up @@ -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
18 changes: 18 additions & 0 deletions config/initializers/connection_fix.rb
@@ -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

53 changes: 53 additions & 0 deletions config/resque.god
@@ -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
1 change: 1 addition & 0 deletions init.rb
Expand Up @@ -14,6 +14,7 @@
require 'carrierwave/orm/activerecord'
require 'lilypad'
require 'bebop'
require 'resque'

begin
require 'mysql'
Expand Down
55 changes: 55 additions & 0 deletions lib/tasks/heroku.rake
@@ -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
5 changes: 5 additions & 0 deletions lib/tasks/resque.rake
@@ -0,0 +1,5 @@
require 'resque/tasks'

namespace :resque do
task :setup => :environment
end
7 changes: 4 additions & 3 deletions spec/models/item_spec.rb
@@ -1,4 +1,5 @@
require 'spec_helper'
require 'resque'

describe Item do
it { should validate_presence_of(:name) }
Expand Down Expand Up @@ -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

Expand Down

0 comments on commit bcf6326

Please sign in to comment.