Class: Antilles
- Inherits:
-
Object
- Object
- Antilles
- Defined in:
- lib/antilles.rb
Instance Attribute Summary collapse
-
#log ⇒ Object
Returns the value of attribute log.
-
#port ⇒ Object
Returns the value of attribute port.
Class Method Summary collapse
- .clear ⇒ Object
- .configure {|server| ... } ⇒ Object
- .find_available_port ⇒ Object
- .install(*args) ⇒ Object
- .server ⇒ Object
- .start ⇒ Object
Instance Method Summary collapse
- #call_api(method, path, params = {}, headers = {}) ⇒ Object
- #clear ⇒ Object
-
#initialize(port = nil, log = nil) ⇒ Antilles
constructor
A new instance of Antilles.
- #install(verb, path, body, options = {}) ⇒ Object
- #ping ⇒ Object
- #start ⇒ Object
- #started? ⇒ Boolean
- #stop ⇒ Object
- #wait ⇒ Object
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
#log ⇒ Object
Returns the value of attribute log.
9 10 11 |
# File 'lib/antilles.rb', line 9 def log @log end |
#port ⇒ Object
Returns the value of attribute port.
8 9 10 |
# File 'lib/antilles.rb', line 8 def port @port end |
Class Method Details
.clear ⇒ Object
112 113 114 |
# File 'lib/antilles.rb', line 112 def clear @server.clear rescue nil end |
.configure {|server| ... } ⇒ Object
97 98 99 |
# File 'lib/antilles.rb', line 97 def configure(&block) yield server end |
.find_available_port ⇒ Object
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 |
.server ⇒ Object
107 108 109 110 |
# File 'lib/antilles.rb', line 107 def server @server ||= Antilles.new @server end |
.start ⇒ Object
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
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 |
#clear ⇒ Object
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, ={}) api_headers = .delete(:api_headers) params = { 'path' => path, 'body' => body.to_json }.merge() http = call_api(:post, "/api/#{verb}", params.to_json, api_headers) return http end |
#ping ⇒ Object
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 |
#start ⇒ Object
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
17 18 19 |
# File 'lib/antilles.rb', line 17 def started? @pid_list.size > 0 end |
#stop ⇒ Object
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 |
#wait ⇒ Object
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 |