waithook 0.4.0 → 0.4.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 587f43be470d0045276f76af9a9518cde2c4c0fda3a48894a09bc8a4c053b78d
4
- data.tar.gz: bb935b3ba35498e9250f4df4f7f750939defd5c08eb6bbf6501bbfdec97dca74
3
+ metadata.gz: c3df6454a557d19f38729b89f1874b43111ab2d367c9f6f018064ad012d3ff2a
4
+ data.tar.gz: f933b2e75524e3301afbe50c8e56bbb525cea7a5503a15071cfad2ab4bd7ff59
5
5
  SHA512:
6
- metadata.gz: a8e43d129c6ba5043ce3b5aa1f279f3c16e104392f4d54ae6f3cb3a21b788037705933bbd037315477c7f29e919d0233b83d7c7fc2ced9de1dcbf670c20e06f2
7
- data.tar.gz: 62a00aa0dd68c73d4e1ed57cc941e1797f8c6e43df03ccb8d5d84001728925ff043100dbb72c98790cf1a61dcbe20e32b62c0141755a0113e1f84668070a7cca
6
+ metadata.gz: 8f6403f65b5bf735376deea1d9eb4157b66d87428e1c13717c71b80045bfa898487863e15d9a206c59575652cfaa6003dc14885746f1dcd22a2f6ebec03eac1a
7
+ data.tar.gz: b7f95a852f39039a4832f5a0f5889163e92fb855ade1b7e33a9c5b3ad5dbd0d587f393f8925156d5e8102f055eef0f7843e93be2f5122c1d9934bdbfca0ba70d
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- waithook (0.4.0)
4
+ waithook (0.4.2)
5
5
  websocket (~> 1.2)
6
6
 
7
7
  GEM
data/example/example.rb CHANGED
@@ -1,7 +1,7 @@
1
1
  require 'looksee'
2
2
  require 'net/http'
3
3
 
4
- require './websocket-client'
4
+ require_relative '../lib/waithook/websocket_client'
5
5
 
6
6
  HOST = 'localhost'
7
7
  PORT = 3012
@@ -1,7 +1,7 @@
1
1
  require 'looksee'
2
2
  require 'excon'
3
3
 
4
- require './websocket-client'
4
+ require_relative '../lib/waithook/websocket_client'
5
5
 
6
6
  #HOST = 'localhost'
7
7
  #PORT = 3012
@@ -1,7 +1,7 @@
1
- require_relative './waithook'
1
+ require_relative '../lib/waithook'
2
2
  require 'excon'
3
3
 
4
- waithook = Waithook.subscribe('my-super-test', host: 'localhost', port: 3012)
4
+ waithook = Waithook.subscribe(path: 'my-super-test', host: 'localhost', port: 3012)
5
5
 
6
6
  Excon.post('http://localhost:3012/my-super-test', body: Time.now.to_s)
7
7
 
data/lib/waithook/cli.rb CHANGED
@@ -61,4 +61,4 @@ class Waithook
61
61
  end
62
62
  end
63
63
  end
64
- end
64
+ end
@@ -1,3 +1,3 @@
1
1
  class Waithook
2
- VERSION = "0.4.0"
2
+ VERSION = "0.4.2"
3
3
  end
@@ -79,7 +79,7 @@ class Waithook
79
79
  end
80
80
  end
81
81
 
82
- # Estabilish connection to server and send initial handshake
82
+ # Establish connection to server and send initial handshake
83
83
  def connect!
84
84
  logger.info "Connecting to #{@host} #{@port}"
85
85
 
@@ -118,7 +118,7 @@ class Waithook
118
118
  def _start_parser!
119
119
  @reader, @writter = IO.pipe
120
120
  @processing_thread = Thread.new do
121
- Thread.current.abort_on_exception = true
121
+ # Thread.current.abort_on_exception = true
122
122
  begin
123
123
  logger.debug "Start reading in thread"
124
124
  handshake_response = _wait_handshake_response
@@ -129,8 +129,14 @@ class Waithook
129
129
  _handshake_recieved!
130
130
  _wait_frames!
131
131
  rescue Object => error
132
+ # if got error - close socket and end thread
133
+ # @last_connection_error will be detected in #wait_message and raised to caller thread,
134
+ # so caller can decide to reconnect or do something else
135
+ @last_connection_error = error
136
+ close!(send_close: false, kill_processing_thread: false) rescue nil
137
+ @messages.close
138
+
132
139
  logger.error "#{error.class}: #{error.message}\n#{error.backtrace.join("\n")}"
133
- raise error
134
140
  end
135
141
  end
136
142
  end
@@ -150,7 +156,7 @@ class Waithook
150
156
  _send_frame(:text, payload)
151
157
  end
152
158
 
153
- # Wait for new message (thread safe, all waiting threads will recieve a message)
159
+ # Wait for new message (thread safe, all waiting threads will receive a message)
154
160
  # Message format [type, data], e.g. <tt>[:text, "hello world"]</tt>
155
161
  def wait_new_message
156
162
  waiter = Waiter.new
@@ -158,14 +164,27 @@ class Waithook
158
164
  waiter.wait
159
165
  end
160
166
 
161
- # Synchroniously waiting for new message. Not thread safe, only one thread will receive message
167
+ # Synchronously waiting for new message. Not thread safe, only one thread will receive message
162
168
  # Message format [type, data], e.g. <tt>[:text, "hello world"]</tt>
163
169
  def wait_message
164
- @messages.pop
170
+ begin
171
+ message = @messages.pop
172
+ if (!message || message.first == nil) && @messages.closed? && @last_connection_error
173
+ raise @last_connection_error
174
+ end
175
+
176
+ return message
177
+ rescue => error
178
+ if @last_connection_error
179
+ raise @last_connection_error
180
+ else
181
+ raise error
182
+ end
183
+ end
165
184
  end
166
185
 
167
186
  # Wait until server handshake recieved.
168
- # Once it's recieved - we can are listening for new messages
187
+ # Once it's received - we can are listening for new messages
169
188
  # and connection is ready for sending data
170
189
  def wait_handshake!
171
190
  return true if @handshake_received
@@ -240,16 +259,20 @@ class Waithook
240
259
  data.join("")
241
260
  end
242
261
 
262
+ def socket_open?
263
+ @socket && !@socket.closed?
264
+ end
265
+
243
266
  # Send <tt>:close</tt> message to socket and close socket connection
244
- def close!(options = {send_close: true})
267
+ def close!(send_close: true, kill_processing_thread: true)
245
268
  unless @is_open
246
269
  logger.info "Already closed"
247
270
  return false
248
271
  end
249
272
 
250
273
  logger.info "Disconnecting from #{@host} #{@port}"
251
- @processing_thread.kill
252
- _send_frame(:close) if options[:send_close]
274
+ @processing_thread.kill if kill_processing_thread
275
+ _send_frame(:close) if send_close
253
276
  @socket.close
254
277
  @is_open = false
255
278
 
data/lib/waithook.rb CHANGED
@@ -118,7 +118,11 @@ class Waithook
118
118
  client.ping_sender
119
119
  logger.debug("Exit ping sender thread")
120
120
  rescue => error
121
- logger.warn("Error in ping sender thread: #{error.message} (#{error.class})\n#{error.backtrace.join("\n")}")
121
+ if error.message == "closed stream" && !@client.socket_open?
122
+ logger.debug("Connection closed, stopping ping sender thread")
123
+ else
124
+ logger.warn("Error in ping sender thread: #{error.message} (#{error.class})\n#{error.backtrace.join("\n")}")
125
+ end
122
126
  end
123
127
  end
124
128
  end
@@ -221,6 +225,8 @@ class Waithook
221
225
  end
222
226
  end
223
227
 
228
+ SKIP_FORWARD_HEADERS = %w[host content-length connection accept-encoding accept content-encoding]
229
+
224
230
  # Send webhook information to other HTTP server
225
231
  #
226
232
  # webhook = Waithook.subscribe(path: 'my-webhooks').wait_message
@@ -245,7 +251,9 @@ class Waithook
245
251
 
246
252
  request = http_klass.new(uri)
247
253
  headers.each do |key, value|
248
- request[key] = value
254
+ if !SKIP_FORWARD_HEADERS.include?(key.to_s.downcase)
255
+ request[key] = value
256
+ end
249
257
  end
250
258
 
251
259
  if body
@@ -96,4 +96,20 @@ describe "Waithook" do
96
96
 
97
97
  assert_includes(out, "Timeout::Error: execution expired")
98
98
  end
99
+
100
+ it "should throw exception when got socket error" do
101
+ waithook = default_client(logger: true)
102
+ socket = waithook.client.instance_variable_get(:@socket)
103
+ socket.close
104
+
105
+ captured_error = nil
106
+ begin
107
+ waithook.wait_message(raise_on_timeout: false)
108
+ rescue => error
109
+ captured_error = error
110
+ end
111
+
112
+ assert_instance_of(IOError, captured_error)
113
+ assert_equal("stream closed in another thread", captured_error.message)
114
+ end
99
115
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: waithook
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.4.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Pavel Evstigneev
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-10-22 00:00:00.000000000 Z
11
+ date: 2024-10-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: websocket