Class: Spring::ApplicationManager::Worker

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(env, args) ⇒ Worker



7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/spring-jruby/impl/pool/application_manager.rb', line 7

def initialize(env, args)
  @spring_env = Env.new
  channel, @remote_socket = WorkerChannel.pair
  @uuid = File.basename(@remote_socket.path).gsub('.sock', '')

  Bundler.with_clean_env do
    spawn_screen(
      env.merge("SPRING_SOCKET" => @remote_socket.path),
      args
    )
  end

  @socket = channel.to_io
end

Instance Attribute Details

#on_doneObject

Returns the value of attribute on_done.



5
6
7
# File 'lib/spring-jruby/impl/pool/application_manager.rb', line 5

def on_done
  @on_done
end

#pidObject (readonly)

Returns the value of attribute pid.



4
5
6
# File 'lib/spring-jruby/impl/pool/application_manager.rb', line 4

def pid
  @pid
end

#screen_nameObject (readonly)

Returns the value of attribute screen_name.



4
5
6
# File 'lib/spring-jruby/impl/pool/application_manager.rb', line 4

def screen_name
  @screen_name
end

#screen_pidObject (readonly)

Returns the value of attribute screen_pid.



4
5
6
# File 'lib/spring-jruby/impl/pool/application_manager.rb', line 4

def screen_pid
  @screen_pid
end

#socketObject (readonly)

Returns the value of attribute socket.



4
5
6
# File 'lib/spring-jruby/impl/pool/application_manager.rb', line 4

def socket
  @socket
end

#uuidObject (readonly)

Returns the value of attribute uuid.



4
5
6
# File 'lib/spring-jruby/impl/pool/application_manager.rb', line 4

def uuid
  @uuid
end

Instance Method Details

#await_bootObject



35
36
37
38
39
40
# File 'lib/spring-jruby/impl/pool/application_manager.rb', line 35

def await_boot
  Process.detach(screen_pid)
  @pid = socket.gets.to_i
  start_wait_thread(pid, socket) unless pid.zero?
  @remote_socket.close
end

#log(message) ⇒ Object



55
56
57
# File 'lib/spring-jruby/impl/pool/application_manager.rb', line 55

def log(message)
  @spring_env.log "[worker:#{uuid}] #{message}"
end

#spawn_screen(env, args) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/spring-jruby/impl/pool/application_manager.rb', line 22

def spawn_screen(env, args)
  @screen_name = "spring_#{@uuid}"

  @screen_pid =
    Process.spawn(
      env.merge("SPRING_SCREEN_NAME" => screen_name),
      "screen", "-d", "-m", "-S", screen_name,
      *args
    )

  log "(spawn #{@screen_pid})"
end

#start_wait_thread(pid, child) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/spring-jruby/impl/pool/application_manager.rb', line 42

def start_wait_thread(pid, child)
  Thread.new {
    begin
      Process.kill(0, pid) while sleep(1)
    rescue Errno::ESRCH
    end

    log "child #{pid} shutdown"

    on_done.call(self) if on_done
  }
end