stalking 0.0.9 → 0.1.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.
- data/README.md +1 -1
- data/lib/stalking/producer.rb +20 -33
- data/lib/stalking/version.rb +1 -1
- data/test/stalking/producer_test.rb +2 -14
- metadata +2 -2
data/README.md
CHANGED
|
@@ -154,7 +154,7 @@ when a single server is down, of course require some time for the error
|
|
|
154
154
|
detection and processing. This usually should not be a big issue, except there
|
|
155
155
|
is some temporary lag between the producer and the beanstalkd server. The
|
|
156
156
|
producer then has to wait for a timeout and while the producer is waiting for
|
|
157
|
-
the timeout it will block your application. The default timeout is
|
|
157
|
+
the timeout it will block your application. The default timeout is 500
|
|
158
158
|
milliseconds, but you can change the timeout via:
|
|
159
159
|
|
|
160
160
|
```ruby
|
data/lib/stalking/producer.rb
CHANGED
|
@@ -2,17 +2,13 @@
|
|
|
2
2
|
module Stalking
|
|
3
3
|
class Producer
|
|
4
4
|
def initialize(options = {}, &block)
|
|
5
|
-
@timeout = options[:timeout] || 0.
|
|
6
|
-
|
|
5
|
+
@timeout = options[:timeout] || 0.5
|
|
7
6
|
@pri = options[:pri] || 65536
|
|
8
7
|
@delay = options[:delay] || 0
|
|
9
8
|
@ttr = options[:ttr] || 120
|
|
10
|
-
|
|
11
9
|
@logger = options[:logger]
|
|
12
|
-
|
|
13
10
|
@servers = options[:servers] || ["localhost:11300"]
|
|
14
|
-
|
|
15
|
-
@tries = options[:tries] || @servers.size
|
|
11
|
+
@tries = options[:tries] || [@servers.size, 2].max
|
|
16
12
|
|
|
17
13
|
@connections = {}
|
|
18
14
|
end
|
|
@@ -22,33 +18,8 @@ module Stalking
|
|
|
22
18
|
|
|
23
19
|
# Send the job to a random server.
|
|
24
20
|
|
|
25
|
-
@servers.shuffle.first(@tries).each do |server|
|
|
26
|
-
|
|
27
|
-
Timeout::timeout @timeout do
|
|
28
|
-
@connections[server] ||= Beanstalk::Pool.new([server])
|
|
29
|
-
|
|
30
|
-
enq @connections[server], name, args, options
|
|
31
|
-
end
|
|
32
|
-
|
|
33
|
-
return true
|
|
34
|
-
rescue Beanstalk::NotConnected
|
|
35
|
-
# Connecting to the beanstalk server has failed.
|
|
36
|
-
# Let's try to reconnect and enqueue afterwards.
|
|
37
|
-
|
|
38
|
-
begin
|
|
39
|
-
Timeout::timeout @timeout do
|
|
40
|
-
@connections[server] = Beanstalk::Pool.new([server])
|
|
41
|
-
|
|
42
|
-
enq @connections[server], name, args, options
|
|
43
|
-
end
|
|
44
|
-
|
|
45
|
-
return true
|
|
46
|
-
rescue Beanstalk::NotConnected, Timeout::Error, StandardError => e
|
|
47
|
-
@logger.andand.error e
|
|
48
|
-
end
|
|
49
|
-
rescue Timeout::Error, StandardError => e
|
|
50
|
-
@logger.andand.error e
|
|
51
|
-
end
|
|
21
|
+
(@servers.shuffle * @tries).first(@tries).each do |server|
|
|
22
|
+
return true if enqueue_at(server, name, args, options)
|
|
52
23
|
end
|
|
53
24
|
|
|
54
25
|
@logger.andand.error "Enqueue #{name.inspect} with #{args.inspect} and #{options.inspect} failed"
|
|
@@ -61,6 +32,22 @@ module Stalking
|
|
|
61
32
|
|
|
62
33
|
private
|
|
63
34
|
|
|
35
|
+
def enqueue_at(server, name, args = {}, options = {})
|
|
36
|
+
Timeout::timeout @timeout do
|
|
37
|
+
@connections[server] ||= Beanstalk::Pool.new([server])
|
|
38
|
+
|
|
39
|
+
enq @connections[server], name, args, options
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
true
|
|
43
|
+
rescue Beanstalk::NotConnected, Timeout::Error, StandardError => e
|
|
44
|
+
@connections[server] = nil # Reset the connection.
|
|
45
|
+
|
|
46
|
+
@logger.andand.error e
|
|
47
|
+
|
|
48
|
+
false
|
|
49
|
+
end
|
|
50
|
+
|
|
64
51
|
def enq(connection, name, args = {}, options = {})
|
|
65
52
|
pri = options[:pri] || @pri
|
|
66
53
|
delay = options[:delay] || @delay
|
data/lib/stalking/version.rb
CHANGED
|
@@ -32,21 +32,11 @@ class Stalking::ProducerTest < Test::Unit::TestCase
|
|
|
32
32
|
srand 0
|
|
33
33
|
|
|
34
34
|
producer.enqueue "test", "up" => "true"
|
|
35
|
-
|
|
36
35
|
assert_equal ['["test",{"up":"true"}]', 65536, 0, 120], server1.queues["test"].last
|
|
37
36
|
|
|
38
|
-
srand 0
|
|
39
|
-
|
|
40
|
-
server1.disconnect!
|
|
41
|
-
|
|
42
|
-
producer.enqueue "test", "down" => "disconnected"
|
|
43
|
-
|
|
44
|
-
assert_equal ['["test",{"down":"disconnected"}]', 65536, 0, 120], server1.queues["test"].last
|
|
45
|
-
|
|
46
37
|
server1.down!
|
|
47
38
|
|
|
48
39
|
producer.enqueue "test", "down" => "permanent"
|
|
49
|
-
|
|
50
40
|
assert_equal ['["test",{"down":"permanent"}]', 65536, 0, 120], server2.queues["test"].last
|
|
51
41
|
end
|
|
52
42
|
|
|
@@ -59,7 +49,6 @@ class Stalking::ProducerTest < Test::Unit::TestCase
|
|
|
59
49
|
srand 0
|
|
60
50
|
|
|
61
51
|
producer.enqueue "test", "up" => "true"
|
|
62
|
-
|
|
63
52
|
assert_equal ['["test",{"up":"true"}]', 65536, 0, 120], server1.queues["test"].last
|
|
64
53
|
|
|
65
54
|
srand 0
|
|
@@ -67,20 +56,19 @@ class Stalking::ProducerTest < Test::Unit::TestCase
|
|
|
67
56
|
server1.delay = 5
|
|
68
57
|
|
|
69
58
|
producer.enqueue "test", "down" => "delayed"
|
|
70
|
-
|
|
71
59
|
assert_equal ['["test",{"down":"delayed"}]', 65536, 0, 120], server2.queues["test"].last
|
|
72
60
|
end
|
|
73
61
|
|
|
74
62
|
def test_logger
|
|
75
63
|
logger = TestLogger.new
|
|
76
64
|
|
|
77
|
-
producer = Stalking::Producer.new(:logger => logger)
|
|
65
|
+
producer = Stalking::Producer.new(:logger => logger, :tries => 2)
|
|
78
66
|
|
|
79
67
|
Beanstalk::Pool.new(["localhost:11300"]).down!
|
|
80
68
|
|
|
81
69
|
producer.enqueue "test", "up" => "true"
|
|
82
70
|
|
|
83
|
-
assert_equal ['Enqueue "test" with {"up"=>"true"} and {}', "Beanstalk::NotConnected", 'Enqueue "test" with {"up"=>"true"} and {} failed'], logger.entries.collect(&:to_s)
|
|
71
|
+
assert_equal ['Enqueue "test" with {"up"=>"true"} and {}', "Beanstalk::NotConnected", "Beanstalk::NotConnected", 'Enqueue "test" with {"up"=>"true"} and {} failed'], logger.entries.collect(&:to_s)
|
|
84
72
|
end
|
|
85
73
|
end
|
|
86
74
|
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: stalking
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.0
|
|
4
|
+
version: 0.1.0
|
|
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: 2014-
|
|
12
|
+
date: 2014-05-17 00:00:00.000000000 Z
|
|
13
13
|
dependencies:
|
|
14
14
|
- !ruby/object:Gem::Dependency
|
|
15
15
|
name: rake
|