beanpool 0.1.0 → 0.1.1

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
  SHA1:
3
- metadata.gz: 39756d1320016f7c5ef3c729618c38489aa897e1
4
- data.tar.gz: 2f242163e3a922cc6898be819648a05343ba4d7a
3
+ metadata.gz: 797cf452e255d5c593af0b924b72abad25f109b0
4
+ data.tar.gz: ebb47bb2be3dc39254b270fc01d6df9b3135579d
5
5
  SHA512:
6
- metadata.gz: 69698c9aa7696acc46e85e43d1257ee8de887c1a64b2989627c66e8ad635728dc6cb8867e9d2ed43a1475b3a7f1d1e53fa16abd76ff39aa4b31a1369cf155673
7
- data.tar.gz: 5952adb8b31f29f4561b10af8b7c637a894f32ba4b12b07025e08adf254856d762e2da3d860036a2bfc6bd53865da5f2e39ca30eb92ccfc970a52e85fd6f44f4
6
+ metadata.gz: d8ee79d0a40d6155f4f6d6e4ff7d51195fb1a0e0821cc0fbc7dc0480ff53035aa8a165c2cefc7202128c99c3a42da2213b5b95755e27f5cc5f69b49f757492da
7
+ data.tar.gz: d70822c114b114c56010d19d779c2aa3d10c46df04a8be93a78610cbbcaafcc93c72026a4f0c4f35983f1c603a91c3b030b9a148ae0b0d8a01056a1faebe41c8
data/beanpool.gemspec CHANGED
@@ -23,5 +23,5 @@ Gem::Specification.new do |spec|
23
23
  spec.add_development_dependency "bundler", "~> 1.11"
24
24
  spec.add_development_dependency "rake", "~> 10.0"
25
25
  spec.add_development_dependency "rspec", "~> 3.0"
26
- spec.add_dependency "beaneater"
26
+ spec.add_dependency "beaneater", "~> 1.0"
27
27
  end
data/lib/beanpool.rb CHANGED
@@ -5,9 +5,9 @@ end
5
5
  class Beanpool
6
6
  attr_reader :connections
7
7
 
8
- def initialize(ip_array)
8
+ def initialize(ip_array, debug = false)
9
9
  raise 'Beanpool must be initialized with an array of IP addresses' unless ip_array.is_a?(Array) && !ip_array.empty?
10
- @connections = Beanpool::Connections.new(ip_array)
10
+ @connections = Beanpool::Connections.new(ip_array, debug)
11
11
  end
12
12
 
13
13
  def put(body, tube_name = 'default', pri = 32_000, ttr = 60, delay = 0)
@@ -29,6 +29,10 @@ class Beanpool
29
29
  return {}
30
30
  end
31
31
 
32
+ def get_stat_by_tube(tube_name, stat_name)
33
+ @connections.stats(tube_name,stat_name.to_sym)
34
+ end
35
+
32
36
  def get_ready_by_tube(name)
33
37
  @connections.stats(name, :current_jobs_ready)
34
38
  end
@@ -4,12 +4,31 @@ class Beanpool
4
4
  class Connections
5
5
  attr_reader :ip_array, :connections
6
6
 
7
- def initialize(ip_array)
7
+ def initialize(ip_array, debug)
8
8
  @ip_array = ip_array
9
+ @troubled_ips = {}
9
10
  @connections = {}
11
+ @debug = debug
10
12
  build_connections
11
13
  end
12
14
 
15
+ def notify(object)
16
+ if @debug
17
+ if object.is_a? Exception
18
+ backtrace_array = object.backtrace
19
+ backtrace_array.reject! { |x| x =~ /\.rvm/ }
20
+ backtrace_array.unshift(object.message.to_s)
21
+ raw_string = backtrace_array.join("\n")
22
+ puts "EXCEPTION: #{object.message}"
23
+ puts raw_string
24
+ elsif object.is_a?(Hash) || object.is_a?(Array)
25
+ puts object
26
+ elsif object.is_a?(String)
27
+ puts object
28
+ end
29
+ end
30
+ end
31
+
13
32
  def build_connections
14
33
  @ip_array.each do |ip|
15
34
  raise 'Only single IP for beaneater' if ip.is_a?(Array)
@@ -23,17 +42,64 @@ class Beanpool
23
42
  end
24
43
  end
25
44
 
26
- def get_job_from_tube(timeout = nil, tube_name = 'default')
45
+ def put_ip_in_timeout_and_reload(ip)
46
+ return unless @connections.size > 1
47
+ @troubled_ips[ip] = { time: Time.now }
48
+ @connections.delete(ip) unless @connections.size < 2
49
+ notify("Added #{ip} to troubled")
50
+ end
51
+
52
+ def connection_sample
53
+ check_times
27
54
  ip_id = @connections.keys.sample
55
+ ip_id
56
+ end
57
+
58
+ def check_times
59
+ @troubled_ips.each do |k, v|
60
+ notify("Checking troubled #{k}")
61
+ if v[:time] < Time.now - 60
62
+ begin
63
+ @connections[k] = Beaneater.new(k)
64
+ notify("Readded to live: #{k}")
65
+ @troubled_ips.delete(k)
66
+ rescue => ex
67
+ notify(ex)
68
+ # Keep retrying every minute
69
+ v[:time] = Time.now
70
+ end
71
+ end
72
+ end
73
+ end
74
+
75
+ def get_job_from_tube(timeout = nil, tube_name = 'default')
76
+ ip_id = connection_sample
28
77
  connection = @connections[ip_id]
29
- job = connection.tubes[tube_name].reserve(timeout)
30
- job
78
+ begin
79
+ job = connection.tubes[tube_name].reserve(timeout)
80
+ return job
81
+ rescue Beaneater::TimedOutError
82
+ return nil
83
+ rescue => ex
84
+ notify(ex)
85
+ notify("Exception IP: #{ip_id}")
86
+ put_ip_in_timeout_and_reload(ip_id)
87
+ return nil
88
+ end
31
89
  end
32
90
 
33
91
  def put_job_to_tube(body, pri = 32_000, ttr = 60, tube_name = 'default', delay = 0)
34
- ip_id = @connections.keys.sample
92
+ ip_id = connection_sample
35
93
  connection = @connections[ip_id]
36
- connection.tubes[tube_name].put(body, :pri => pri, :delay => delay, :ttr => ttr)
94
+ begin
95
+ connection.tubes[tube_name].put(body, :pri => pri, :delay => delay, :ttr => ttr)
96
+ rescue => ex
97
+ notify(ex)
98
+ put_ip_in_timeout_and_reload(ip_id)
99
+ ip_id = connection_sample
100
+ connection = @connections[ip_id]
101
+ connection.tubes[tube_name].put(body, :pri => pri, :delay => delay, :ttr => ttr)
102
+ end
37
103
  end
38
104
 
39
105
  def stats(tube_name, stat_name)
@@ -1,3 +1,3 @@
1
1
  class Beanpool
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.1"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: beanpool
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jason M.
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-01-16 00:00:00.000000000 Z
11
+ date: 2018-01-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -56,16 +56,16 @@ dependencies:
56
56
  name: beaneater
57
57
  requirement: !ruby/object:Gem::Requirement
58
58
  requirements:
59
- - - ">="
59
+ - - "~>"
60
60
  - !ruby/object:Gem::Version
61
- version: '0'
61
+ version: '1.0'
62
62
  type: :runtime
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
- - - ">="
66
+ - - "~>"
67
67
  - !ruby/object:Gem::Version
68
- version: '0'
68
+ version: '1.0'
69
69
  description: Simple pool for beanstalkd connections. Uses beaneater as underlying
70
70
  connections
71
71
  email: