class Spring::ApplicationManager::WorkerPool

Public Class Methods

new(app_env, *app_args) click to toggle source
# File lib/spring-jruby/impl/pool/application_manager.rb, line 61
def initialize(app_env, *app_args)
  @app_env = app_env
  @app_args = app_args
  @spring_env = Env.new

  @workers = []
  @workers_in_use = []
  @spawning_workers = []

  @check_mutex = Mutex.new
  @workers_mutex = Mutex.new

  run
end

Public Instance Methods

add_worker() click to toggle source
# File lib/spring-jruby/impl/pool/application_manager.rb, line 76
def add_worker
  worker = Worker.new(@app_env, @app_args)
  worker.on_done = method(:worker_done)
  @workers_mutex.synchronize { @spawning_workers << worker }
  Thread.new do
    worker.await_boot
    log "+ worker #{worker.pid} (#{worker.uuid})"
    @workers_mutex.synchronize do
      @spawning_workers.delete(worker)
      @workers << worker
    end
  end
end
all_size() click to toggle source
# File lib/spring-jruby/impl/pool/application_manager.rb, line 129
def all_size
  @workers_mutex.synchronize { @workers.size + @spawning_workers.size }
end
check_min_free_workers() click to toggle source
# File lib/spring-jruby/impl/pool/application_manager.rb, line 115
def check_min_free_workers
  if @check_mutex.try_lock
    # TODO: mutex, and dont do it if already in progress
    # do this in thread
    while all_size < Spring.pool_min_free_workers
      unless Spring.pool_spawn_parallel
        sleep 0.1 until @workers_mutex.synchronize { @spawning_workers.empty? }
      end
      add_worker
    end
    @check_mutex.unlock
  end
end
get_worker(spawn_new = true) click to toggle source
# File lib/spring-jruby/impl/pool/application_manager.rb, line 97
def get_worker(spawn_new = true)
  add_worker if spawn_new && all_size == 0

  worker = nil
  while worker.nil? && all_size > 0
    @workers_mutex.synchronize do
      worker = @workers.shift
      @workers_in_use << worker if worker
    end
    break if worker
    sleep 1
  end

  Thread.new { check_min_free_workers } if spawn_new

  worker
end
stop!() click to toggle source
# File lib/spring-jruby/impl/pool/application_manager.rb, line 133
def stop!
  if spawning_worker_pids.include?(nil)
    log "Waiting for workers to quit..."
    sleep 0.1 while spawning_worker_pids.include?(nil)
  end

  @workers_mutex.synchronize do
    (@spawning_workers + @workers_in_use + @workers).each do |worker|
      kill_worker(worker)
    end
  end
end
worker_done(worker) click to toggle source
# File lib/spring-jruby/impl/pool/application_manager.rb, line 90
def worker_done(worker)
  log "- worker #{worker.pid} (#{worker.uuid})"
  @workers_mutex.synchronize do
    @workers_in_use.delete(worker)
  end
end

Private Instance Methods

kill_worker(worker) click to toggle source
# File lib/spring-jruby/impl/pool/application_manager.rb, line 146
def kill_worker(worker)
  log "- worker #{worker.pid} (#{worker.uuid})."
  system("kill -9 #{worker.pid} > /dev/null 2>&1")
  system("screen -S #{worker.screen_name} -X quit > /dev/null 2>&1")
rescue
end
log(message) click to toggle source
# File lib/spring-jruby/impl/pool/application_manager.rb, line 163
def log(message)
  @spring_env.log "[worker:pool] #{message}"
end
run() click to toggle source
# File lib/spring-jruby/impl/pool/application_manager.rb, line 157
def run
  system("screen -wipe > /dev/null 2>&1")

  check_min_free_workers
end
spawning_worker_pids() click to toggle source
# File lib/spring-jruby/impl/pool/application_manager.rb, line 153
def spawning_worker_pids
  @spawning_workers.map { |worker| worker.pid }
end