Class: Spring::ApplicationManager::WorkerPool

Inherits:
Object
  • Object
show all
Defined in:
lib/spring-jruby/impl/pool/application_manager.rb

Instance Method Summary collapse

Constructor Details

#initialize(app_env, *app_args) ⇒ WorkerPool

Returns a new instance of WorkerPool.



61
62
63
64
65
66
67
68
69
70
71
72
73
74
# 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

Instance Method Details

#add_workerObject



76
77
78
79
80
81
82
83
84
85
86
87
88
# 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_sizeObject



129
130
131
# 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_workersObject



115
116
117
118
119
120
121
122
123
124
125
126
127
# 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) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# 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!Object



133
134
135
136
137
138
139
140
141
142
143
144
# 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) ⇒ Object



90
91
92
93
94
95
# 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