Class: CouchLoafer::Job

Inherits:
Object
  • Object
show all
Includes:
CouchSurfer::Model
Defined in:
lib/couch_loafer/job.rb

Class Method Summary collapse

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
  options = extract_options!(args)
  case instance
  when CouchSurfer::Model
    if foreground?
      instance.send(options[:method])
    else
      create_instance_job(options.merge(:class_name => instance.class.name, :instance_id => instance.id))
    end
  end
end

.availableObject



52
53
54
# File 'lib/couch_loafer/job.rb', line 52

def available
  by_importance(:limit => 5)
end

.foreground?Boolean

Returns:

  • (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_offObject



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.message, :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