Class: Clockworkd::Worker

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Worker

Returns a new instance of Worker.



18
19
20
21
22
23
24
# File 'lib/clockworkd/worker.rb', line 18

def initialize(options={})
  self.events = []

  @quiet = options.has_key?(:quiet) ? options[:quiet] : true
  self.class.clock_file = options[:clock_file] if options.has_key?(:clock_file)
  self.class.sleep_delay = options[:sleep_delay] if options.has_key?(:sleep_delay)
end

Instance Attribute Details

#eventsObject

name_prefix is ignored if name is set directly



16
17
18
# File 'lib/clockworkd/worker.rb', line 16

def events
  @events
end

Class Method Details

.after_forkObject

Hook method that is called after a new worker is forked



32
33
34
# File 'lib/clockworkd/worker.rb', line 32

def self.after_fork
  ::ActiveRecord::Base.establish_connection
end

.before_forkObject

Hook method that is called before a new worker is forked



27
28
29
# File 'lib/clockworkd/worker.rb', line 27

def self.before_fork
  ::ActiveRecord::Base.clear_all_connections!
end

Instance Method Details

#log(text, level = Logger::INFO) ⇒ Object



94
95
96
97
98
# File 'lib/clockworkd/worker.rb', line 94

def log(text, level = Logger::INFO)
  text = "[Worker(#{name})] #{text}"
  puts text unless @quiet
  logger.add level, "#{Time.now.strftime('%FT%T%z')}: #{text}" if logger
end

#nameObject

Every worker has a unique name which by default is the pid of the process. There are some advantages to overriding this with something which survives worker retarts: Workers can# safely resume working on tasks which are locked by themselves. The worker will assume that it crashed before.



40
41
42
43
# File 'lib/clockworkd/worker.rb', line 40

def name
  return @name unless @name.nil?
  "host:#{Socket.gethostname} pid:#{Process.pid}" rescue "pid:#{Process.pid}"
end

#name=(val) ⇒ Object

Sets the name of the worker. Setting the name to nil will reset the default worker name



47
48
49
# File 'lib/clockworkd/worker.rb', line 47

def name=(val)
  @name = val
end

#runObject



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/clockworkd/worker.rb', line 52

def run
  trap('TERM') { log 'Exiting...'; $exit = true }
  trap('INT')  { log 'Exiting...'; $exit = true }

  # Load scheduling
  log "Load file with scheduling #{self.class.clock_file.to_s}"
  yml_file = YAML::load(File.open(self.class.clock_file))
  yml_file.each do |key, value|
    job = key
    block = value["block"]
    cronline = value["cron"]
    self.events << Event.new(job, block, cronline)
  end

  # Start a processing
  log "Starting clock for #{self.events.size} events: [ " + self.events.map { |e| e.to_s }.join(' ') + " ]"
  loop do
    tick
    break if $exit
    sleep(self.class.sleep_delay)
    break if $exit
  end
end

#tick(t = Time.now) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/clockworkd/worker.rb', line 76

def tick(t=Time.now)
  to_run = self.events.select do |event|
    event.time?(t)
  end

  to_run.each do |event|
    log "Triggering #{event} with cronline #{event.cronline} and next time: #{event.next_time}"
    begin
      event.run(t)
    rescue Exception => e
      log "Unable to execute #{event} with error: #{e.to_s}", Logger::ERROR
      raise e
    end
  end

  to_run
end