wrenchmode-rack 0.0.10 → 0.0.11

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/wrenchmode/rack.rb +32 -10
  3. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 58f083348339efd1edfece7a5f8a2385e79058a9
4
- data.tar.gz: 730b06f6d008933865c86c4752b70428a3fb9a3c
3
+ metadata.gz: 584fb51bc86dff8160908557e0c4ff93c0db93c6
4
+ data.tar.gz: d37c07aaeacfd306c2d48a2a3893b8a4feb327c3
5
5
  SHA512:
6
- metadata.gz: 543dd1b8120bfccf7feff548bad8233287e827911442a5aaa7bbf4b26057a3b7187fc1bf260d84820e5d641d6cc660f2cc9e6a77c07d7f7d2cb846ee9e2a4f29
7
- data.tar.gz: 2518bc795d9a5085a0006eaa8ad8369adf26e7880a674ccb386d2a632a0f0c7e90711996384030b89e5a01a54b78f640b453f5cb84292fcd6a08226511328756
6
+ metadata.gz: de8659ad78ba58508d49a52e7fdba393460f6ae19a424f0e7f0cc1aaeb175d8f0c79ff9ebec2fb2b19b4d8810d11d85c373e125d831177f5fa961865516c1bbc
7
+ data.tar.gz: d89904abe80c669836dfdc7b2e52fbc60361351f7ed57a35eaf85e881a33eae72198f94d2596f8eb7094254e1dc8bef50a9a6348ed49f6193d1f0cb3234aa2a8
@@ -6,7 +6,7 @@ require 'ipaddr'
6
6
  module Wrenchmode
7
7
  class Rack
8
8
  CLIENT_NAME = "wrenchmode-rack"
9
- VERSION = '0.0.10'
9
+ VERSION = '0.0.11'
10
10
 
11
11
  SWITCH_URL_KEY = "switch_url"
12
12
  TEST_MODE_KEY = "test_mode"
@@ -45,6 +45,11 @@ module Wrenchmode
45
45
  @enable_reverse_proxy = false
46
46
 
47
47
  @made_contact = false
48
+
49
+ # Use a queue with 0 or 1 items to allow the threads to communicate. When a response from the main Wrenchmode server is received,
50
+ # parse the JSON and put the hash in the queue. Then, the main request thread will update the underlying middleware state
51
+ # the next time a request is received.
52
+ @queue = Queue.new
48
53
  end
49
54
 
50
55
  def call(env)
@@ -59,6 +64,15 @@ module Wrenchmode
59
64
  @check_thread ||= start_check_thread()
60
65
  sleep(0.01) while !@made_contact
61
66
 
67
+ # If we've gotten a new response from the server, use it
68
+ # to update local status
69
+ json = begin
70
+ @queue.pop(true)
71
+ rescue ThreadError
72
+ nil
73
+ end
74
+ update_status(json) if json
75
+
62
76
  should_display_wrenchmode = false
63
77
  if @switched
64
78
  req = ::Rack::Request.new(env)
@@ -78,9 +92,7 @@ module Wrenchmode
78
92
  end
79
93
  end
80
94
 
81
- def update_status
82
- json = fetch_status
83
-
95
+ def update_status(json)
84
96
  @switch_url = json[SWITCH_URL_KEY]
85
97
  test_mode = json[TEST_MODE_KEY] || false
86
98
  @switched = json[IS_SWITCHED_KEY] && !(@ignore_test_mode && test_mode)
@@ -91,23 +103,30 @@ module Wrenchmode
91
103
  @enable_reverse_proxy = json[REVERSE_PROXY_KEY]["enabled"]
92
104
  @reverse_proxy_config = symbolize_keys(json[REVERSE_PROXY_KEY])
93
105
  end
106
+ end
107
+
108
+ private
94
109
 
95
- rescue OpenURI::HTTPError => e
110
+ def fetch_status
111
+ inner_fetch
112
+ rescue Net::HTTPError => e
96
113
  log("Wrenchmode Check HTTP Error: #{e.message}")
97
114
  @switched = false
115
+ nil
98
116
  rescue JSON::JSONError => e
99
117
  log("Wrenchmode Check JSON Error: #{e.message}")
100
118
  @switched = false
119
+ nil
101
120
  rescue StandardError => e
102
121
  log("Wrenchmode Check Unknown Error: #{e.message}")
103
122
  @switched = false
123
+ nil
104
124
  ensure
105
125
  @made_contact = true
106
126
  end
107
127
 
108
- private
109
-
110
- def fetch_status
128
+ # Split this one out for easier mocking/stubbing in the specs
129
+ def inner_fetch
111
130
  payload = JSON.generate(build_update_package)
112
131
  body = nil
113
132
 
@@ -149,7 +168,11 @@ module Wrenchmode
149
168
  def start_check_thread
150
169
  Thread.new do
151
170
  while true do
152
- update_status
171
+ if json = fetch_status
172
+ @queue.clear()
173
+ @queue.push(json)
174
+ end
175
+
153
176
  sleep(@check_delay_secs)
154
177
  end
155
178
  end
@@ -202,6 +225,5 @@ module Wrenchmode
202
225
  def symbolize_keys(hash)
203
226
  hash.each_with_object({}) { |(k,v), h| h[k.to_sym] = v }
204
227
  end
205
-
206
228
  end
207
229
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: wrenchmode-rack
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.10
4
+ version: 0.0.11
5
5
  platform: ruby
6
6
  authors:
7
7
  - Micah Wedemeyer
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-04-26 00:00:00.000000000 Z
11
+ date: 2016-04-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rack