Class: Specwrk::Client
- Inherits:
-
Object
- Object
- Specwrk::Client
- Defined in:
- lib/specwrk/client.rb
Constant Summary collapse
- LockedResponseError =
Class.new(StandardError)
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.
51 52 53 54 55 56 |
# File 'lib/specwrk/client.rb', line 51 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.
49 50 51 |
# File 'lib/specwrk/client.rb', line 49 def last_request_at @last_request_at end |
#retry_count ⇒ Object (readonly)
Returns the value of attribute retry_count.
49 50 51 |
# File 'lib/specwrk/client.rb', line 49 def retry_count @retry_count end |
#worker_status ⇒ Object (readonly)
Returns the value of attribute worker_status.
49 50 51 |
# File 'lib/specwrk/client.rb', line 49 def worker_status @worker_status end |
Class Method Details
.build_http ⇒ Object
27 28 29 30 31 32 33 34 35 |
# File 'lib/specwrk/client.rb', line 27 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
17 18 19 20 21 22 23 24 25 |
# File 'lib/specwrk/client.rb', line 17 def self.connect? http = build_http http.start http.finish true rescue Errno::ECONNREFUSED, Errno::EHOSTUNREACH false end |
.wait_for_server! ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/specwrk/client.rb', line 37 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
58 59 60 |
# File 'lib/specwrk/client.rb', line 58 def close @mutex.synchronize { @http.finish } end |
#complete_and_fetch_examples(examples) ⇒ Object
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/specwrk/client.rb', line 105 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
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/specwrk/client.rb', line 88 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
62 63 64 65 66 |
# File 'lib/specwrk/client.rb', line 62 def heartbeat response = get "/heartbeat" response.code == "200" end |
#report ⇒ Object
68 69 70 71 72 73 74 75 76 |
# File 'lib/specwrk/client.rb', line 68 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
122 123 124 125 126 |
# File 'lib/specwrk/client.rb', line 122 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
78 79 80 81 82 83 84 85 86 |
# File 'lib/specwrk/client.rb', line 78 def shutdown response = delete "/shutdown" if response.code == "200" response.body else raise UnhandledResponseError.new("#{response.code}: #{response.body}") end end |