class UniqueThread
Attributes
error_handlers[W]
logger[W]
redis[W]
locksmith[R]
stopwatch[R]
Public Class Methods
error_handlers()
click to toggle source
# File lib/unique_thread.rb, line 20 def error_handlers @error_handlers ||= [] end
logger()
click to toggle source
# File lib/unique_thread.rb, line 12 def logger @logger ||= default_logger end
new(name, downtime: 30)
click to toggle source
# File lib/unique_thread.rb, line 56 def initialize(name, downtime: 30) @stopwatch = Stopwatch.new(downtime: downtime) @locksmith = Locksmith.new(name: name, stopwatch: stopwatch) end
redis()
click to toggle source
# File lib/unique_thread.rb, line 16 def redis @redis ||= Redis.new end
safe_thread() { || ... }
click to toggle source
# File lib/unique_thread.rb, line 24 def safe_thread Thread.new do begin yield rescue StandardError => error report_error(error) retry end end end
Private Class Methods
default_logger()
click to toggle source
# File lib/unique_thread.rb, line 37 def default_logger case when defined?(Rails) && defined?(Rails::Console) Logger.new('/dev/null') when defined?(Rails) Rails.logger else Logger.new($stdout) end end
report_error(exception)
click to toggle source
# File lib/unique_thread.rb, line 48 def report_error(exception) logger.error(exception.inspect) error_handlers.each { |handler| handler.call(exception) } end
Public Instance Methods
run(&block)
click to toggle source
# File lib/unique_thread.rb, line 61 def run(&block) self.class.safe_thread do loop { try_being_the_unique_thread(&block) } end end
Private Instance Methods
try_being_the_unique_thread(&block)
click to toggle source
# File lib/unique_thread.rb, line 69 def try_being_the_unique_thread(&block) lock = locksmith.new_lock if lock.acquired? self.class.logger.info('Lock acquired! Running the unique thread.') lock.while_held(&block) else self.class.logger.debug('Could not acquire the lock. Sleeping until next attempt.') stopwatch.sleep_until_next_attempt(lock.locked_until.to_f) end end