Class: Specwrk::Client
- Inherits:
-
Object
- Object
- Specwrk::Client
- Defined in:
- lib/specwrk/client.rb
Instance Attribute Summary collapse
-
#last_request_at ⇒ Object
readonly
Returns the value of attribute last_request_at.
-
#retry_count ⇒ Object
readonly
Returns the value of attribute retry_count.
-
#worker_status ⇒ Object
readonly
Returns the value of attribute worker_status.
Class Method Summary collapse
Instance Method Summary collapse
- #close ⇒ Object
- #complete_and_fetch_examples(examples) ⇒ Object
- #fetch_examples ⇒ Object
- #heartbeat ⇒ Object
-
#initialize ⇒ Client
constructor
A new instance of Client.
- #report ⇒ Object
- #seed(examples, max_retries) ⇒ Object
- #shutdown ⇒ Object
Constructor Details
#initialize ⇒ Client
Returns a new instance of Client.
49 50 51 52 53 54 |
# File 'lib/specwrk/client.rb', line 49 def initialize @mutex = Mutex.new @http = self.class.build_http @http.start @worker_status = 1 end |
Instance Attribute Details
#last_request_at ⇒ Object (readonly)
Returns the value of attribute last_request_at.
47 48 49 |
# File 'lib/specwrk/client.rb', line 47 def last_request_at @last_request_at end |
#retry_count ⇒ Object (readonly)
Returns the value of attribute retry_count.
47 48 49 |
# File 'lib/specwrk/client.rb', line 47 def retry_count @retry_count end |
#worker_status ⇒ Object (readonly)
Returns the value of attribute worker_status.
47 48 49 |
# File 'lib/specwrk/client.rb', line 47 def worker_status @worker_status end |
Class Method Details
.build_http ⇒ Object
25 26 27 28 29 30 31 32 33 |
# File 'lib/specwrk/client.rb', line 25 def self.build_http uri = URI(ENV.fetch("SPECWRK_SRV_URI", "http://localhost:5138")) Specwrk.net_http.new(uri.host, uri.port).tap do |http| http.use_ssl = uri.scheme == "https" http.open_timeout = ENV.fetch("SPECWRK_TIMEOUT", "5").to_i http.read_timeout = ENV.fetch("SPECWRK_TIMEOUT", "5").to_i http.keep_alive_timeout = 300 end end |
.connect? ⇒ Boolean
15 16 17 18 19 20 21 22 23 |
# File 'lib/specwrk/client.rb', line 15 def self.connect? http = build_http http.start http.finish true rescue Errno::ECONNREFUSED, Errno::EHOSTUNREACH false end |
.wait_for_server! ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/specwrk/client.rb', line 35 def self.wait_for_server! timeout = Time.now + ENV.fetch("SPECWRK_TIMEOUT", "5").to_i connected = false until connected || Time.now > timeout connected = connect? sleep 0.1 unless connected end raise Errno::ECONNREFUSED unless connected end |
Instance Method Details
#close ⇒ Object
56 57 58 |
# File 'lib/specwrk/client.rb', line 56 def close @mutex.synchronize { @http.finish } end |
#complete_and_fetch_examples(examples) ⇒ Object
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/specwrk/client.rb', line 103 def complete_and_fetch_examples(examples) response = post "/complete_and_pop", body: examples.to_json case response.code when "200" JSON.parse(response.body, symbolize_names: true) when "204" raise WaitingForSeedError when "404" raise NoMoreExamplesError when "410" raise CompletedAllExamplesError else raise UnhandledResponseError.new("#{response.code}: #{response.body}") end end |
#fetch_examples ⇒ Object
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/specwrk/client.rb', line 86 def fetch_examples response = post "/pop" case response.code when "200" JSON.parse(response.body, symbolize_names: true) when "204" raise WaitingForSeedError when "404" raise NoMoreExamplesError when "410" raise CompletedAllExamplesError else raise UnhandledResponseError.new("#{response.code}: #{response.body}") end end |
#heartbeat ⇒ Object
60 61 62 63 64 |
# File 'lib/specwrk/client.rb', line 60 def heartbeat response = get "/heartbeat" response.code == "200" end |
#report ⇒ Object
66 67 68 69 70 71 72 73 74 |
# File 'lib/specwrk/client.rb', line 66 def report response = get "/report" if response.code == "200" JSON.parse(response.body, symbolize_names: true) else raise UnhandledResponseError.new("#{response.code}: #{response.body}") end end |
#seed(examples, max_retries) ⇒ Object
120 121 122 123 124 |
# File 'lib/specwrk/client.rb', line 120 def seed(examples, max_retries) response = post "/seed", body: {max_retries: max_retries, examples: examples}.to_json (response.code == "200") ? true : raise(UnhandledResponseError.new("#{response.code}: #{response.body}")) end |
#shutdown ⇒ Object
76 77 78 79 80 81 82 83 84 |
# File 'lib/specwrk/client.rb', line 76 def shutdown response = delete "/shutdown" if response.code == "200" response.body else raise UnhandledResponseError.new("#{response.code}: #{response.body}") end end |