Module: RubyCliDaemon

Defined in:
lib/ruby_cli_daemon.rb,
lib/ruby_cli_daemon/version.rb

Constant Summary collapse

TIMEOUT =
60 * 60
VERSION =
"0.6.0"

Class Method Summary collapse

Class Method Details

.install(path) ⇒ Object



10
11
12
13
# File 'lib/ruby_cli_daemon.rb', line 10

def install(path)
  File.unlink(path) if File.exist?(path)
  File.symlink(File.expand_path("../bin/ruby-cli-daemon.sh", __dir__), path)
end

.start(socket, executable) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/ruby_cli_daemon.rb', line 15

def start(socket, executable)
  path = preload_gem(executable)
  server = create_socket(socket) # do this last, it signals we are ready

  loop do
    return unless IO.select([server], nil, nil, TIMEOUT)

    # execute the gems binary in a fork
    _, status = Process.wait2(fork do
      File.write("#{socket}.pid", Process.pid) # uncovered
      replace_env server # uncovered
      load path # uncovered
    end)

    # send back exit status
    File.write("#{socket}.status", status.exitstatus || 127)
  end
ensure
  # signal that this program is done so ruby-sli-daemon.sh restarts it
  File.unlink socket if File.exist?(socket)
end