class HaveAPI::Fs::Worker
Base class for classes that perform some regular work in a separate thread.
Attributes
runs[R]
Public Class Methods
new(fs)
click to toggle source
@param [HaveAPI::Fs::Fs] fs
# File lib/haveapi/fs/worker.rb, line 9 def initialize(fs) @fs = fs @run = true @pipe_r, @pipe_w = IO.pipe @runs = 0 @mutex = Mutex.new end
Public Instance Methods
last_time()
click to toggle source
The time when the work method was last run.
# File lib/haveapi/fs/worker.rb, line 48 def last_time @mutex.synchronize { @last_time } end
next_time()
click to toggle source
The time when the work method will be run next.
# File lib/haveapi/fs/worker.rb, line 53 def next_time @mutex.synchronize { @next_time } end
start()
click to toggle source
Start the work thread.
# File lib/haveapi/fs/worker.rb, line 18 def start @thread = Thread.new do @mutex.synchronize { @next_time = Time.now + start_delay } wait(start_delay) while @run do @fs.synchronize { work } @runs += 1 @mutex.synchronize do @last_time = Time.now @next_time = @last_time + work_period end wait(work_period) end end end
stop()
click to toggle source
Stop and join the work thread.
# File lib/haveapi/fs/worker.rb, line 38 def stop @run = false @pipe_w.write('CLOSE') @thread.join @pipe_r.close @pipe_w.close end
Protected Instance Methods
start_delay()
click to toggle source
@return [Integer] number of seconds to wait before the first work
# File lib/haveapi/fs/worker.rb, line 63 def start_delay raise NotImplementedError end
wait(n)
click to toggle source
# File lib/haveapi/fs/worker.rb, line 58 def wait(n) IO.select([@pipe_r], [], [], n) end
work()
click to toggle source
This method is regularly called to perform the work.
# File lib/haveapi/fs/worker.rb, line 73 def work raise NotImplementedError end
work_period()
click to toggle source
@return [Integer] number of seconds to wait between working
# File lib/haveapi/fs/worker.rb, line 68 def work_period raise NotImplementedError end