em-jack 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/lib/em-jack.rb +1 -1
  2. data/lib/em-jack/connection.rb +43 -6
  3. metadata +11 -4
@@ -11,6 +11,6 @@ end
11
11
 
12
12
  module EMJack
13
13
  module VERSION
14
- STRING = '0.1.0'
14
+ STRING = '0.1.2'
15
15
  end
16
16
  end
@@ -25,13 +25,11 @@ module EMJack
25
25
  @port = opts[:port] || 11300
26
26
  @tube = opts[:tube]
27
27
 
28
- @used_tube = 'default'
29
- @watched_tubes = ['default']
28
+ reset_tube_state
30
29
 
31
30
  @data = ""
32
31
  @retries = 0
33
32
  @in_reserve = false
34
- @deferrables = []
35
33
 
36
34
  @conn = EM::connect(host, port, EMJack::BeanstalkConnection) do |conn|
37
35
  conn.client = self
@@ -43,6 +41,17 @@ module EMJack
43
41
  end
44
42
  end
45
43
 
44
+ def reset_tube_state
45
+ prev_used = @used_tube
46
+ prev_watched = @watched_tubes.dup if @watched_tubes
47
+
48
+ @used_tube = 'default'
49
+ @watched_tubes = ['default']
50
+ @deferrables = []
51
+
52
+ return [prev_used, prev_watched]
53
+ end
54
+
46
55
  def fiber!
47
56
  eigen = (class << self
48
57
  self
@@ -230,15 +239,39 @@ module EMJack
230
239
 
231
240
  def disconnected
232
241
  d = @deferrables.dup
233
- @deferrables = []
242
+
243
+ prev_used, prev_watched = reset_tube_state
234
244
 
235
245
  set_deferred_status(nil)
236
246
  d.each { |df| df.fail(:disconnected) }
237
247
 
238
- raise EMJack::Disconnected if @retries >= RETRY_COUNT
248
+ if @retries >= RETRY_COUNT
249
+ if @disconnected_callback
250
+ @disconnected_callback.call
251
+ else
252
+ raise EMJack::Disconnected
253
+ end
254
+ end
239
255
 
240
256
  @retries += 1
241
- EM.add_timer(5) { @conn.reconnect(@host, @port) }
257
+ EM.add_timer(5) { reconnect(prev_used, prev_watched) }
258
+ end
259
+
260
+ def reconnect(prev_used, prev_watched)
261
+ @conn.reconnect(@host, @port)
262
+
263
+ use(prev_used) if prev_used
264
+ [ prev_watched ].flatten.compact.each do |tube|
265
+ watch(tube)
266
+ end
267
+ end
268
+
269
+ def reconnect!
270
+ @retries = 0
271
+
272
+ prev_used, prev_watched = reset_tube_state
273
+
274
+ EM.next_tick { reconnect(prev_used, prev_watched) }
242
275
  end
243
276
 
244
277
  def add_deferrable(&blk)
@@ -256,6 +289,10 @@ module EMJack
256
289
  def on_error(&blk)
257
290
  @error_callback = blk
258
291
  end
292
+
293
+ def on_disconnect(&blk)
294
+ @disconnected_callback = blk
295
+ end
259
296
 
260
297
  def received(data)
261
298
  @data << data
metadata CHANGED
@@ -1,12 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: em-jack
3
3
  version: !ruby/object:Gem::Version
4
+ hash: 31
4
5
  prerelease: false
5
6
  segments:
6
7
  - 0
7
8
  - 1
8
- - 1
9
- version: 0.1.1
9
+ - 2
10
+ version: 0.1.2
10
11
  platform: ruby
11
12
  authors:
12
13
  - dan sinclair
@@ -14,16 +15,18 @@ autorequire:
14
15
  bindir: bin
15
16
  cert_chain: []
16
17
 
17
- date: 2010-08-13 00:00:00 -04:00
18
+ date: 2010-12-01 00:00:00 -05:00
18
19
  default_executable:
19
20
  dependencies:
20
21
  - !ruby/object:Gem::Dependency
21
22
  name: eventmachine
22
23
  prerelease: false
23
24
  requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
24
26
  requirements:
25
27
  - - ">="
26
28
  - !ruby/object:Gem::Version
29
+ hash: 3
27
30
  segments:
28
31
  - 0
29
32
  version: "0"
@@ -72,23 +75,27 @@ rdoc_options:
72
75
  require_paths:
73
76
  - lib
74
77
  required_ruby_version: !ruby/object:Gem::Requirement
78
+ none: false
75
79
  requirements:
76
80
  - - ">="
77
81
  - !ruby/object:Gem::Version
82
+ hash: 3
78
83
  segments:
79
84
  - 0
80
85
  version: "0"
81
86
  required_rubygems_version: !ruby/object:Gem::Requirement
87
+ none: false
82
88
  requirements:
83
89
  - - ">="
84
90
  - !ruby/object:Gem::Version
91
+ hash: 3
85
92
  segments:
86
93
  - 0
87
94
  version: "0"
88
95
  requirements: []
89
96
 
90
97
  rubyforge_project:
91
- rubygems_version: 1.3.6
98
+ rubygems_version: 1.3.7
92
99
  signing_key:
93
100
  specification_version: 3
94
101
  summary: An evented Beanstalk client.