Class: Antilles

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(port = nil, log = nil) ⇒ Antilles

Returns a new instance of Antilles.



11
12
13
14
15
# File 'lib/antilles.rb', line 11

def initialize(port=nil, log=nil)
  @port = port || Antilles.find_available_port
  @pid_list = []
  @log = log
end

Instance Attribute Details

#logObject

Returns the value of attribute log.



9
10
11
# File 'lib/antilles.rb', line 9

def log
  @log
end

#portObject

Returns the value of attribute port.



8
9
10
# File 'lib/antilles.rb', line 8

def port
  @port
end

Class Method Details

.clearObject



112
113
114
# File 'lib/antilles.rb', line 112

def clear
  @server.clear rescue nil
end

.configure {|server| ... } ⇒ Object

Yields:



97
98
99
# File 'lib/antilles.rb', line 97

def configure(&block)
  yield server 
end

.find_available_portObject



120
121
122
123
124
125
# File 'lib/antilles.rb', line 120

def find_available_port
  server = TCPServer.new('127.0.0.1', 0)
  server.addr[1]
ensure
  server.close if server
end

.install(*args) ⇒ Object



116
117
118
# File 'lib/antilles.rb', line 116

def install(*args)
  server.install(*args)
end

.serverObject



107
108
109
110
# File 'lib/antilles.rb', line 107

def server
  @server ||= Antilles.new
  @server
end

.startObject



101
102
103
104
105
# File 'lib/antilles.rb', line 101

def start
  return server if server.started?
  server.start
  server
end

Instance Method Details

#call_api(method, path, params = {}, headers = {}) ⇒ Object

Raises:

  • (Timeout::Error)


75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/antilles.rb', line 75

def call_api(method, path, params = {}, headers = {})
  tries = 0
  retries = 7
  done = false
  while (tries <= retries) && !done
    begin
      http = HTTParty.send(method, "http://localhost:#{@port}#{path}",
                           :body => params, :headers => headers)
      done = true
    rescue SystemCallError
      Kernel.sleep(0.5)
    rescue Timeout::Error
      Kernel.sleep(0.5)
    ensure
      tries += 1
    end
  end
  raise Timeout::Error if tries > retries && retries >= 0
  return http
end

#clearObject



63
64
65
66
# File 'lib/antilles.rb', line 63

def clear
  http = call_api(:post,  '/api/clear')
  return http
end

#install(verb, path, body, options = {}) ⇒ Object



68
69
70
71
72
73
# File 'lib/antilles.rb', line 68

def install(verb, path, body, options={})
  api_headers = options.delete(:api_headers)
  params = { 'path' => path, 'body' => body.to_json }.merge(options)
  http = call_api(:post,  "/api/#{verb}", params.to_json, api_headers)
  return http
end

#pingObject



54
55
56
57
58
59
60
61
# File 'lib/antilles.rb', line 54

def ping
  begin
    http = call_api(:get,  '/api/ping')
  rescue Exception, Timeout::Error
    return false
  end
  return http.code == 200
end

#startObject



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/antilles.rb', line 21

def start
  pid = Kernel.fork
  if pid.nil? then
    args = {:fork => false,
            :host => 'localhost',
            :port => @port,
            :remote_configuration_path => '/api'}
    args[:log] = @log if @log
    Mimic.mimic(args) do
      [:INT, :TERM].each { |sig| trap(sig) { Kernel.exit!(0) } }
    end
    Kernel.exit!(0)
  end
  @pid_list.push(pid)
  wait
end

#started?Boolean

Returns:

  • (Boolean)


17
18
19
# File 'lib/antilles.rb', line 17

def started?
  @pid_list.size > 0
end

#stopObject



38
39
40
41
42
43
# File 'lib/antilles.rb', line 38

def stop
  @pid_list.each do |pid|
    Process.kill("TERM", pid)
  end
  @pid_list = []
end

#waitObject



45
46
47
48
49
50
51
52
# File 'lib/antilles.rb', line 45

def wait
  5.times do
    if ping then
      break
    end
    Kernel.sleep(0.1)
  end
end