class Applitools::Selenium::VGThreadPool

Attributes

concurrency[RW]

Public Class Methods

new(concurrency = 10) click to toggle source
# File lib/applitools/selenium/visual_grid/thread_pool.rb, line 11
def initialize(concurrency = 10)
  self.concurrency = concurrency
  @stopped = true
  @thread_group = ThreadGroup.new
  @semaphore = Mutex.new
  @next_task_block = nil
  @watchdog = nil
end

Public Instance Methods

on_next_task_needed(&block) click to toggle source
# File lib/applitools/selenium/visual_grid/thread_pool.rb, line 20
def on_next_task_needed(&block)
  @next_task_block = block if block_given?
end
start() click to toggle source
# File lib/applitools/selenium/visual_grid/thread_pool.rb, line 24
def start
  @semaphore.synchronize { @stopped = false }
  init_or_renew_threads
  @watchdog = Thread.new do
    catch(:exit) do
      loop do
        throw :exit if stopped?
        sleep 5
        init_or_renew_threads
      end
    end
  end
end
stop() click to toggle source
# File lib/applitools/selenium/visual_grid/thread_pool.rb, line 38
def stop
  @watchdog.exit
  @semaphore.synchronize do
    @stopped = true
  end
  @thread_group.list.each(&:join)
  stopped?
end
stopped?() click to toggle source
# File lib/applitools/selenium/visual_grid/thread_pool.rb, line 47
def stopped?
  @semaphore.synchronize { @stopped }
end

Private Instance Methods

init_or_renew_threads() click to toggle source
# File lib/applitools/selenium/visual_grid/thread_pool.rb, line 60
def init_or_renew_threads
  one_concurrency = 1 # Thread's moved to universal server
  (one_concurrency - @thread_group.list.count).times do
    logger.debug 'starting new thread (task worker)'
    next_thread
  end
end
next_task() click to toggle source
# File lib/applitools/selenium/visual_grid/thread_pool.rb, line 53
def next_task
  @semaphore.synchronize do
    return @next_task_block.call if @next_task_block && @next_task_block.respond_to?(:call)
  end
  nil
end
next_thread() click to toggle source
# File lib/applitools/selenium/visual_grid/thread_pool.rb, line 68
def next_thread
  thread = Thread.new do
    begin
      catch(:exit) do
        loop do
          throw :exit if stopped?
          task_to_run = next_task
          if task_to_run && task_to_run.respond_to?(:call)
            logger.debug "Executing new task... #{task_to_run.name}"
            task_to_run.call
            logger.debug 'Done!'
          else
            sleep 0.5
          end
        end
      end
      Applitools::EyesLogger.logger.info 'Worker is stopped'
    rescue => e
      Applitools::EyesLogger.logger.error "Failed to execute task - #{task_to_run.name}"
      Applitools::EyesLogger.logger.error e.message
      Applitools::EyesLogger.logger.error e.backtrace.join(' ')
    end
  end
  @thread_group.add thread
end