beaneater 1.1.4 → 1.3.0
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 +4 -4
- data/.github/workflows/ruby.yml +7 -3
- data/CHANGELOG.md +28 -0
- data/Gemfile +1 -1
- data/README.md +34 -1
- data/Rakefile +55 -0
- data/beaneater.gemspec +8 -1
- data/lib/beaneater/arguments.rb +66 -0
- data/lib/beaneater/configuration.rb +8 -0
- data/lib/beaneater/connection.rb +322 -61
- data/lib/beaneater/errors.rb +14 -0
- data/lib/beaneater/job/collection.rb +45 -7
- data/lib/beaneater/job/record.rb +17 -8
- data/lib/beaneater/stats/fast_struct.rb +7 -0
- data/lib/beaneater/stats/stat_struct.rb +8 -2
- data/lib/beaneater/stats.rb +6 -0
- data/lib/beaneater/tube/collection.rb +8 -6
- data/lib/beaneater/tube/record.rb +6 -5
- data/lib/beaneater/version.rb +1 -1
- data/lib/beaneater.rb +1 -1
- data/test/arguments_test.rb +174 -0
- data/test/connection_test.rb +628 -16
- data/test/job_test.rb +36 -0
- data/test/jobs_test.rb +440 -0
- data/test/stat_struct_test.rb +30 -0
- data/test/stats_test.rb +16 -0
- data/test/test_helper.rb +45 -0
- data/test/tube_test.rb +123 -0
- data/test/tubes_test.rb +30 -1
- metadata +12 -6
- data/.travis.yml +0 -15
|
@@ -19,13 +19,19 @@ class Beaneater
|
|
|
19
19
|
|
|
20
20
|
# Access value for stat with specified key.
|
|
21
21
|
#
|
|
22
|
+
# Keys are stored underscored (see {.from_hash}), so hyphenated beanstalkd
|
|
23
|
+
# names like "current-jobs-ready" are underscored before lookup. The value
|
|
24
|
+
# is read from the stats themselves, as the readers do, so any key works.
|
|
25
|
+
#
|
|
22
26
|
# @param [String] key Key to fetch from stats.
|
|
23
|
-
# @return [String, Integer] Value for specified stat key.
|
|
27
|
+
# @return [String, Integer, nil] Value for specified stat key, nil if there is none.
|
|
24
28
|
# @example
|
|
25
29
|
# @stats['foo'] # => "bar"
|
|
30
|
+
# @stats['current-jobs-ready'] # => 5
|
|
26
31
|
#
|
|
27
32
|
def [](key)
|
|
28
|
-
|
|
33
|
+
name = key.to_s.gsub(/-/, '_')
|
|
34
|
+
@hash.fetch(name) { @hash[name.to_sym] }
|
|
29
35
|
end
|
|
30
36
|
|
|
31
37
|
# Returns set of keys within this struct
|
data/lib/beaneater/stats.rb
CHANGED
|
@@ -65,6 +65,12 @@ class Beaneater
|
|
|
65
65
|
end
|
|
66
66
|
end
|
|
67
67
|
|
|
68
|
+
# Answers true for the stats keys method_missing looks up.
|
|
69
|
+
# @api public
|
|
70
|
+
def respond_to_missing?(name, include_private = false)
|
|
71
|
+
data.keys.include?(name.to_s) || super
|
|
72
|
+
end
|
|
73
|
+
|
|
68
74
|
protected
|
|
69
75
|
|
|
70
76
|
# Returns struct based on stats data from response.
|
|
@@ -63,7 +63,7 @@ class Beaneater
|
|
|
63
63
|
# @api public
|
|
64
64
|
def reserve(timeout=nil, &block)
|
|
65
65
|
res = transmit(
|
|
66
|
-
timeout ? "reserve-with-timeout #{timeout}" : 'reserve')
|
|
66
|
+
timeout ? "reserve-with-timeout #{Arguments.number(timeout, 'timeout')}" : 'reserve')
|
|
67
67
|
job = Job.new(client, res)
|
|
68
68
|
block.call(job) if block_given?
|
|
69
69
|
job
|
|
@@ -132,7 +132,7 @@ class Beaneater
|
|
|
132
132
|
#
|
|
133
133
|
# @api public
|
|
134
134
|
def watch(*names)
|
|
135
|
-
names.each do |t|
|
|
135
|
+
names.map { |t| Arguments.tube_name(t) }.each do |t|
|
|
136
136
|
transmit "watch #{t}"
|
|
137
137
|
client.connection.add_to_watched(t)
|
|
138
138
|
end
|
|
@@ -162,7 +162,7 @@ class Beaneater
|
|
|
162
162
|
#
|
|
163
163
|
# @api public
|
|
164
164
|
def ignore(*names)
|
|
165
|
-
names.each do |w|
|
|
165
|
+
names.map { |w| Arguments.tube_name(w) }.each do |w|
|
|
166
166
|
transmit "ignore #{w}"
|
|
167
167
|
client.connection.remove_from_watched(w)
|
|
168
168
|
end
|
|
@@ -175,9 +175,11 @@ class Beaneater
|
|
|
175
175
|
# @conn.tubes.use("some-tube")
|
|
176
176
|
#
|
|
177
177
|
def use(tube)
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
178
|
+
name = Arguments.tube_name(tube)
|
|
179
|
+
return tube if last_used == name
|
|
180
|
+
transmit("use #{name}")
|
|
181
|
+
self.last_used = name
|
|
182
|
+
tube
|
|
181
183
|
rescue BadFormatError
|
|
182
184
|
raise InvalidTubeName, "Tube cannot be named '#{tube}'"
|
|
183
185
|
end
|
|
@@ -52,7 +52,8 @@ class Beaneater
|
|
|
52
52
|
:ttr => config.default_put_ttr
|
|
53
53
|
}.merge(options)
|
|
54
54
|
|
|
55
|
-
|
|
55
|
+
pri, delay, ttr = %i(pri delay ttr).map { |key| Arguments.unsigned(options[key], key.to_s) }
|
|
56
|
+
cmd_options = "#{pri} #{delay} #{ttr} #{serialized_body.bytesize}"
|
|
56
57
|
transmit("put #{cmd_options}\r\n#{serialized_body}")
|
|
57
58
|
end
|
|
58
59
|
end
|
|
@@ -67,7 +68,7 @@ class Beaneater
|
|
|
67
68
|
# @api public
|
|
68
69
|
def peek(state)
|
|
69
70
|
safe_use do
|
|
70
|
-
res = transmit("peek-#{state}")
|
|
71
|
+
res = transmit("peek-#{Arguments.peek_state(state)}")
|
|
71
72
|
Job.new(client, res)
|
|
72
73
|
end
|
|
73
74
|
rescue Beaneater::NotFoundError
|
|
@@ -99,7 +100,7 @@ class Beaneater
|
|
|
99
100
|
#
|
|
100
101
|
# @api public
|
|
101
102
|
def kick(bounds=1)
|
|
102
|
-
safe_use { transmit("kick #{bounds}") }
|
|
103
|
+
safe_use { transmit("kick #{Arguments.number(bounds, 'bounds')}") }
|
|
103
104
|
end
|
|
104
105
|
|
|
105
106
|
# Returns related stats for this tube.
|
|
@@ -110,7 +111,7 @@ class Beaneater
|
|
|
110
111
|
#
|
|
111
112
|
# @api public
|
|
112
113
|
def stats
|
|
113
|
-
res = transmit("stats-tube #{name}")
|
|
114
|
+
res = transmit("stats-tube #{Arguments.tube_name(name)}")
|
|
114
115
|
StatStruct.from_hash(res[:body])
|
|
115
116
|
end
|
|
116
117
|
|
|
@@ -123,7 +124,7 @@ class Beaneater
|
|
|
123
124
|
#
|
|
124
125
|
# @api public
|
|
125
126
|
def pause(delay)
|
|
126
|
-
transmit("pause-tube #{name} #{delay}")
|
|
127
|
+
transmit("pause-tube #{Arguments.tube_name(name)} #{Arguments.unsigned(delay, 'delay')}")
|
|
127
128
|
end
|
|
128
129
|
|
|
129
130
|
# Clears all unreserved jobs in all states from the tube
|
data/lib/beaneater/version.rb
CHANGED
data/lib/beaneater.rb
CHANGED
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# test/arguments_test.rb
|
|
4
|
+
|
|
5
|
+
require File.expand_path('../test_helper', __FILE__)
|
|
6
|
+
|
|
7
|
+
describe "Command arguments" do
|
|
8
|
+
before do
|
|
9
|
+
@beanstalk = Beaneater.new('localhost')
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
# The connection's next command still gets its own reply
|
|
13
|
+
def assert_in_step
|
|
14
|
+
assert_equal 'USING', @beanstalk.connection.transmit('list-tube-used')[:status]
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
describe "tube names" do
|
|
18
|
+
it "should raise InvalidTubeName for a name beanstalkd does not accept" do
|
|
19
|
+
calls = {
|
|
20
|
+
'use' => ->(name) { @beanstalk.tubes.use(name) },
|
|
21
|
+
'watch' => ->(name) { @beanstalk.tubes.watch(name) },
|
|
22
|
+
'ignore' => ->(name) { @beanstalk.tubes.ignore(name) },
|
|
23
|
+
'put' => ->(name) { @beanstalk.tubes.find(name).put('x') },
|
|
24
|
+
'peek' => ->(name) { @beanstalk.tubes.find(name).peek(:ready) },
|
|
25
|
+
'stats' => ->(name) { @beanstalk.tubes.find(name).stats },
|
|
26
|
+
'pause' => ->(name) { @beanstalk.tubes.find(name).pause(0) }
|
|
27
|
+
}
|
|
28
|
+
["a\r\nb", "a\nb", "a\rb", "a b", "-a", "a" * 201, "", "tubeé", "a\xffb", "a\xffb".b].each do |name|
|
|
29
|
+
calls.each do |call, block|
|
|
30
|
+
assert_raises(Beaneater::InvalidTubeName, "#{call} #{name.inspect}") { block.call(name) }
|
|
31
|
+
assert_in_step
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
assert_equal ['default'], @beanstalk.tubes.watched.map(&:name)
|
|
35
|
+
assert_equal 'default', @beanstalk.tubes.used.name
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
it "should raise InvalidTubeName when a reconnect would restore a name beanstalkd does not accept" do
|
|
39
|
+
@beanstalk.connection.tubes_watched = ['default', "a\r\nb"]
|
|
40
|
+
@beanstalk.connection.connection.shutdown(Socket::SHUT_RDWR) # the next command reconnects
|
|
41
|
+
assert_raises(Beaneater::InvalidTubeName) { @beanstalk.tubes.used }
|
|
42
|
+
@beanstalk.connection.tubes_watched = ['default']
|
|
43
|
+
assert_equal 'default', @beanstalk.tubes.used.name
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
it "should accept every name beanstalkd does" do
|
|
47
|
+
['demo.test.foo', 'jobs(eu).mailers', 'MyApp.mailers', 'a+b/c;d$e_f-g', 'a' * 200, :symbol_tube].each do |name|
|
|
48
|
+
tube = @beanstalk.tubes.find(name)
|
|
49
|
+
tube.put('job')
|
|
50
|
+
assert_equal name.to_s, tube.stats.name
|
|
51
|
+
tube.pause(0)
|
|
52
|
+
@beanstalk.tubes.watch(name)
|
|
53
|
+
assert_includes @beanstalk.connection.tubes_watched, name.to_s
|
|
54
|
+
assert_equal 'job', tube.peek(:ready).body
|
|
55
|
+
@beanstalk.tubes.ignore(name)
|
|
56
|
+
end
|
|
57
|
+
end
|
|
58
|
+
end # tube names
|
|
59
|
+
|
|
60
|
+
describe "job ids" do
|
|
61
|
+
before do
|
|
62
|
+
@tube = @beanstalk.tubes.find('arguments')
|
|
63
|
+
@id = @tube.put('job')[:id]
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
it "should find a job by an Integer or a String of digits" do
|
|
67
|
+
assert_equal 'job', @beanstalk.jobs.find(@id.to_i).body
|
|
68
|
+
assert_equal 'job', @beanstalk.jobs.find(@id.to_s).body
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
it "should raise ArgumentError for an id that is not a whole number" do
|
|
72
|
+
["#{@id}\r\nstats", "#{@id}\nstats", "#{@id} 1", "abc", "", nil, 1.5, :"1"].each do |id|
|
|
73
|
+
assert_raises(ArgumentError, id.inspect) { @beanstalk.jobs.find(id) }
|
|
74
|
+
assert_in_step
|
|
75
|
+
job = Beaneater::Job.new(@beanstalk, :id => id, :status => 'RESERVED')
|
|
76
|
+
[:delete, :touch, :kick, :stats, :bury, :release].each do |call|
|
|
77
|
+
assert_raises(ArgumentError, "#{call} #{id.inspect}") { job.send(call) }
|
|
78
|
+
assert_in_step
|
|
79
|
+
end
|
|
80
|
+
end
|
|
81
|
+
assert_equal 'job', @beanstalk.jobs.find(@id).body
|
|
82
|
+
end
|
|
83
|
+
end # job ids
|
|
84
|
+
|
|
85
|
+
describe "numbers" do
|
|
86
|
+
before do
|
|
87
|
+
@tube = @beanstalk.tubes.find('arguments')
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
it "should raise ArgumentError for a number that is not one" do
|
|
91
|
+
["1\r\nstats", "1\n", "1 2", "x", nil].each do |bad|
|
|
92
|
+
[:pri, :delay, :ttr].each do |key|
|
|
93
|
+
assert_raises(ArgumentError, "put #{key} #{bad.inspect}") { @tube.put('job', key => bad) }
|
|
94
|
+
assert_in_step
|
|
95
|
+
end
|
|
96
|
+
assert_raises(ArgumentError, "pause #{bad.inspect}") { @tube.pause(bad) }
|
|
97
|
+
assert_in_step
|
|
98
|
+
assert_raises(ArgumentError, "kick #{bad.inspect}") { @tube.kick(bad) }
|
|
99
|
+
assert_in_step
|
|
100
|
+
assert_raises(ArgumentError, "reserve #{bad.inspect}") { @beanstalk.tubes.reserve(bad) } if bad
|
|
101
|
+
assert_in_step
|
|
102
|
+
|
|
103
|
+
@tube.put('job')
|
|
104
|
+
job = @tube.reserve(0)
|
|
105
|
+
assert_raises(ArgumentError, "release pri #{bad.inspect}") { job.release(:pri => bad, :delay => 0) }
|
|
106
|
+
assert_in_step
|
|
107
|
+
assert_raises(ArgumentError, "release delay #{bad.inspect}") { job.release(:pri => 1, :delay => bad) }
|
|
108
|
+
assert_in_step
|
|
109
|
+
assert_raises(ArgumentError, "bury pri #{bad.inspect}") { job.bury(:pri => bad) }
|
|
110
|
+
assert_in_step
|
|
111
|
+
job.delete
|
|
112
|
+
end
|
|
113
|
+
assert_nil @tube.peek(:ready)
|
|
114
|
+
end
|
|
115
|
+
|
|
116
|
+
it "should raise ArgumentError for a priority, delay or TTR outside 0 to 4294967295" do
|
|
117
|
+
[-1, "-1", 2**32, "4294967296"].each do |bad|
|
|
118
|
+
[:pri, :delay, :ttr].each do |key|
|
|
119
|
+
assert_raises(ArgumentError, "put #{key} #{bad.inspect}") { @tube.put('stats', key => bad) }
|
|
120
|
+
assert_in_step
|
|
121
|
+
end
|
|
122
|
+
assert_raises(ArgumentError, "pause #{bad.inspect}") { @tube.pause(bad) }
|
|
123
|
+
assert_in_step
|
|
124
|
+
|
|
125
|
+
@tube.put('job')
|
|
126
|
+
job = @tube.reserve(0)
|
|
127
|
+
assert_raises(ArgumentError, "release pri #{bad.inspect}") { job.release(:pri => bad, :delay => 0) }
|
|
128
|
+
assert_in_step
|
|
129
|
+
assert_raises(ArgumentError, "release delay #{bad.inspect}") { job.release(:pri => 1, :delay => bad) }
|
|
130
|
+
assert_in_step
|
|
131
|
+
assert_raises(ArgumentError, "bury pri #{bad.inspect}") { job.bury(:pri => bad) }
|
|
132
|
+
assert_in_step
|
|
133
|
+
job.delete
|
|
134
|
+
end
|
|
135
|
+
assert_nil @tube.peek(:ready)
|
|
136
|
+
end
|
|
137
|
+
|
|
138
|
+
it "should accept the largest priority beanstalkd takes" do
|
|
139
|
+
[4294967295, "4294967295"].each do |pri|
|
|
140
|
+
@tube.put('job', :pri => pri)
|
|
141
|
+
job = @tube.reserve(0)
|
|
142
|
+
assert_equal 4294967295, job.stats.pri
|
|
143
|
+
job.release(:pri => pri, :delay => 0)
|
|
144
|
+
job = @tube.reserve(0)
|
|
145
|
+
job.bury(:pri => pri)
|
|
146
|
+
assert_equal 4294967295, job.stats.pri
|
|
147
|
+
job.delete
|
|
148
|
+
end
|
|
149
|
+
end
|
|
150
|
+
|
|
151
|
+
it "should accept Integers and Strings of digits, and Floats where beanstalkd does" do
|
|
152
|
+
@tube.put('job', :pri => '5', :delay => '0', :ttr => '60')
|
|
153
|
+
job = @tube.reserve(0.5)
|
|
154
|
+
assert_equal 5, job.stats.pri
|
|
155
|
+
job.release(:pri => '6', :delay => '0')
|
|
156
|
+
job = @beanstalk.tubes.reserve('0')
|
|
157
|
+
job.bury(:pri => 7)
|
|
158
|
+
assert_equal 1, @tube.kick(1.5)[:id].to_i
|
|
159
|
+
@tube.pause('0')
|
|
160
|
+
end
|
|
161
|
+
end # numbers
|
|
162
|
+
|
|
163
|
+
describe "peek states" do
|
|
164
|
+
it "should raise ArgumentError for a state other than ready, delayed or buried" do
|
|
165
|
+
tube = @beanstalk.tubes.find('arguments')
|
|
166
|
+
tube.put('job')
|
|
167
|
+
["ready\r\nstats", "ready ", "reserved", ""].each do |state|
|
|
168
|
+
assert_raises(ArgumentError, state.inspect) { tube.peek(state) }
|
|
169
|
+
assert_in_step
|
|
170
|
+
end
|
|
171
|
+
assert_equal 'job', tube.peek('ready').body
|
|
172
|
+
end
|
|
173
|
+
end # peek states
|
|
174
|
+
end
|