Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

using AwesomeQueue

  • Loading branch information...
commit 15a5d7c3c05989abaf0b48718455c106b5b9f04f 1 parent 83bcaa6
@caike caike authored
View
1  .jobs
@@ -0,0 +1 @@
+
View
2  app/models/guitar.rb
@@ -15,7 +15,7 @@ def to_s
def search_guitar_reviews
reviews_search = GuitarReviewsSearch.new(self.to_s)
- reviews_search.run
+ Rails.queue.push(reviews_search)
end
end
View
4 app/models/guitar_reviews_search.rb
@@ -5,8 +5,12 @@ def initialize(name)
end
def run
+ p ""
+ p ""
p "searching reviews for #{@name}"
sleep 10
p "done!"
+ p ""
+ p ""
end
end
View
7 config/application.rb
@@ -51,5 +51,12 @@ class Application < Rails::Application
# Enable app-wide asynchronous ActionMailer.
# config.action_mailer.async = true
+
+ initializer 'awesome queue' do |app|
+ p "Initializing ........"
+ app.queue_consumer = config.queue_consumer.start(app.queue)
+ at_exit { app.queue_consumer.shutdown }
+ end
+
end
end
View
47 config/environments/development.rb
@@ -38,6 +38,51 @@
# Debug mode disables concatenation and preprocessing of assets.
config.assets.debug = true
+
+ class GuitarStore::AwesomeQueue
+
+ def initialize
+ @queue = []
+ end
+
+ def push(job)
+ write_to_file(job)
+ end
+
+ def pop
+ if job = read_from_file
+ clear_file
+ job
+ else
+ Struct.new(:run).new(true)
+ end
+ end
+
+ private
+
+ JOBS_FILE = '.jobs'
+
+ def write_to_file(job)
+ File.open(JOBS_FILE,'w') do |file|
+ Marshal.dump(job, file)
+ end
+ end
+
+ def read_from_file
+ begin
+ File.open(JOBS_FILE, 'r') do |file|
+ Marshal.load(file)
+ end
+ rescue EOFError
+ end
+ end
+
+ def clear_file
+ File.truncate(JOBS_FILE, 0)
+ end
+ end
+
# In development, use an in-memory queue for queueing.
- config.queue = Rails::Queueing::Queue
+ config.queue = GuitarStore::AwesomeQueue
+
end
Please sign in to comment.
Something went wrong with that request. Please try again.