Class: Clockworkd::Worker
- Inherits:
-
Object
- Object
- Clockworkd::Worker
- Defined in:
- lib/clockworkd/worker.rb
Instance Attribute Summary collapse
-
#events ⇒ Object
name_prefix is ignored if name is set directly.
Class Method Summary collapse
-
.after_fork ⇒ Object
Hook method that is called after a new worker is forked.
-
.before_fork ⇒ Object
Hook method that is called before a new worker is forked.
Instance Method Summary collapse
-
#initialize(options = {}) ⇒ Worker
constructor
A new instance of Worker.
- #log(text, level = Logger::INFO) ⇒ Object
-
#name ⇒ Object
Every worker has a unique name which by default is the pid of the process.
-
#name=(val) ⇒ Object
Sets the name of the worker.
- #run ⇒ Object
- #tick(t = Time.now) ⇒ Object
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(={}) self.events = [] @quiet = .has_key?(:quiet) ? [:quiet] : true self.class.clock_file = [:clock_file] if .has_key?(:clock_file) self.class.sleep_delay = [:sleep_delay] if .has_key?(:sleep_delay) end |
Instance Attribute Details
#events ⇒ Object
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_fork ⇒ Object
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_fork ⇒ Object
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 |
#name ⇒ Object
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 |
#run ⇒ Object
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 |