Permalink
Browse files

Add use of resque queue

  • Loading branch information...
1 parent f14bf17 commit 43452d83990e96fde019c7e06dfd5e2866477800 @rwdaigle committed May 23, 2011
Showing with 72 additions and 31 deletions.
  1. +1 −0 .gitignore
  2. +3 −2 DEMO
  3. +1 −0 Gemfile
  4. +11 −0 Gemfile.lock
  5. +1 −0 Procfile
  6. +10 −2 Rakefile
  7. +30 −0 app.rb
  8. +5 −26 config.ru
  9. +9 −0 jobs/index_tweets.rb
  10. +1 −1 views/index.erb
View
@@ -1 +1,2 @@
.env
+log
View
5 DEMO
@@ -7,16 +7,17 @@ bundle install
heroku create ryan-tweetstream --stack cedar
heroku config:set TWITTER_USERNAME=rwdaigle TWITTER_PASSWORD=mypass TWITTER_KEYWORD=ruby
-heroku addons:add pusher:test
+heroku addons:add pusher:test redistogo:small
heroku addons:upgrade logging:expanded releases:advanced
git push heroku master
# Run/scale
-heroku scale web=2 stream=1 clock=1
+heroku scale web=1 stream=1 clock=1
# Introspect
watch heroku ps
heroku releases
heroku logs --tail -s app
+/resque
View
@@ -6,6 +6,7 @@ gem "sinatra"
gem "pusher"
gem "tweetstream"
gem "clockwork"
+gem "resque"
gem "hoptoad_notifier"
gem "newrelic_rpm"
View
@@ -19,6 +19,14 @@ GEM
signature (~> 0.1.2)
rack (1.2.2)
rake (0.8.7)
+ redis (2.2.0)
+ redis-namespace (1.0.3)
+ redis (< 3.0.0)
+ resque (1.16.1)
+ json (>= 1.4.6, < 1.6)
+ redis-namespace (~> 1.0.2)
+ sinatra (>= 0.9.2)
+ vegas (~> 0.1.2)
ruby-hmac (0.4.0)
signature (0.1.2)
ruby-hmac
@@ -37,6 +45,8 @@ GEM
twitter-stream (0.1.13)
eventmachine (~> 0.12.8)
simple_oauth (~> 0.1.4)
+ vegas (0.1.8)
+ rack (>= 1.0.0)
PLATFORMS
ruby
@@ -47,6 +57,7 @@ DEPENDENCIES
newrelic_rpm
pusher
rake
+ resque
sinatra
thin
tweetstream
View
@@ -1,3 +1,4 @@
web: bundle exec thin start -p $PORT -e $RACK_ENV
stream: bundle exec rake tweetstream:stream --trace
+worker: QUEUE=normal VERBOSE=1 bundle exec rake resque:work
clock: bundle exec clockwork clock.rb
View
@@ -3,13 +3,21 @@ require "bundler/setup"
require "pusher"
require "tweetstream"
+require "resque"
+require 'resque/tasks'
+
+uri = URI.parse(ENV["REDISTOGO_URL"])
+Resque.redis = Redis.new(:host => uri.host, :port => uri.port, :password => uri.password)
+Pusher.app_id = "ryan-test-heroku-tweetstream"
STDOUT.sync = true
+
task "tweetstream:stream" do
- Pusher.app_id =
TweetStream::Client.new(ENV["TWITTER_USERNAME"], ENV["TWITTER_PASSWORD"]).track(ENV["TWITTER_KEYWORD"]) do |status|
- puts "%s: %s" % [ status[:user][:screen_name], status[:text] ]
+ Resque.enqueue(IndexTweet, status)
Pusher['tweets'].trigger('tweet', status)
end
end
+
+task "resque:setup"
View
30 app.rb
@@ -0,0 +1,30 @@
+require "sinatra/base"
+require "uri"
+require "newrelic_rpm"
+require "hoptoad_notifier"
+
+module TweetStream
+ class App < Sinatra::Base
+
+ get "/" do
+ erb :index
+ end
+
+ get "/bad" do
+ raise "this is an error"
+ end
+
+ helpers do
+ def pusher_api_key
+ URI.parse(ENV["PUSHER_URL"]).user
+ end
+ end
+
+ # send errors to hoptoad
+ enable :raise_errors
+ HoptoadNotifier.configure do |config|
+ config.api_key = ENV['HOPTOAD_API_KEY']
+ end
+ use HoptoadNotifier::Rack
+ end
+end
View
@@ -1,31 +1,10 @@
require "rubygems"
require "bundler/setup"
+require "resque/server"
-require "sinatra"
-require "uri"
-require "newrelic_rpm"
-require "hoptoad_notifier"
+require File.dirname(__FILE__) + "/app"
-get "/" do
- erb :index
-end
-
-get "/bad" do
- raise "this is an error"
-end
-
-helpers do
- def pusher_api_key
- URI.parse(ENV["PUSHER_URL"]).user
- end
-end
-
-# send errors to hoptoad
-enable :raise_errors
-HoptoadNotifier.configure do |config|
- config.api_key = ENV['HOPTOAD_API_KEY']
-end
-use HoptoadNotifier::Rack
-
-run Sinatra::Application
+run Rack::URLMap.new \
+ "/" => TweetStream::App.new,
+ "/resque" => Resque::Server.new
View
@@ -0,0 +1,9 @@
+class IndexTweet
+
+ @queue = :normal
+
+ def self.perform(tweet)
+ puts "Indexing: #{tweet}"
+ end
+
+end
View
@@ -1,6 +1,6 @@
<html>
<head>
- <title>Pusher Example</title>
+ <title>Tweet streaming on Heroku</title>
<script src="http://js.pusherapp.com/1.8/pusher.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.0/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.12/jquery-ui.min.js"></script>

0 comments on commit 43452d8

Please sign in to comment.