Class: Sauce::Utilities::RailsServer

Inherits:
Object
  • Object
show all
Includes:
Sauce::Utilities
Defined in:
lib/sauce/utilities/rails_server.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Sauce::Utilities

incorrectly_integrated_warning, page_deprecation_message, #silence_stream, #wait_for_server_on_port, warn_if_suspect_misconfiguration

Instance Attribute Details

#portObject (readonly)

Returns the value of attribute port



53
54
55
# File 'lib/sauce/utilities/rails_server.rb', line 53

def port
  @port
end

Class Method Details

.is_rails_app?Boolean

Returns:

  • (Boolean)


20
21
22
# File 'lib/sauce/utilities/rails_server.rb', line 20

def self.is_rails_app?
  return !major_version.nil?
end

.major_versionObject



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/sauce/utilities/rails_server.rb', line 24

def self.major_version
  paths = ["script/server", "script/rails", "bin/rails"]
  startup_script = paths.detect {|path| File.exists? path}

  case startup_script
    when 'script/server'
      return 2
    when 'script/rails'
      return 3
    when 'bin/rails'
      return 4
    else
      return nil
  end
end

.process_argumentsObject



40
41
42
43
44
45
46
47
# File 'lib/sauce/utilities/rails_server.rb', line 40

def self.process_arguments
  case major_version
    when 2
      ["ruby", "script/server"]
    else
      ["bundle", "exec", "rails", "server"]
  end
end

.server_poolObject



49
50
51
# File 'lib/sauce/utilities/rails_server.rb', line 49

def self.server_pool
  @@server_pool ||= {}
end

.start_if_required(config) ⇒ Object



9
10
11
12
13
14
15
16
17
18
# File 'lib/sauce/utilities/rails_server.rb', line 9

def self.start_if_required(config)
  if config[:start_local_application] && self.is_rails_app?
    server = new
    server.start

    return server
  end

  return server
end

Instance Method Details

#startObject



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/sauce/utilities/rails_server.rb', line 55

def start
  @port = Sauce::Config.new[:application_port]

  if ENV["TEST_ENV_NUMBER"]
    @test_env = ENV["TEST_ENV_NUMBER"].to_i
  end

  STDERR.puts "Starting Rails server on port #{@port}..."

  @process_args = RailsServer.process_arguments
  @process_args.push *["-e", "test", "--port", "#{@port}"]

  if @test_env
    @process_args.push *["--pid", "#{Dir.pwd}/tmp/pids/server-#{@test_env}"]
  end

  @server = ChildProcess.build *@process_args
  @server.io.inherit!
  @server.start

  wait_for_server_on_port(@port)

  at_exit do
    @server.stop(3, "INT")
    RailsServer.server_pool.delete Thread.current.object_id
  end
  STDERR.puts "Rails server running!"

  RailsServer.server_pool[Thread.current.object_id] = @server
end

#stopObject



86
87
88
89
90
91
92
# File 'lib/sauce/utilities/rails_server.rb', line 86

def stop
  begin
    @server.stop(3, "INT")
  rescue
    STDERR.puts "Rails server could not be killed. Did it fail to start?"
  end
end