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