Sha256: af0f39167c8806b11fbf1197664e435b89867b64b53d65c4aa228a606c62a0a6

Contents?: true

Size: 965 Bytes

Versions: 1

Compression:

Stored size: 965 Bytes

Contents

class RailsServer
  attr_accessor :dir, :env, :verbose
  
  def initialize
    @env = "production"
  end
  
  # Tries to start a Rails server in the given directory.
  def run
    Dir.chdir(@dir) if @dir
    cmd = "rails server -e \"#{@env}\""
    
    if @verbose
      %x[#{cmd}]
    else
      require "php4r"
      Php4r.passthru(cmd)
    end
  end
  
  # Tries to start a Rails server. Restarts if it crashes automatically with a small sleep.
  def run_loop
    loop do
      self.run
      sleep 0.5
    end
  end
  
  # Returns true if the Rails server is running.
  def running?
    pid_path = "#{@dir}/tmp/pids/server.pid"
    return false unless File.exists?(pid_path)
    
    pid = File.read(pid_path).to_s.strip.to_i
    
    begin
      Process.getpgid(pid)
      return true
    rescue Errno::ESRCH
      return false
    end
  end
  
  # Runs a Rails server if it is not running already.
  def run_ensure
    self.run unless self.running?
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rails-server-0.0.0 lib/rails-server.rb