Class: HttpServerManager::Server

Inherits:
Object
  • Object
show all
Defined in:
lib/http_server_manager/server.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Server

Returns a new instance of Server.



7
8
9
10
11
12
13
# File 'lib/http_server_manager/server.rb', line 7

def initialize(options)
  @name                = options[:name]
  @host                = options[:host]
  @port                = options[:port]
  @timeout_in_seconds  = options[:timeout_in_seconds] || (ENV["timeout"] ? ENV["timeout"].to_i : 20)
  @deletable_artifacts = [ pid_file_path ]
end

Instance Attribute Details

#hostObject (readonly)

Returns the value of attribute host.



5
6
7
# File 'lib/http_server_manager/server.rb', line 5

def host
  @host
end

#nameObject (readonly)

Returns the value of attribute name.



5
6
7
# File 'lib/http_server_manager/server.rb', line 5

def name
  @name
end

#portObject (readonly)

Returns the value of attribute port.



5
6
7
# File 'lib/http_server_manager/server.rb', line 5

def port
  @port
end

Instance Method Details

#loggerObject



50
51
52
# File 'lib/http_server_manager/server.rb', line 50

def logger
  HttpServerManager.logger
end

#restart!Object



37
38
39
40
# File 'lib/http_server_manager/server.rb', line 37

def restart!
  stop!
  start!
end

#start!Object



15
16
17
18
19
20
21
22
23
24
25
# File 'lib/http_server_manager/server.rb', line 15

def start!
  if running?
    logger.info "#{@name} already running on #{@host}:#{@port}"
  else
    ensure_directories_exist
    pid = Process.spawn(start_command, [:out, :err] => [ log_file_path, "w" ])
    create_pid_file(pid)
    Wait.until_true!(description: "#{@name} is running", timeout_in_seconds: @timeout_in_seconds) { running? }
    logger.info "#{@name} started on #{@host}:#{@port}"
  end
end

#statusObject



42
43
44
# File 'lib/http_server_manager/server.rb', line 42

def status
  running? ? :started : :stopped
end

#stop!Object



27
28
29
30
31
32
33
34
35
# File 'lib/http_server_manager/server.rb', line 27

def stop!
  if running?
    Process.kill_tree(9, current_pid)
    FileUtils.rm_f(@deletable_artifacts)
    logger.info "#{@name} stopped"
  else
    logger.info "#{@name} not running"
  end
end

#to_sObject



46
47
48
# File 'lib/http_server_manager/server.rb', line 46

def to_s
  "#{@name} on #{@host}:#{@port}"
end