Sha256: cdaaa2d22fda24dba6917b8125020a2a7c92d2450fe14e46c06dfc613f826a78

Contents?: true

Size: 1.72 KB

Versions: 42

Compression:

Stored size: 1.72 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

    PermKey = 'PUMA_DAEMON_PERM'
    RestartKey = 'PUMA_DAEMON_RESTART'

    # Called to tell things "Your now always in daemon mode,
    # don't try to reenter it."
    #
    def self.perm_daemonize
      ENV[PermKey] = "1"
    end

    def self.daemon?
      ENV.key?(PermKey) || ENV.key?(RestartKey)
    end

    def self.daemon_init
      return true if ENV.key?(PermKey)

      return false unless ENV.key? RestartKey

      master = ENV[RestartKey]

      # In case the master disappears early
      begin
        Process.kill "SIGUSR2", master.to_i
      rescue SystemCallError => e
      end

      ENV[RestartKey] = ""

      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[RestartKey] = Process.pid.to_s

      if k = ENV['PUMA_JRUBY_DAEMON_OPTS']
        ENV['JRUBY_OPTS'] = k
      end

      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

42 entries across 40 versions & 3 rubygems

Version Path
puma-3.6.2-java lib/puma/jruby_restart.rb
puma-3.6.2 lib/puma/jruby_restart.rb
puma-3.6.1-java lib/puma/jruby_restart.rb
puma-3.6.1 lib/puma/jruby_restart.rb
puma-3.6.0-java lib/puma/jruby_restart.rb
puma-3.6.0 lib/puma/jruby_restart.rb
puma-3.5.2-java lib/puma/jruby_restart.rb
puma-3.5.2 lib/puma/jruby_restart.rb
puma-3.5.1-java lib/puma/jruby_restart.rb
puma-3.5.1 lib/puma/jruby_restart.rb
puma-3.5.0-java lib/puma/jruby_restart.rb
puma-3.5.0 lib/puma/jruby_restart.rb
puma-3.4.0-java lib/puma/jruby_restart.rb
puma-3.4.0 lib/puma/jruby_restart.rb
puma-3.3.0-java lib/puma/jruby_restart.rb
puma-3.3.0 lib/puma/jruby_restart.rb
puma-3.2.0-java lib/puma/jruby_restart.rb
puma-3.2.0 lib/puma/jruby_restart.rb
puma-3.1.1-java lib/puma/jruby_restart.rb
puma-3.1.1 lib/puma/jruby_restart.rb