Module: RubyCliDaemon

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

Constant Summary collapse

TIMEOUT =
60 * 60
VERSION =
"0.4.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
36
37
38
39
# 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 (command = wait_for_command(server))
    command, env = command
    # execute the command in a fork
    capture :STDOUT, "#{socket}.out" do
      capture :STDERR, "#{socket}.err" do
        _, status = Process.wait2(fork do
          ENV.replace env
          ARGV.replace(command) # uncovered
          load path # uncovered
        end)

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