stalk_climber 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +1 -0
- data/lib/stalk_climber/climber.rb +5 -3
- data/lib/stalk_climber/connection.rb +25 -12
- data/lib/stalk_climber/connection_pool.rb +4 -3
- data/lib/stalk_climber/job.rb +4 -2
- data/lib/stalk_climber/version.rb +1 -1
- data/test/unit/climber_test.rb +9 -6
- data/test/unit/connection_pool_test.rb +19 -12
- data/test/unit/connection_test.rb +16 -0
- data/test/unit/job_test.rb +5 -4
- metadata +2 -2
data/README.md
CHANGED
@@ -1,4 +1,5 @@
|
|
1
1
|
# StalkClimber
|
2
|
+
[![Gem Version](https://badge.fury.io/rb/stalk_climber.png)](http://badge.fury.io/rb/stalk_climber)
|
2
3
|
[![Build Status](https://secure.travis-ci.org/freewrite/stalk_climber.png)](http://travis-ci.org/freewrite/stalk_climber)
|
3
4
|
[![Dependency Status](https://gemnasium.com/freewrite/stalk_climber.png)](https://gemnasium.com/freewrite/stalk_climber)
|
4
5
|
[![Coverage Status](https://coveralls.io/repos/freewrite/stalk_climber/badge.png?branch=master)](https://coveralls.io/r/freewrite/stalk_climber)
|
@@ -1,6 +1,6 @@
|
|
1
1
|
module StalkClimber
|
2
2
|
class Climber
|
3
|
-
include LazyEnumerable
|
3
|
+
include RUBY_VERSION >= '2.0.0' ? LazyEnumerable : Enumerable
|
4
4
|
|
5
5
|
attr_accessor :beanstalk_addresses, :test_tube
|
6
6
|
attr_reader :cache
|
@@ -11,7 +11,7 @@ module StalkClimber
|
|
11
11
|
if self.beanstalk_addresses.nil?
|
12
12
|
raise RuntimeError, 'beanstalk_addresses must be set in order to establish a connection'
|
13
13
|
end
|
14
|
-
@connection_pool = ConnectionPool.new(self.beanstalk_addresses)
|
14
|
+
@connection_pool = ConnectionPool.new(self.beanstalk_addresses, self.test_tube)
|
15
15
|
end
|
16
16
|
|
17
17
|
|
@@ -30,7 +30,9 @@ module StalkClimber
|
|
30
30
|
|
31
31
|
# Creates a new Climber instance, optionally yielding the instance
|
32
32
|
# if a block is given
|
33
|
-
def initialize
|
33
|
+
def initialize(beanstalk_addresses = nil, test_tube = nil)
|
34
|
+
self.beanstalk_addresses = beanstalk_addresses
|
35
|
+
self.test_tube = test_tube
|
34
36
|
yield(self) if block_given?
|
35
37
|
end
|
36
38
|
|
@@ -5,8 +5,7 @@ module StalkClimber
|
|
5
5
|
DEFAULT_TUBE = 'stalk_climber'
|
6
6
|
PROBE_TRANSMISSION = "put 4294967295 0 300 2\r\n{}"
|
7
7
|
|
8
|
-
|
9
|
-
attr_reader :max_climbed_job_id, :min_climbed_job_id
|
8
|
+
attr_reader :max_climbed_job_id, :min_climbed_job_id, :test_tube
|
10
9
|
|
11
10
|
|
12
11
|
# Returns or creates a Hash used for caching jobs by ID
|
@@ -40,18 +39,12 @@ module StalkClimber
|
|
40
39
|
# all transmissions.
|
41
40
|
# Optionally yields the instance if a block is given. The instance is yielded
|
42
41
|
# prior to test_tube configuration to allow the test_tube to be configured.
|
43
|
-
def initialize(address)
|
44
|
-
super
|
45
|
-
|
42
|
+
def initialize(address, test_tube = DEFAULT_TUBE)
|
43
|
+
super(address)
|
44
|
+
@test_tube = test_tube || DEFAULT_TUBE
|
46
45
|
clear_cache
|
47
46
|
yield(self) if block_given?
|
48
|
-
|
49
|
-
"use #{self.test_tube}",
|
50
|
-
"watch #{self.test_tube}",
|
51
|
-
'ignore default',
|
52
|
-
].each do |transmission|
|
53
|
-
transmit(transmission)
|
54
|
-
end
|
47
|
+
use_test_tube
|
55
48
|
end
|
56
49
|
|
57
50
|
|
@@ -68,6 +61,13 @@ module StalkClimber
|
|
68
61
|
end
|
69
62
|
|
70
63
|
|
64
|
+
# Set and use the provided +test_tube+
|
65
|
+
def test_tube=(test_tube)
|
66
|
+
@test_tube = test_tube
|
67
|
+
use_test_tube
|
68
|
+
end
|
69
|
+
|
70
|
+
|
71
71
|
# Safe form of with_job!, yields a Job instance to +block+ for the specified +job_id+.
|
72
72
|
# If the job does not exist, the error is caught and nil is passed to +block+ instead.
|
73
73
|
def with_job(job_id, &block)
|
@@ -139,5 +139,18 @@ module StalkClimber
|
|
139
139
|
end
|
140
140
|
end
|
141
141
|
|
142
|
+
|
143
|
+
# Dispatch transmissions notifying Beanstalk to use the configured test_tube for all
|
144
|
+
# commands from this connection and to ignore the default tube
|
145
|
+
def use_test_tube
|
146
|
+
[
|
147
|
+
"use #{self.test_tube}",
|
148
|
+
"watch #{self.test_tube}",
|
149
|
+
'ignore default',
|
150
|
+
].each do |transmission|
|
151
|
+
transmit(transmission)
|
152
|
+
end
|
153
|
+
end
|
154
|
+
|
142
155
|
end
|
143
156
|
end
|
@@ -3,13 +3,14 @@ module StalkClimber
|
|
3
3
|
|
4
4
|
class InvalidURIScheme < RuntimeError; end
|
5
5
|
|
6
|
-
attr_reader :addresses
|
6
|
+
attr_reader :addresses, :test_tube
|
7
7
|
|
8
8
|
# Constructs a Beaneater::Pool from a less strict URL
|
9
9
|
# +url+ can be a string i.e 'localhost:11300' or an array of addresses.
|
10
|
-
def initialize(addresses = nil)
|
10
|
+
def initialize(addresses = nil, test_tube = nil)
|
11
11
|
@addresses = Array(parse_addresses(addresses) || host_from_env || Beaneater.configuration.beanstalkd_url)
|
12
|
-
@
|
12
|
+
@test_tube = test_tube
|
13
|
+
@connections = @addresses.map { |address| Connection.new(address, test_tube) }
|
13
14
|
end
|
14
15
|
|
15
16
|
|
data/lib/stalk_climber/job.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
require 'json'
|
2
|
+
|
1
3
|
module StalkClimber
|
2
4
|
class Job
|
3
5
|
|
@@ -13,7 +15,7 @@ module StalkClimber
|
|
13
15
|
|
14
16
|
# Returns or fetches the body of the job obtained via the peek command
|
15
17
|
def body
|
16
|
-
return @body ||= connection.transmit("peek #{id}")[:body]
|
18
|
+
return @body ||= JSON.parse(connection.transmit("peek #{id}")[:body])
|
17
19
|
end
|
18
20
|
|
19
21
|
|
@@ -74,7 +76,7 @@ module StalkClimber
|
|
74
76
|
@body = @stats = nil
|
75
77
|
when 'FOUND' # peek
|
76
78
|
@id = job_data[:id].to_i
|
77
|
-
@body = job_data[:body]
|
79
|
+
@body = JSON.parse(job_data[:body])
|
78
80
|
@stats = nil
|
79
81
|
when 'OK' # stats-job
|
80
82
|
@body = nil
|
data/test/unit/climber_test.rb
CHANGED
@@ -3,9 +3,7 @@ require 'test_helper'
|
|
3
3
|
class ClimberTest < Test::Unit::TestCase
|
4
4
|
|
5
5
|
def test_climb_caches_jobs_for_later_use
|
6
|
-
climber = StalkClimber::Climber.new
|
7
|
-
c.beanstalk_addresses = BEANSTALK_ADDRESSES
|
8
|
-
end
|
6
|
+
climber = StalkClimber::Climber.new(BEANSTALK_ADDRESSES)
|
9
7
|
|
10
8
|
test_jobs = {}
|
11
9
|
climber.connection_pool.connections.each do |connection|
|
@@ -33,9 +31,7 @@ class ClimberTest < Test::Unit::TestCase
|
|
33
31
|
|
34
32
|
|
35
33
|
def test_connection_pool_creates_a_connection_pool
|
36
|
-
climber = StalkClimber::Climber.new
|
37
|
-
c.beanstalk_addresses = 'beanstalk://localhost'
|
38
|
-
end
|
34
|
+
climber = StalkClimber::Climber.new('beanstalk://localhost')
|
39
35
|
assert_kind_of StalkClimber::ConnectionPool, climber.connection_pool
|
40
36
|
end
|
41
37
|
|
@@ -56,4 +52,11 @@ class ClimberTest < Test::Unit::TestCase
|
|
56
52
|
)
|
57
53
|
end
|
58
54
|
|
55
|
+
|
56
|
+
def test_with_a_test_tube
|
57
|
+
climber = StalkClimber::Climber.new(BEANSTALK_ADDRESSES, 'test_tube')
|
58
|
+
assert_equal 'test_tube', climber.test_tube
|
59
|
+
assert_equal 'test_tube', climber.connection_pool.test_tube
|
60
|
+
end
|
61
|
+
|
59
62
|
end
|
@@ -13,37 +13,44 @@ class ConnectionPoolTest < Test::Unit::TestCase
|
|
13
13
|
|
14
14
|
|
15
15
|
def test_for_delegated_methods_it_should_delegate_methods_to_beanstalk_connection
|
16
|
-
|
17
|
-
assert_equal 'localhost',
|
16
|
+
connection_pool = StalkClimber::ConnectionPool.new('beanstalk://localhost')
|
17
|
+
assert_equal 'localhost', connection_pool.connections.first.host
|
18
18
|
end
|
19
19
|
|
20
20
|
|
21
21
|
def test_with_multiple_urls_it_should_support_array_of_connections
|
22
|
-
|
23
|
-
connections =
|
24
|
-
assert_equal 2,
|
22
|
+
connection_pool = StalkClimber::ConnectionPool.new(['beanstalk://localhost:11300','beanstalk://localhost'])
|
23
|
+
connections = connection_pool.connections
|
24
|
+
assert_equal 2, connection_pool.connections.size
|
25
25
|
assert_equal ['localhost:11300','localhost:11300'], connections.map(&:address)
|
26
26
|
end
|
27
27
|
|
28
28
|
|
29
29
|
def test_with_multiple_urls_it_should_support_single_string_with_commas
|
30
|
-
|
31
|
-
connections =
|
30
|
+
connection_pool = StalkClimber::ConnectionPool.new('beanstalk://localhost:11300,beanstalk://localhost')
|
31
|
+
connections = connection_pool.connections
|
32
32
|
assert_equal 2, connections.size
|
33
33
|
assert_equal ['localhost:11300','localhost:11300'], connections.map(&:address)
|
34
34
|
end
|
35
35
|
|
36
36
|
|
37
37
|
def test_with_single_url_it_should_set_up_connection_pool
|
38
|
-
|
39
|
-
assert_kind_of StalkClimber::ConnectionPool,
|
40
|
-
assert_kind_of Beaneater::Pool,
|
38
|
+
connection_pool = StalkClimber::ConnectionPool.new('beanstalk://localhost')
|
39
|
+
assert_kind_of StalkClimber::ConnectionPool, connection_pool
|
40
|
+
assert_kind_of Beaneater::Pool, connection_pool
|
41
41
|
end
|
42
42
|
|
43
43
|
|
44
44
|
def test_with_a_single_url_it_should_convert_url_to_address_array
|
45
|
-
|
46
|
-
assert_equal ['localhost:11300'],
|
45
|
+
connection_pool = StalkClimber::ConnectionPool.new('beanstalk://localhost')
|
46
|
+
assert_equal ['localhost:11300'], connection_pool.addresses
|
47
|
+
end
|
48
|
+
|
49
|
+
|
50
|
+
def test_with_a_test_tube
|
51
|
+
connection_pool = StalkClimber::ConnectionPool.new('beanstalk://localhost', 'test_tube')
|
52
|
+
assert_equal 'test_tube', connection_pool.test_tube
|
53
|
+
assert connection_pool.connections.all? { |connection| connection.test_tube == 'test_tube' }
|
47
54
|
end
|
48
55
|
|
49
56
|
end
|
@@ -157,6 +157,13 @@ class ConnectionTest < Test::Unit::TestCase
|
|
157
157
|
end
|
158
158
|
|
159
159
|
|
160
|
+
def test_initialize_with_a_test_tube
|
161
|
+
StalkClimber::Connection.any_instance.expects(:use_test_tube)
|
162
|
+
connection = StalkClimber::Connection.new('localhost:11300', 'test_tube')
|
163
|
+
assert_equal 'test_tube', connection.test_tube
|
164
|
+
end
|
165
|
+
|
166
|
+
|
160
167
|
def test_test_tube_is_initialized_but_configurable
|
161
168
|
assert_equal StalkClimber::Connection::DEFAULT_TUBE, @connection.test_tube
|
162
169
|
tube_name = 'test_tube'
|
@@ -165,6 +172,15 @@ class ConnectionTest < Test::Unit::TestCase
|
|
165
172
|
end
|
166
173
|
|
167
174
|
|
175
|
+
def test_setting_test_tube_uses_test_tube
|
176
|
+
@connection.expects(:transmit).with('use test_tube')
|
177
|
+
@connection.expects(:transmit).with('watch test_tube')
|
178
|
+
@connection.expects(:transmit).with('ignore default')
|
179
|
+
@connection.test_tube = 'test_tube'
|
180
|
+
assert_equal 'test_tube', @connection.test_tube
|
181
|
+
end
|
182
|
+
|
183
|
+
|
168
184
|
def test_with_job_yields_nil_or_the_requested_job_if_it_exists
|
169
185
|
assert_nothing_raised do
|
170
186
|
@connection.with_job(@connection.max_job_id) do |job|
|
data/test/unit/job_test.rb
CHANGED
@@ -1,4 +1,5 @@
|
|
1
1
|
require 'test_helper'
|
2
|
+
require 'json'
|
2
3
|
|
3
4
|
class Job < Test::Unit::TestCase
|
4
5
|
|
@@ -8,11 +9,11 @@ class Job < Test::Unit::TestCase
|
|
8
9
|
end
|
9
10
|
|
10
11
|
|
11
|
-
def
|
12
|
-
body = {
|
12
|
+
def test_body_retrives_performs_peek_and_parses_json
|
13
|
+
body = {'test' => true}
|
13
14
|
|
14
15
|
@job.connection.expects(:transmit).returns({
|
15
|
-
:body => body,
|
16
|
+
:body => body.to_json,
|
16
17
|
})
|
17
18
|
assert_equal body, @job.body
|
18
19
|
|
@@ -52,7 +53,7 @@ class Job < Test::Unit::TestCase
|
|
52
53
|
assert_equal @connection, job.connection
|
53
54
|
assert_equal @job.id, job.id
|
54
55
|
assert_equal 'FOUND', job.instance_variable_get(:@status)
|
55
|
-
assert_equal
|
56
|
+
assert_equal({}, job.body)
|
56
57
|
refute job.instance_variable_get(:@stats)
|
57
58
|
end
|
58
59
|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: stalk_climber
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-09-
|
12
|
+
date: 2013-09-04 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: bundler
|