Class: CouchLoafer::Job
- Inherits:
-
Object
- Object
- CouchLoafer::Job
- Includes:
- CouchSurfer::Model
- Defined in:
- lib/couch_loafer/job.rb
Class Method Summary collapse
- .add(*args) ⇒ Object
- .available ⇒ Object
- .foreground? ⇒ Boolean
- .retrieve(id) ⇒ Object
- .work_off ⇒ Object
Instance Method Summary collapse
Class Method Details
.add(*args) ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/couch_loafer/job.rb', line 39 def add(*args) instance = args.first = (args) case instance when CouchSurfer::Model if foreground? instance.send([:method]) else create_instance_job(.merge(:class_name => instance.class.name, :instance_id => instance.id)) end end end |
.available ⇒ Object
52 53 54 |
# File 'lib/couch_loafer/job.rb', line 52 def available by_importance(:limit => 5) end |
.foreground? ⇒ Boolean
35 36 37 |
# File 'lib/couch_loafer/job.rb', line 35 def foreground? @@keep_in_foreground end |
.retrieve(id) ⇒ Object
63 64 65 66 67 |
# File 'lib/couch_loafer/job.rb', line 63 def retrieve(id) get(id) rescue RestClient::ResourceNotFound nil end |
.work_off ⇒ Object
56 57 58 59 60 61 |
# File 'lib/couch_loafer/job.rb', line 56 def work_off available.each do |job| job.lock!(worker_name) job.execute! end end |
Instance Method Details
#execute! ⇒ Object
90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/couch_loafer/job.rb', line 90 def execute! instance = ::Extlib::Inflection.constantize(class_name).get(instance_id) if arguments instance.send(method_name, *arguments) else instance.send(method_name) end destroy rescue StandardError => e fail!(e) end |
#fail!(e) ⇒ Object
86 87 88 |
# File 'lib/couch_loafer/job.rb', line 86 def fail!(e) update_attributes(:failed_at => Time.now, :owner => nil, :exception => {:message => e., :backtrace => e.backtrace}) end |
#lock!(worker_name) ⇒ Object
82 83 84 |
# File 'lib/couch_loafer/job.rb', line 82 def lock!(worker_name) update_attributes(:owner => worker_name) end |