Sha256: 9dbbfd8725d12bb4ba396aab27798623067242c9b402d8fb60a184e428894b12

Contents?: true

Size: 1.27 KB

Versions: 14

Compression:

Stored size: 1.27 KB

Contents

require 'ffi'

module Puma
  module JRubyRestart
    extend FFI::Library
    ffi_lib 'c'

    attach_function :execlp, [:string, :varargs], :int
    attach_function :chdir, [:string], :int
    attach_function :fork, [], :int
    attach_function :exit, [:int], :void
    attach_function :setsid, [], :int

    def self.chdir_exec(dir, argv)
      chdir(dir)
      cmd = argv.first
      argv = ([:string] * argv.size).zip(argv).flatten
      argv << :string
      argv << nil
      execlp(cmd, *argv)
      raise SystemCallError.new(FFI.errno)
    end

    def self.daemon?
      ENV.key? 'PUMA_DAEMON_RESTART'
    end

    def self.daemon_init
      return false unless ENV.key? 'PUMA_DAEMON_RESTART'

      master = ENV['PUMA_DAEMON_RESTART']
      Process.kill "SIGUSR2", master.to_i

      setsid

      null = File.open "/dev/null", "w+"
      STDIN.reopen null
      STDOUT.reopen null
      STDERR.reopen null

      true
    end

    def self.daemon_start(dir, argv)
      ENV['PUMA_DAEMON_RESTART'] = Process.pid.to_s

      cmd = argv.first
      argv = ([:string] * argv.size).zip(argv).flatten
      argv << :string
      argv << nil

      chdir(dir)
      ret = fork
      return ret if ret != 0
      execlp(cmd, *argv)
      raise SystemCallError.new(FFI.errno)
    end
  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
puma-2.3.2-java lib/puma/jruby_restart.rb
puma-2.3.2 lib/puma/jruby_restart.rb
puma-2.3.1-java lib/puma/jruby_restart.rb
puma-2.3.1 lib/puma/jruby_restart.rb
puma-2.3.0-java lib/puma/jruby_restart.rb
puma-2.3.0 lib/puma/jruby_restart.rb
puma-2.2.2-java lib/puma/jruby_restart.rb
puma-2.2.2 lib/puma/jruby_restart.rb
puma-2.2.1-java lib/puma/jruby_restart.rb
puma-2.2.1 lib/puma/jruby_restart.rb
puma-2.2.0-java lib/puma/jruby_restart.rb
puma-2.2.0 lib/puma/jruby_restart.rb
puma-2.1.1-java lib/puma/jruby_restart.rb
puma-2.1.1 lib/puma/jruby_restart.rb