Class: Hireling

Inherits:
Object
  • Object
show all
Defined in:
lib/hireling/hireling.rb

Defined Under Namespace

Classes: Schedule, SchedulerProxy

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Class Attribute Details

.scheduleObject

Returns the value of attribute schedule.



5
6
7
# File 'lib/hireling/hireling.rb', line 5

def schedule
  @schedule
end

Instance Attribute Details

#reraise_work_item_errorsObject

Returns the value of attribute reraise_work_item_errors.



2
3
4
# File 'lib/hireling/hireling.rb', line 2

def reraise_work_item_errors
  @reraise_work_item_errors
end

Class Method Details

.each_app_hireling_name(rails_root) ⇒ Object



8
9
10
11
12
13
14
15
16
# File 'lib/hireling/hireling.rb', line 8

def self.each_app_hireling_name(rails_root)
  hireling_classes ||= Dir[File.join(rails_root, %w(app hirelings *.rb))]
  hireling_classes ||= Dir[File.join(%w(app hirelings *.rb))]
  hireling_classes.each do |hireling_file|
    require hireling_file
    hireling_name = File.basename(hireling_file, '.rb')
    yield hireling_name.to_sym
  end
end

.info(message) ⇒ Object



44
45
46
# File 'lib/hireling/hireling.rb', line 44

def self.info(message)
  Rails.logger.info "Hireling #{name} at #{Time.now}: #{message}"
end

.schedule_with_scheduler(scheduler) ⇒ Object



18
19
20
21
# File 'lib/hireling/hireling.rb', line 18

def self.schedule_with_scheduler(scheduler)
  Hireling.schedule.schedule(scheduler, new)
  info "scheduled"
end

Instance Method Details

#call(job) ⇒ Object



23
24
25
26
27
# File 'lib/hireling/hireling.rb', line 23

def call(job)
  self.class.info "starting work"
  work
  self.class.info "done working"
end

#workObject



29
30
31
# File 'lib/hireling/hireling.rb', line 29

def work
  raise "#{self.class.name} doesn't know how to do their job."
end

#work_on_items_from(scope) ⇒ Object



33
34
35
36
37
38
39
40
41
42
# File 'lib/hireling/hireling.rb', line 33

def work_on_items_from(scope)
  scope.find_each(:batch_size => 100) do |item|
    begin
      yield item
    rescue Exception => e
      Rails.logger.error "#{e.class}: #{e.message} - #{e.backtrace.join("\n  ")}"
      raise e if reraise_work_item_errors
    end
  end
end