class GrpcForRails::Server

Constants

EXECUTABLES

Attributes

options[R]

Public Class Methods

new(options) click to toggle source
# File lib/grpc_for_rails/server.rb, line 16
def initialize(options)
  @options = options
  ENV['RAILS_ENV'] = @options[:env]

  # daemonization will change CWD so expand relative paths now
  options[:logfile] = File.expand_path(logfile) if logfile?
  options[:pidfile] = File.expand_path(pidfile) if pidfile?
end

Public Instance Methods

run() click to toggle source
# File lib/grpc_for_rails/server.rb, line 25
def run
  check_pid
  daemonize if daemonize?
  write_pid

  if logfile?
    redirect_output
  elsif daemonize?
    suppress_output
  end
  startup
end

Private Instance Methods

check_pid() click to toggle source
# File lib/grpc_for_rails/server.rb, line 87
def check_pid
  if pidfile?
    case pid_status(pidfile)
    when :running, :not_owned
      puts "A server is already running. Check #{pidfile}"
      exit(1)
    when :dead
      File.delete(pidfile)
    end
  end
end
daemonize() click to toggle source
# File lib/grpc_for_rails/server.rb, line 117
def daemonize
  exit if fork
  Process.setsid
  exit if fork
  #Dir.chdir "/"
end
daemonize?() click to toggle source
# File lib/grpc_for_rails/server.rb, line 55
def daemonize?
  options[:daemonize]
end
find_executable() click to toggle source
# File lib/grpc_for_rails/server.rb, line 164
def find_executable
  EXECUTABLES.find { |exe| File.file?(exe) }
end
force_delete_pidfile!() click to toggle source
# File lib/grpc_for_rails/server.rb, line 99
def force_delete_pidfile!
  if pidfile?
    File.delete(pidfile) rescue nil
  end
end
load_rails_environment!() click to toggle source
# File lib/grpc_for_rails/server.rb, line 149
def load_rails_environment!
  loop do
    if exe = find_executable
      Object.const_set(:APP_PATH, File.expand_path('config/application', Dir.pwd))
      require File.expand_path('../environment', APP_PATH)
      break
    end

    raise 'You are not in Rails project' if Pathname.new(Dir.pwd).root?

    # Otherwise keep moving upwards in search of an executable.
    Dir.chdir('..')
  end
end
logfile() click to toggle source
# File lib/grpc_for_rails/server.rb, line 59
def logfile
  options[:logfile]
end
logfile?() click to toggle source
# File lib/grpc_for_rails/server.rb, line 67
def logfile?
  !logfile.nil?
end
pid_status(pidfile) click to toggle source
# File lib/grpc_for_rails/server.rb, line 105
def pid_status(pidfile)
  return :exited unless File.exists?(pidfile)
  pid = ::File.read(pidfile).to_i
  return :dead if pid == 0
  Process.kill(0, pid)      # check process status
  :running
rescue Errno::ESRCH
  :dead
rescue Errno::EPERM
  :not_owned
end
pidfile() click to toggle source
# File lib/grpc_for_rails/server.rb, line 63
def pidfile
  options[:pidfile]
end
pidfile?() click to toggle source
# File lib/grpc_for_rails/server.rb, line 71
def pidfile?
  !pidfile.nil?
end
redirect_output() click to toggle source
# File lib/grpc_for_rails/server.rb, line 124
def redirect_output
  FileUtils.mkdir_p(File.dirname(logfile), mode: 0755)
  FileUtils.touch logfile
  File.chmod(0644, logfile)
  $stderr.reopen(logfile, 'a')
  $stdout.reopen($stderr)
  $stdout.sync = $stderr.sync = true
end
services() click to toggle source
# File lib/grpc_for_rails/server.rb, line 138
def services
  Dir["#{Rails.root}/app/grpc/services/**/*.rb"].each { |f| require f }

  result = Dir["#{Rails.root}/app/grpc/services/*.rb"].map do |file|
    class_name = file.split('/').last.sub(/\.rb$/, '').camelize
    Object.const_get class_name
  end
  result.delete_if { |e| !(e < GRPC::GenericService) }
  result
end
startup() click to toggle source
# File lib/grpc_for_rails/server.rb, line 40
def startup
  load_rails_environment!

  @server ||= GRPC::RpcServer.new pool_size: @options[:pool_size]
  @server.add_http2_port "#{@options[:host]}:#{@options[:port]}", :this_port_is_insecure
  services.each { |service| @server.handle service }
  begin
    @server.run
  rescue SignalException => e
    GRPC.logger.info "shutdown server..."
    @server.stop
    force_delete_pidfile!
  end
end
suppress_output() click to toggle source
# File lib/grpc_for_rails/server.rb, line 133
def suppress_output
  $stderr.reopen('/dev/null', 'a')
  $stdout.reopen($stderr)
end
write_pid() click to toggle source
# File lib/grpc_for_rails/server.rb, line 75
def write_pid
  if pidfile?
    begin
      File.open(pidfile, ::File::CREAT | ::File::EXCL | ::File::WRONLY){|f| f.write("#{Process.pid}") }
      at_exit { File.delete(pidfile) if File.exists?(pidfile) }
    rescue Errno::EEXIST
      check_pid
      retry
    end
  end
end