Permalink
Browse files

Rework process method ownership. Add ability to trigger subject proce…

…ssing via controller
  • Loading branch information...
1 parent df09084 commit 02bfebd877363ce5232d22ff83a53ff7ad37cc82 @rwdaigle committed Mar 14, 2012
@@ -48,6 +48,12 @@ def learn; redirect_to_next_learning end
def review; redirect_to_next_review; end
+ def process_units
+ $process_queue << { subject_id: current_object.id }
+ flash[:notice] = "#{current_object} has been queued for processing of empty units."
+ redirect_to current_object
+ end
+
private
def force_seed
@@ -5,14 +5,14 @@ class WordnikProcessor
class << self
def process!(subject, unit)
- logger.info "[Processor] event=start processor=#{self.to_s} subject_id=#{subject.id} subject=#{subject.title} unit_id=#{unit.id} question=\"#{unit.question}\""
+ 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 "[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"
+ 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
@@ -20,28 +20,19 @@ 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)
- processor_klass = resolve_processor_class
- if processor_klass
+ with_processor do |processor_klass|
(overwrite ? units : units.empty).each do |unit|
processor_klass.process!(self, unit)
end
end
end
- def process_unit!(unit_id, overwrite = false)
+ def with_processor(&block)
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
+ yield processor_klass if processor_klass
end
# Override the param field to use in URIs
View
@@ -40,13 +40,27 @@ class << self
# TODO: Find some way to patternize this
def clean(scope = :all, opts = {})
find(scope, {:select => 'units.*'}.merge(opts))
+ end
+
+ def process!(unit_id, overwrite=false)
+ unit = self.find(unit_id)
+ unit.process!(overwrite) if unit
end
+
end
def <=>(other)
self.position <=> other.position
end
+ def process!(overwrite = false)
+ if answer.blank? || overwrite
+ subject.with_processor do |processor_klass|
+ processor_klass.process!(self, unit)
+ end
+ end
+ end
+
def enqueue_for_processing
if answer.blank?
$queue.enqueue('Subject.process_unit!', subject_id, self.id)
View
@@ -26,6 +26,10 @@
get 'learn'
get 'review'
get 'quiz'
+ get 'process_units'
+ end
+ resources :units do
+ get 'process_all', :on => :collection
end
resources :learnings do
get 'today', :on => :collection

0 comments on commit 02bfebd

Please sign in to comment.