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
14
# File 'lib/http_server_manager/server.rb', line 7

def initialize(options)
  @name                = options[:name]
  @host                = options[:host]
  @port                = options[:port]
  @ping_uri            = options[:ping_uri] || "/"
  @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


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

def logger
  HttpServerManager.logger
end

#restart!Object


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

def restart!
  stop!
  start!
end

#start!Object


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

def start!
  if running?
    logger.info "#{@name} already running on #{@host}:#{@port}"
  else
    ensure_directories_exist
    pid = Process.spawn(start_command, %i{ 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


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

def status
  running? ? :started : :stopped
end

#stop!Object


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

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


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

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