beaneater 0.3.1 → 1.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.
metadata CHANGED
@@ -1,83 +1,83 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: beaneater
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.1
4
+ version: 1.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nico Taing
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-06-28 00:00:00.000000000 Z
11
+ date: 2021-04-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: minitest
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - ~>
17
+ - - "~>"
18
18
  - !ruby/object:Gem::Version
19
19
  version: 4.1.0
20
20
  type: :development
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - ~>
24
+ - - "~>"
25
25
  - !ruby/object:Gem::Version
26
26
  version: 4.1.0
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: rake
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - '>='
31
+ - - ">="
32
32
  - !ruby/object:Gem::Version
33
33
  version: '0'
34
34
  type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - '>='
38
+ - - ">="
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: mocha
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
- - - '>='
45
+ - - ">="
46
46
  - !ruby/object:Gem::Version
47
47
  version: '0'
48
48
  type: :development
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
- - - '>='
52
+ - - ">="
53
53
  - !ruby/object:Gem::Version
54
54
  version: '0'
55
55
  - !ruby/object:Gem::Dependency
56
56
  name: term-ansicolor
57
57
  requirement: !ruby/object:Gem::Requirement
58
58
  requirements:
59
- - - '>='
59
+ - - ">="
60
60
  - !ruby/object:Gem::Version
61
61
  version: '0'
62
62
  type: :development
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
- - - '>='
66
+ - - ">="
67
67
  - !ruby/object:Gem::Version
68
68
  version: '0'
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: json
71
71
  requirement: !ruby/object:Gem::Requirement
72
72
  requirements:
73
- - - '>='
73
+ - - ">="
74
74
  - !ruby/object:Gem::Version
75
75
  version: '0'
76
76
  type: :development
77
77
  prerelease: false
78
78
  version_requirements: !ruby/object:Gem::Requirement
79
79
  requirements:
80
- - - '>='
80
+ - - ">="
81
81
  - !ruby/object:Gem::Version
82
82
  version: '0'
83
83
  description: Simple beanstalkd client for ruby
@@ -87,8 +87,9 @@ executables: []
87
87
  extensions: []
88
88
  extra_rdoc_files: []
89
89
  files:
90
- - .gitignore
91
- - .yardopts
90
+ - ".gitignore"
91
+ - ".travis.yml"
92
+ - ".yardopts"
92
93
  - CHANGELOG.md
93
94
  - Gemfile
94
95
  - LICENSE.txt
@@ -104,8 +105,6 @@ files:
104
105
  - lib/beaneater/job.rb
105
106
  - lib/beaneater/job/collection.rb
106
107
  - lib/beaneater/job/record.rb
107
- - lib/beaneater/pool.rb
108
- - lib/beaneater/pool_command.rb
109
108
  - lib/beaneater/stats.rb
110
109
  - lib/beaneater/stats/fast_struct.rb
111
110
  - lib/beaneater/stats/stat_struct.rb
@@ -118,8 +117,6 @@ files:
118
117
  - test/errors_test.rb
119
118
  - test/job_test.rb
120
119
  - test/jobs_test.rb
121
- - test/pool_command_test.rb
122
- - test/pool_test.rb
123
120
  - test/prompt_regexp_test.rb
124
121
  - test/stat_struct_test.rb
125
122
  - test/stats_test.rb
@@ -127,26 +124,26 @@ files:
127
124
  - test/tube_test.rb
128
125
  - test/tubes_test.rb
129
126
  homepage: ''
130
- licenses: []
127
+ licenses:
128
+ - MIT
131
129
  metadata: {}
132
- post_install_message:
130
+ post_install_message:
133
131
  rdoc_options: []
134
132
  require_paths:
135
133
  - lib
136
134
  required_ruby_version: !ruby/object:Gem::Requirement
137
135
  requirements:
138
- - - '>='
136
+ - - ">="
139
137
  - !ruby/object:Gem::Version
140
138
  version: '0'
141
139
  required_rubygems_version: !ruby/object:Gem::Requirement
142
140
  requirements:
143
- - - '>='
141
+ - - ">="
144
142
  - !ruby/object:Gem::Version
145
143
  version: '0'
146
144
  requirements: []
147
- rubyforge_project:
148
- rubygems_version: 2.0.2
149
- signing_key:
145
+ rubygems_version: 3.0.8
146
+ signing_key:
150
147
  specification_version: 4
151
148
  summary: Simple beanstalkd client for ruby.
152
149
  test_files:
@@ -155,12 +152,9 @@ test_files:
155
152
  - test/errors_test.rb
156
153
  - test/job_test.rb
157
154
  - test/jobs_test.rb
158
- - test/pool_command_test.rb
159
- - test/pool_test.rb
160
155
  - test/prompt_regexp_test.rb
161
156
  - test/stat_struct_test.rb
162
157
  - test/stats_test.rb
163
158
  - test/test_helper.rb
164
159
  - test/tube_test.rb
165
160
  - test/tubes_test.rb
166
- has_rdoc:
@@ -1,166 +0,0 @@
1
- # Simple ruby client for interacting with beanstalkd.
2
- module Beaneater
3
- # Represents collection of beanstalkd connections.
4
- class Pool
5
- # Default number of retries to send a command to a connection
6
- MAX_RETRIES = 3
7
-
8
- # @!attribute connections
9
- # @return [Array<Beaneater::Connection>] returns Collection of connections
10
- attr_reader :connections
11
-
12
- # Initialize new connection
13
- #
14
- # @param [Array<String>] addresses Array of beanstalkd server addresses
15
- # @example
16
- # Beaneater::Pool.new(['localhost:11300', '127.0.0.1:11300'])
17
- #
18
- # ENV['BEANSTALKD_URL'] = 'localhost:11300,127.0.0.1:11300'
19
- # @bp = Beaneater::Pool.new
20
- # @bp.connections.first.host # => 'localhost'
21
- # @bp.connections.last.host # => '127.0.0.1'
22
- #
23
- def initialize(addresses=nil)
24
- addresses = addresses || host_from_env || Beaneater.configuration.beanstalkd_url
25
- @connections = Array(addresses).map { |a| Connection.new(a) }
26
- end
27
-
28
- # Returns Beaneater::Stats object for accessing beanstalk stats.
29
- #
30
- # @return [Beaneater::Stats] stats object
31
- # @api public
32
- def stats
33
- @stats ||= Stats.new(self)
34
- end
35
-
36
- # Returns Beaneater::Jobs object for accessing job related functions.
37
- #
38
- # @return [Beaneater::Jobs] jobs object
39
- # @api public
40
- def jobs
41
- @jobs ||= Jobs.new(self)
42
- end
43
-
44
- # Returns Beaneater::Tubes object for accessing tube related functions.
45
- #
46
- # @return [Beaneater::Tubes] tubes object
47
- # @api public
48
- def tubes
49
- @tubes ||= Tubes.new(self)
50
- end
51
-
52
- # Sends command to every beanstalkd server set in the pool.
53
- #
54
- # @param [String] command Beanstalkd command
55
- # @param [Hash{String => String, Boolean}] options socket connections options
56
- # @param [Proc] block Block passed to socket connection during transmit
57
- # @return [Array<Hash{String => String, Number}>] Beanstalkd command response from each instance
58
- # @example
59
- # @pool.transmit_to_all("stats")
60
- #
61
- def transmit_to_all(command, options={}, &block)
62
- res_exception = nil
63
- res = connections.map { |conn|
64
- begin
65
- safe_transmit { conn.transmit(command, options, &block) }
66
- rescue UnexpectedResponse => ex # not the correct status
67
- res_exception = ex
68
- nil
69
- end
70
- }.compact
71
- raise res_exception if res.none? && res_exception
72
- res
73
- end
74
-
75
- # Send command to each beanstalkd servers until getting response expected
76
- #
77
- # @param [String] command Beanstalkd command
78
- # @param [Hash{String => String, Boolean}] options socket connections options
79
- # @param [Proc] block Block passed in socket connection object
80
- # @return [Array<Hash{String => String, Number}>] Beanstalkd command response from the instance
81
- # @example
82
- # @pool.transmit_until_res('peek-ready', :status => "FOUND", &block)
83
- #
84
- def transmit_until_res(command, options={}, &block)
85
- status_expected = options.delete(:status)
86
- res_exception = nil
87
- connections.each do |conn|
88
- begin
89
- res = safe_transmit { conn.transmit(command, options, &block) }
90
- return res if res[:status] == status_expected
91
- rescue UnexpectedResponse => ex # not the correct status
92
- res_exception = ex
93
- next
94
- end
95
- end
96
- raise res_exception if res_exception
97
- end
98
-
99
- # Sends command to a random beanstalkd server in the pool.
100
- #
101
- # @param [String] command Beanstalkd command
102
- # @param [Hash{String => String,Boolean}] options socket connections options
103
- # @param [Proc] block Block passed in socket connection object
104
- # @return [Array<Hash{String => String, Number}>] Beanstalkd command response from the instance
105
- # @example
106
- # @pool.transmit_to_rand("stats", :match => /\n/)
107
- #
108
- def transmit_to_rand(command, options={}, &block)
109
- safe_transmit do
110
- conn = connections.respond_to?(:sample) ? connections.sample : connections.choice
111
- conn.transmit(command, options, &block)
112
- end
113
- end
114
-
115
- # Closes all connections within the pool.
116
- #
117
- # @example
118
- # @pool.close
119
- #
120
- def close
121
- while @connections.any?
122
- conn = @connections.pop
123
- conn.close
124
- end
125
- end
126
-
127
- protected
128
-
129
- # Transmit command to beanstalk connections safely handling failed connections
130
- #
131
- # @param [Proc] block The command to execute.
132
- # @return [Object] Result of the block passed
133
- # @raise [Beaneater::DrainingError,Beaneater::NotConnected] Could not connect to Beanstalk client
134
- # @example
135
- # safe_transmit { conn.transmit('foo') }
136
- # # => "result of foo command from beanstalk"
137
- #
138
- def safe_transmit(&block)
139
- retries = 1
140
- begin
141
- yield
142
- rescue DrainingError, EOFError, Errno::ECONNRESET, Errno::EPIPE => ex
143
- # TODO remove faulty connections from pool?
144
- # https://github.com/kr/beanstalk-client-ruby/blob/master/lib/beanstalk-client/connection.rb#L405-410
145
- if retries < MAX_RETRIES
146
- retries += 1
147
- retry
148
- else # finished retrying, fail out
149
- ex.is_a?(DrainingError) ? raise(ex) : raise(NotConnected, "Could not connect!")
150
- end
151
- end
152
- end # transmit_call
153
-
154
- # The hosts provided by BEANSTALKD_URL environment variable, if available.
155
- #
156
- # @return [Array] Set of beanstalkd host addresses
157
- # @example
158
- # ENV['BEANSTALKD_URL'] = "localhost:1212,localhost:2424"
159
- # # => ['localhost:1212', 'localhost:2424']
160
- #
161
- def host_from_env
162
- ENV['BEANSTALKD_URL'].respond_to?(:length) && ENV['BEANSTALKD_URL'].length > 0 && ENV['BEANSTALKD_URL'].split(',').map(&:strip)
163
- end
164
-
165
- end # Pool
166
- end # Beaneater
@@ -1,79 +0,0 @@
1
- require 'set'
2
-
3
- module Beaneater
4
- # Represents collection of pool related commands.
5
- class PoolCommand
6
- # @!attribute pool
7
- # @return [Beaneater::Pool] returns Pool object
8
- attr_reader :pool
9
-
10
- # Initialize new connection
11
- #
12
- # @param [Beaneater::Pool] pool Pool object
13
- def initialize(pool)
14
- @pool = pool
15
- end
16
-
17
- # Delegate to Pool#transmit_to_all and if needed will merge responses from beanstalkd.
18
- #
19
- # @param [String] body Beanstalkd command
20
- # @param [Hash{String => String, Boolean}] options socket connections options
21
- # @option options [Boolean] merge Ask for merging responses or not
22
- # @param [Proc] block Block passed in socket connection object
23
- # @example
24
- # @pool.transmit_to_all("stats")
25
- #
26
- def transmit_to_all(body, options={}, &block)
27
- merge = options.delete(:merge)
28
- res = pool.transmit_to_all(body, options, &block)
29
- first = res.find { |r| r && r[:status] }
30
- if first && merge
31
- res = { :status => first[:status], :body => sum_items(res.map { |r| r[:body] }) }
32
- end
33
- res
34
- end
35
-
36
- # Delegate missing methods to pool
37
- # @api public
38
- def method_missing(name, *args, &block)
39
- if pool.respond_to?(name)
40
- pool.send(name, *args, &block)
41
- else # not a known pool command
42
- super
43
- end
44
- end
45
-
46
- protected
47
-
48
- # Selects items from collection and then merges the individual values
49
- # Supports array of hashes or array of arrays
50
- #
51
- # @param [Array<Hash, Array>] hs Collection of responses returned from beanstalkd
52
- # @return [Hash{Symbol => String}] Merged responses combining values from all the hash bodies
53
- # @example
54
- # self.sum_items([{ :foo => 1, :bar => 5 }, { :foo => 2, :bar => 3 }])
55
- # => { :foo => 3, :bar => 8 }
56
- # self.sum_items([['foo', 'bar'], ['foo', 'bar', 'baz']])
57
- # => ['foo', 'bar', 'baz']
58
- #
59
- def sum_items(items)
60
- if items.first.is_a?(Hash)
61
- items.select { |h| h.is_a?(Hash) }.
62
- inject({}) { |a,b| a.merge(b) { |k,o,n| combine_stats(k, o, n) } }
63
- elsif items.first.is_a?(Array)
64
- items.flatten.uniq
65
- end
66
- end
67
-
68
- # Combine two values for given key
69
- #
70
- # @param [String] k key name within response hash
71
- # @return [Set,Integer] combined value for stat
72
- # @example
73
- # self.combine_stats('total_connections', 4, 5) # => 9
74
- #
75
- def combine_stats(k, a, b)
76
- ['name', 'version', 'pid'].include?(k) ? Set[a] + Set[b] : a + b
77
- end
78
- end # PoolCommand
79
- end # Beaneater
@@ -1,90 +0,0 @@
1
- # test/connection_test.rb
2
-
3
- require File.expand_path('../test_helper', __FILE__)
4
-
5
- describe Beaneater::PoolCommand do
6
-
7
- describe 'for #new' do
8
- before do
9
- @pool = stub
10
- @command = Beaneater::PoolCommand.new(@pool)
11
- end
12
-
13
- it "should store pool" do
14
- assert_equal @pool, @command.pool
15
- end
16
- end #new
17
-
18
- describe 'for #transmit_to_all' do
19
- describe 'for regular command' do
20
- before do
21
- @pool = stub(:transmit_to_all => [{ :body => "foo", :status => "OK" }])
22
- @command = Beaneater::PoolCommand.new(@pool)
23
- end
24
-
25
- it "can run regular command" do
26
- res = @command.transmit_to_all("foo")
27
- assert_equal "OK", res[0][:status]
28
- assert_equal "foo", res[0][:body]
29
- end
30
- end # regular command
31
-
32
- describe 'for merge command with hashes' do
33
- before do
34
- @pool = stub(:transmit_to_all => [
35
- { :body => { 'x' => 1, 'version' => 1.1 }, :status => "OK"},
36
- { :body => { 'x' => 3,'version' => 1.2 }, :status => "OK" }
37
- ])
38
- @command = Beaneater::PoolCommand.new(@pool)
39
- end
40
-
41
- it "can run merge command " do
42
- cmd = @command.transmit_to_all("bar", :merge => true)
43
- assert_equal "OK", cmd[:status]
44
- assert_equal 4, cmd[:body]['x']
45
- assert_equal Set[1.1, 1.2], cmd[:body]['version']
46
- end
47
- end # merged command
48
-
49
- describe 'for merge command with arrays' do
50
- before do
51
- @pool = stub(:transmit_to_all => [
52
- { :body => ['foo', 'bar'], :status => "OK"},
53
- { :body => ['foo', 'bar', 'baz'], :status => "OK" }
54
- ])
55
- @command = Beaneater::PoolCommand.new(@pool)
56
- end
57
-
58
- it "can run merge command " do
59
- cmd = @command.transmit_to_all("bar", :merge => true)
60
- assert_equal "OK", cmd[:status]
61
- assert_equal ['foo', 'bar', 'baz'].sort, cmd[:body].sort
62
- end
63
- end # merged command
64
- end # transmit_to_all
65
-
66
- describe 'for #method_missing' do
67
- describe '#transmit_to_rand' do
68
- before do
69
- @pool = stub
70
- @pool.expects(:transmit_to_rand).with('foo').returns('OK')
71
- @command = Beaneater::PoolCommand.new(@pool)
72
- end
73
-
74
- it 'delegates to connection' do
75
- assert_equal 'OK', @command.transmit_to_rand('foo')
76
- end
77
- end # transmit_to_rand
78
-
79
- describe 'invalid method' do
80
- before do
81
- @pool = stub
82
- @command = Beaneater::PoolCommand.new(@pool)
83
- end
84
-
85
- it 'raises no method error' do
86
- assert_raises(NoMethodError) { @command.foo('foo') }
87
- end
88
- end # transmit_to_rand
89
- end
90
- end # Beaneater::PoolCommand