Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Add support for Queue Classic

  • Loading branch information...
commit 94e9d732e35c6f2f81f91e48ba2ad2d46f051940 1 parent 1550c4b
@rwdaigle authored
View
3  Gemfile
@@ -9,15 +9,16 @@ gem 'pg'
gem 'make_resourceful'
gem 'dynamic_form'
gem 'wordnik'
+gem 'queue_classic', '~> 1.0.1'
gem 'haml'
gem 'jquery-rails'
gem 'unicorn'
+gem 'lograge'
group :production do
gem 'newrelic_rpm'
- gem 'lograge'
end
group :development do
View
5 Gemfile.lock
@@ -69,8 +69,10 @@ GEM
newrelic_rpm (3.3.2)
nokogiri (1.5.2)
permalink_fu (1.0.0)
- pg (0.13.2)
+ pg (0.11.0)
polyglot (0.3.3)
+ queue_classic (1.0.1)
+ pg (~> 0.11.0)
rack (1.4.1)
rack-cache (1.2)
rack (>= 0.4)
@@ -163,6 +165,7 @@ DEPENDENCIES
newrelic_rpm
permalink_fu
pg
+ queue_classic (~> 1.0.1)
rails (~> 3.2)
rails_log_stdout
rspec-rails
View
3  Procfile
@@ -1 +1,2 @@
-web: bundle exec unicorn -p $PORT -c ./config/unicorn.rb
+web: bundle exec unicorn -p $PORT -c ./config/unicorn.rb
+queue: bundle exec rake queue:work
View
3  Rakefile
@@ -5,3 +5,6 @@
require File.expand_path('../config/application', __FILE__)
Ptls::Application.load_tasks
+
+require 'queue_classic'
+require 'queue_classic/tasks'
View
5 app/models/processors/wordnik_processor.rb
@@ -5,13 +5,14 @@ class WordnikProcessor
class << self
def process!(subject, unit)
- result = nil
+ logger.info "[Processor] event=start processor=#{self.to_s} subject_id=#{subject.id} subject=#{subject.title} unit_id=#{unit.id} question=\"#{unit.question}\""
+ result = answer = nil
ms = Benchmark.ms do
result = Wordnik.word.get_definitions(unit.question, :limit => 1)
answer = result && !result.empty? ? result[0]['text'] : nil
unit.update_attributes(answer: answer)
end
- logger.info "[Process] processor=#{self.to_s} subject_id=#{subject.id} subject=\"#{subject.to_s}\" unit_id=#{unit.id} question=\"#{unit.question}\" answer=\"#{answer}\" execution=#{ms}ms"
+ logger.info "[Processor] event=end processor=#{self.to_s} subject_id=#{subject.id} subject=#{subject.title} unit_id=#{unit.id} question=\"#{unit.question}\" answer=\"#{answer}\" execution=#{ms}ms"
end
def logger
View
21 app/models/subject.rb
@@ -11,9 +11,20 @@ class Subject < ActiveRecord::Base
has_permalink :name
class << self
+
def seed(seed)
connection.execute(sanitize_sql(["SELECT SETSEED(?)", seed]))
end
+
+ def process!(subject_id, overwrite=false)
+ subject = self.find(subject_id)
+ subject.process!(overwrite) if subject
+ end
+
+ def process_unit!(subject_id, unit_id, overwrite=false)
+ subject = self.find(subject_id)
+ subject.process_unit!(unit_id, overwrite) if subject
+ end
end
def process!(overwrite = false)
@@ -24,6 +35,14 @@ def process!(overwrite = false)
end
end
end
+
+ def process_unit!(unit_id, overwrite = false)
+ processor_klass = resolve_processor_class
+ unit = Unit.find(unit_id)
+ if processor_klass && unit
+ processor_klass.process!(self, unit) if unit.answer.blank? || overwrite
+ end
+ end
# Override the param field to use in URIs
def to_param; permalink; end
@@ -38,7 +57,7 @@ def resolve_processor_class
begin
@resolved_processor_class ||= "#{unit_processor_type}_processor".classify.constantize
rescue
- logger.error "Unable to resolve processor class '#{unit_processor_type}' #{$!}"
+ logger.error "[Subject] event=error message=\"Unable to resolve processor class '#{unit_processor_type}' #{$!}\""
end
end
end
View
8 app/models/unit.rb
@@ -9,6 +9,8 @@ class Unit < ActiveRecord::Base
has_many :learnings, :dependent => :delete_all
has_many :reviews, :dependent => :delete_all
has_many :associations, :dependent => :delete_all
+
+ after_create :enqueue_for_processing
acts_as_list :scope => :subject
@@ -44,4 +46,10 @@ def clean(scope = :all, opts = {})
def <=>(other)
self.position <=> other.position
end
+
+ def enqueue_for_processing
+ if answer.blank?
+ $queue.enqueue('Subject.process_unit!', subject_id, self.id)
+ end
+ end
end
View
3  config/application.rb
@@ -46,5 +46,8 @@ class Application < Rails::Application
# Version of your assets, change this if you want to expire all your assets
config.assets.version = '1.0'
+
+ # Lograge logging
+ config.lograge.enabled = true
end
end
View
2  config/environments/production.rb
@@ -57,6 +57,4 @@
# Send deprecation notices to registered listeners
config.active_support.deprecation = :notify
-
- config.lograge.enabled = true
end
View
1  config/initializers/queue_classic.rb
@@ -0,0 +1 @@
+$queue = QC::Queue.new("queue_classic_jobs")
View
13 db/migrate/20120313190448_subject_processors.rb
@@ -7,11 +7,20 @@ def up
end
change_column :subjects, :from, :string, :null => true
change_column :subjects, :to, :string, :null => true
+
+ # Queue classic
+ create_table :queue_classic_jobs do |t|
+ t.text :details
+ t.timestamp :locked_at
+ end
end
def down
remove_column :subjects, :unit_processor_type
- change_column :subjects, :from, :string, :null => false
- change_column :subjects, :to, :string, :null => false
+ change_table :subjects do |t|
+ t.column :name, :string
+ end
+
+ drop_table :queue_classic_jobs
end
end
View
13 db/migrate/20120314004517_queue_classic_functions.rb
@@ -0,0 +1,13 @@
+class QueueClassic < ActiveRecord::Migration
+ def up
+ db = QC::Database.new
+ db.load_functions
+ db.disconnect
+ end
+
+ def down
+ db = QC::Database.new
+ db.unload_functions
+ db.disconnect
+ end
+end
View
7 db/schema.rb
@@ -11,7 +11,7 @@
#
# It's strongly recommended to check this file into your version control system.
-ActiveRecord::Schema.define(:version => 20120313190448) do
+ActiveRecord::Schema.define(:version => 20120314004517) do
create_table "associations", :force => true do |t|
t.integer "user_id", :null => false
@@ -33,6 +33,11 @@
add_index "learnings", ["unit_id"], :name => "index_learnings_on_unit_id"
add_index "learnings", ["user_id"], :name => "index_learnings_on_user_id"
+ create_table "queue_classic_jobs", :force => true do |t|
+ t.text "details"
+ t.datetime "locked_at"
+ end
+
create_table "reviews", :force => true do |t|
t.integer "user_id", :null => false
t.integer "unit_id", :null => false
View
13 lib/queue_worker.rb
@@ -0,0 +1,13 @@
+require 'queue_classic'
+
+class QueueWorker < QC::Worker
+
+ def handle_failure(job, exception)
+ logger.error "[Queue] event=error job=#{job.inspect} message=\"#{exception.message}\""
+ logger.error "[Queue] event=error backtrace=#{exception.backtrace}"
+ end
+
+ def logger
+ ActiveRecord::Base.logger
+ end
+end
View
9 lib/tasks/qc.rake
@@ -0,0 +1,9 @@
+require 'queue_classic'
+require 'queue_classic/tasks'
+require 'queue_worker'
+
+namespace :jobs do
+ task :work => :environment do
+ QueueWorker.new.start
+ end
+end
View
11 lib/tasks/queue.rake
@@ -0,0 +1,11 @@
+require 'queue_classic'
+require 'queue_classic/tasks'
+require 'queue_worker'
+
+namespace :queue do
+
+ desc "Custom queue classic worker task"
+ task :work => :environment do
+ QueueWorker.new.start
+ end
+end
Please sign in to comment.
Something went wrong with that request. Please try again.