2012-12-11 10:42:15 +00:00
|
|
|
module AbfWorker
|
2012-12-11 15:25:25 +00:00
|
|
|
class BaseObserver
|
2012-12-13 12:30:37 +00:00
|
|
|
COMPLETED = 0
|
|
|
|
FAILED = 1
|
|
|
|
PENDING = 2
|
|
|
|
STARTED = 3
|
|
|
|
CANCELED = 4
|
2012-12-11 11:47:32 +00:00
|
|
|
|
2013-02-05 18:49:26 +00:00
|
|
|
attr_accessor :status, :options
|
|
|
|
|
|
|
|
protected
|
|
|
|
|
|
|
|
def initialize(options, subject_class)
|
|
|
|
@status = options['status'].to_i
|
|
|
|
@options = options
|
|
|
|
@subject_class = subject_class
|
|
|
|
end
|
|
|
|
|
|
|
|
def subject
|
|
|
|
@subject ||= @subject_class.find options['id']
|
|
|
|
end
|
|
|
|
|
|
|
|
def perform
|
|
|
|
raise NotImplementedError, "You should implement this method"
|
|
|
|
end
|
|
|
|
|
|
|
|
def update_results
|
2012-12-12 12:32:47 +00:00
|
|
|
results = (subject.results || []) + options['results']
|
2013-02-05 18:49:26 +00:00
|
|
|
sort_results_and_save results
|
2012-12-12 12:32:47 +00:00
|
|
|
end
|
|
|
|
|
2013-02-05 18:49:26 +00:00
|
|
|
def sort_results_and_save(results, item = nil)
|
|
|
|
item ||= subject
|
|
|
|
item.results = results.sort_by{ |r| r['file_name'] }
|
|
|
|
item.save!
|
2012-12-11 11:47:32 +00:00
|
|
|
end
|
|
|
|
|
2012-12-11 10:42:15 +00:00
|
|
|
end
|
|
|
|
end
|