reliable-msg-agent 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- data/History.txt +6 -0
- data/lib/reliable-msg/agent/agent.rb +2 -2
- data/lib/reliable-msg/agent/consumers.rb +53 -3
- data/lib/reliable-msg/agent/service.rb +57 -4
- data/lib/reliable-msg/agent/version.rb +1 -1
- data/resources/agent.conf +1 -1
- data/resources/examples/log-level-change-agent.conf +1 -1
- data/resources/examples/log-rotate-daily-agent.conf +1 -1
- data/resources/examples/log-rotate-size-agent.conf +1 -1
- data/spec/service_spec.rb +81 -18
- metadata +4 -4
data/History.txt
CHANGED
@@ -19,8 +19,8 @@ module ReliableMsg::Agent #:nodoc:
|
|
19
19
|
# +conf+ :: consumer configurations.
|
20
20
|
# +options+ :: the options (it is still unused.)
|
21
21
|
#
|
22
|
-
def call msg, options = {}
|
23
|
-
raise AgentError, "#call(msg,options={}) not implemented!"
|
22
|
+
def call msg, conf, options = {}
|
23
|
+
raise AgentError, "#call(msg,conf,options={}) not implemented!"
|
24
24
|
end
|
25
25
|
end
|
26
26
|
end
|
@@ -5,15 +5,35 @@ require "reliable-msg/agent"
|
|
5
5
|
require "monitor"
|
6
6
|
|
7
7
|
module ReliableMsg::Agent #:nodoc:
|
8
|
+
|
9
|
+
# Class of consumers for reliable-msg queue massaging.
|
10
|
+
#
|
11
|
+
# It has the function to make the agent acquire the message
|
12
|
+
# from ReliableMsg-Queue of the object and execute processing.
|
13
|
+
#
|
8
14
|
class Consumers
|
9
15
|
|
10
16
|
@@default_options = {
|
11
17
|
"source_uri" => "druby://localhost:6438",
|
12
|
-
"target" => "queue
|
18
|
+
"target" => "queue.agent",
|
13
19
|
"every" => 1.0,
|
14
20
|
"threads" => 1,
|
15
21
|
}.freeze
|
16
22
|
|
23
|
+
# Initialize.
|
24
|
+
#
|
25
|
+
# === Args
|
26
|
+
#
|
27
|
+
# +logger+ :: the logger.
|
28
|
+
# +options+ :: consumers options.
|
29
|
+
#
|
30
|
+
# valid options for +options+ are:
|
31
|
+
#
|
32
|
+
# +source_uri+ :: uri for source reliable-msg queue.
|
33
|
+
# +target+ :: target queue name for source reliable-msg queue.
|
34
|
+
# +every+ :: interval seconds when connection fails
|
35
|
+
# +threads+ :: times for consumer threads.
|
36
|
+
#
|
17
37
|
def initialize logger, options
|
18
38
|
@logger = logger
|
19
39
|
@options = options
|
@@ -22,6 +42,8 @@ module ReliableMsg::Agent #:nodoc:
|
|
22
42
|
@threads = nil
|
23
43
|
end
|
24
44
|
|
45
|
+
# Start consumers.
|
46
|
+
#
|
25
47
|
def start
|
26
48
|
@locker.synchronize {
|
27
49
|
raise AgentError, "workers already started." if alive?
|
@@ -38,6 +60,8 @@ module ReliableMsg::Agent #:nodoc:
|
|
38
60
|
}
|
39
61
|
end
|
40
62
|
|
63
|
+
# Stop consumers.
|
64
|
+
#
|
41
65
|
def stop
|
42
66
|
@locker.synchronize {
|
43
67
|
raise AgentError, "workers already stopped." unless alive?
|
@@ -51,10 +75,18 @@ module ReliableMsg::Agent #:nodoc:
|
|
51
75
|
}
|
52
76
|
end
|
53
77
|
|
78
|
+
# Return the state of alive or not alive.
|
79
|
+
#
|
54
80
|
def alive?; @locker.synchronize { !! @threads }; end
|
55
81
|
|
56
82
|
private
|
57
83
|
|
84
|
+
# Loop for consumer.
|
85
|
+
#
|
86
|
+
# === Args
|
87
|
+
#
|
88
|
+
# +conf+:: consumer configurations.
|
89
|
+
#
|
58
90
|
def consuming_loop conf
|
59
91
|
agent = Agent.new @logger
|
60
92
|
uri, every, target = conf["source_uri"], conf["every"], conf["target"]
|
@@ -78,12 +110,22 @@ module ReliableMsg::Agent #:nodoc:
|
|
78
110
|
end
|
79
111
|
end
|
80
112
|
|
113
|
+
# Test for connect to reliable-msg queue manager.
|
114
|
+
# Returns valid queue-name.
|
115
|
+
#
|
116
|
+
# === Args
|
117
|
+
#
|
118
|
+
# +qm+ :: the queue manager.
|
119
|
+
# +target+ :: target queue name for source reliable-msg queue.
|
120
|
+
# +uri+ :: uri for source reliable-msg queue.
|
121
|
+
# +every+ :: interval seconds when connection fails
|
122
|
+
#
|
81
123
|
def connect_qm qm, target, uri, every
|
82
124
|
error_raised = false
|
83
125
|
begin
|
84
|
-
|
126
|
+
raise "queue-manager is not alive." unless qm.alive?
|
85
127
|
@logger.warn { "Connect to #{uri} successfully at #{Time.now}" } if error_raised
|
86
|
-
return
|
128
|
+
return target
|
87
129
|
|
88
130
|
rescue => e
|
89
131
|
@logger.warn { "Lost connection to #{uri} at #{Time.now} - #{e.message}" } unless error_raised
|
@@ -94,6 +136,14 @@ module ReliableMsg::Agent #:nodoc:
|
|
94
136
|
end
|
95
137
|
end
|
96
138
|
|
139
|
+
# Fetch message from reliable-msg queue.
|
140
|
+
# Return evaluation result of yield.
|
141
|
+
#
|
142
|
+
# === Args
|
143
|
+
#
|
144
|
+
# +queue_name+ :: queue name for source reliable-msg queue.
|
145
|
+
# +source_uri+ :: uri for source reliable-msg queue.
|
146
|
+
#
|
97
147
|
def fetch queue_name, source_uri
|
98
148
|
ReliableMsg::Queue.new(queue_name, :drb_uri => source_uri).get { |m|
|
99
149
|
begin
|
@@ -5,40 +5,93 @@ require "reliable-msg/agent"
|
|
5
5
|
require "monitor"
|
6
6
|
|
7
7
|
module ReliableMsg::Agent #:nodoc:
|
8
|
+
|
9
|
+
# Class of ReliableMsg-Agent service
|
10
|
+
#
|
8
11
|
class Service
|
12
|
+
|
13
|
+
# Initialilze.
|
14
|
+
#
|
15
|
+
# === Args
|
16
|
+
#
|
17
|
+
# +logger+ :: the logger.
|
18
|
+
# +options+ :: service options.
|
19
|
+
#
|
9
20
|
def initialize logger, options = {}
|
10
21
|
@logger = logger
|
11
22
|
@options = options
|
12
23
|
|
13
|
-
@
|
24
|
+
@consumers = @@dependency_classes[:Consumers].new @logger, @options["consumers"]
|
14
25
|
@locker = Monitor.new
|
15
26
|
end
|
16
27
|
|
28
|
+
# Start service.
|
29
|
+
#
|
17
30
|
def start
|
18
31
|
@locker.synchronize {
|
19
32
|
raise AgentError, "service already started." if alive?
|
20
33
|
|
21
34
|
@logger.info { "*** reliable-msg agent service starting..." }
|
22
|
-
@
|
35
|
+
@consumers.start
|
23
36
|
@logger.info { "*** reliable-msg agent service started." }
|
24
37
|
}
|
25
38
|
end
|
26
39
|
|
40
|
+
# Stop service.
|
41
|
+
#
|
27
42
|
def stop
|
28
43
|
@locker.synchronize {
|
29
44
|
raise AgentError, "service already stopped." unless alive?
|
30
45
|
|
31
46
|
@logger.info { "*** reliable-msg agent service stopping..." }
|
32
|
-
@
|
47
|
+
@consumers.stop
|
33
48
|
@logger.info { "*** reliable-msg agent service stopped." }
|
34
49
|
}
|
35
50
|
end
|
36
51
|
|
52
|
+
# Return the state of alive or not alive.
|
53
|
+
#
|
37
54
|
def alive?
|
38
55
|
@locker.synchronize {
|
39
|
-
!! @
|
56
|
+
!! @consumers.alive?
|
40
57
|
}
|
41
58
|
end
|
59
|
+
|
60
|
+
# For testcase. To replace the class for which Service depends with Mock.
|
61
|
+
#
|
62
|
+
# === Example
|
63
|
+
#
|
64
|
+
# # Change method scope.
|
65
|
+
# Service.class_eval {
|
66
|
+
# public_class_method :dependency_classes
|
67
|
+
# public_class_method :dependency_classes_init
|
68
|
+
# }
|
69
|
+
#
|
70
|
+
# # Consumers is replaced with ConsumersMock.
|
71
|
+
# Service.dependency_classes[:Consumers] = ConsumersMock
|
72
|
+
#
|
73
|
+
# # test code that uses ConsumersMock...
|
74
|
+
# test_foo
|
75
|
+
# test_bar
|
76
|
+
#
|
77
|
+
# # re-init.
|
78
|
+
# Service.dependency_classes_init
|
79
|
+
#
|
80
|
+
def self.dependency_classes
|
81
|
+
@@dependency_classes
|
82
|
+
end
|
83
|
+
private_class_method :dependency_classes
|
84
|
+
|
85
|
+
# For testcase. Dependency classes is initialized.
|
86
|
+
#
|
87
|
+
def self.dependency_classes_init
|
88
|
+
@@dependency_classes = {
|
89
|
+
:Consumers => Consumers,
|
90
|
+
}
|
91
|
+
end
|
92
|
+
private_class_method :dependency_classes_init
|
93
|
+
dependency_classes_init
|
94
|
+
|
42
95
|
end
|
43
96
|
end
|
44
97
|
|
data/resources/agent.conf
CHANGED
data/spec/service_spec.rb
CHANGED
@@ -5,8 +5,12 @@ require "logger"
|
|
5
5
|
|
6
6
|
describe ReliableMsg::Agent::Service do
|
7
7
|
before do
|
8
|
-
|
9
|
-
|
8
|
+
@times_of_start_stop = 30
|
9
|
+
|
10
|
+
@logger = Logger.new(nil)
|
11
|
+
@conf = {
|
12
|
+
"foo" => "bar",
|
13
|
+
"baz" => "qux",
|
10
14
|
"consumers" => [
|
11
15
|
{
|
12
16
|
"source_uri" => "druby://localhost:6438",
|
@@ -16,31 +20,90 @@ describe ReliableMsg::Agent::Service do
|
|
16
20
|
},
|
17
21
|
],
|
18
22
|
}
|
19
|
-
@s = ReliableMsg::Agent::Service.new logger, conf
|
20
23
|
end
|
21
24
|
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
+
describe "When initialize" do
|
26
|
+
before do
|
27
|
+
@s = ReliableMsg::Agent::Service.new @logger, @conf
|
28
|
+
end
|
25
29
|
|
26
|
-
|
27
|
-
|
30
|
+
it "should be able to #start" do
|
31
|
+
@s.start
|
32
|
+
end
|
33
|
+
|
34
|
+
it "should not be able to #stop" do
|
35
|
+
Proc.new {
|
36
|
+
@s.stop
|
37
|
+
}.should raise_error ReliableMsg::Agent::AgentError
|
38
|
+
end
|
39
|
+
|
40
|
+
it "should not be alive" do
|
41
|
+
@s.alive?.should_not be_true
|
42
|
+
end
|
43
|
+
|
44
|
+
describe "When started" do
|
45
|
+
before do
|
46
|
+
@s.start
|
47
|
+
end
|
48
|
+
|
49
|
+
it "should not be able to #start" do
|
50
|
+
Proc.new {
|
51
|
+
@s.start
|
52
|
+
}.should raise_error ReliableMsg::Agent::AgentError
|
53
|
+
end
|
54
|
+
|
55
|
+
it "should be able to #stop" do
|
56
|
+
@s.stop
|
57
|
+
end
|
58
|
+
|
59
|
+
it "should be alive" do
|
60
|
+
@s.alive?.should be_true
|
61
|
+
end
|
62
|
+
|
63
|
+
end
|
64
|
+
|
65
|
+
it "should be able to #start > #stop > #start > ..." do
|
66
|
+
@times_of_start_stop.times {
|
67
|
+
@s.start
|
68
|
+
@s.stop
|
69
|
+
}
|
70
|
+
end
|
71
|
+
|
72
|
+
after do
|
73
|
+
@s.stop rescue nil
|
74
|
+
@s = nil
|
75
|
+
end
|
28
76
|
end
|
29
77
|
|
30
|
-
describe "When
|
78
|
+
describe "When replace the mock classes depend Service" do
|
31
79
|
before do
|
32
|
-
@
|
33
|
-
|
80
|
+
@consumers = mock ReliableMsg::Agent::Consumers
|
81
|
+
@consumers.stub!(:new).and_return @consumers
|
82
|
+
|
83
|
+
@consumers.stub!(:start)
|
84
|
+
@consumers.stub!(:stop)
|
85
|
+
@consumers.stub!(:alive?).and_return(true)
|
34
86
|
|
35
|
-
|
36
|
-
|
87
|
+
ReliableMsg::Agent::Service.class_eval {
|
88
|
+
public_class_method :dependency_classes
|
89
|
+
public_class_method :dependency_classes_init
|
90
|
+
}
|
91
|
+
ReliableMsg::Agent::Service.dependency_classes[:Consumers] = @consumers
|
92
|
+
end
|
93
|
+
|
94
|
+
describe "When initialize" do
|
95
|
+
it "mockclass should receive #new(@logger, @conf['consumers']) exactly 1" do
|
96
|
+
@consumers.should_receive(:new).
|
97
|
+
with(@logger, @conf["consumers"]).
|
98
|
+
exactly(1)
|
99
|
+
ReliableMsg::Agent::Service.new @logger, @conf
|
100
|
+
end
|
101
|
+
end
|
102
|
+
|
103
|
+
after do
|
104
|
+
ReliableMsg::Agent::Service.dependency_classes_init
|
37
105
|
end
|
38
|
-
|
39
106
|
end
|
40
107
|
|
41
|
-
after do
|
42
|
-
@s.stop rescue nil
|
43
|
-
@s = nil
|
44
|
-
end
|
45
108
|
end
|
46
109
|
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: reliable-msg-agent
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 25
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 0
|
9
|
-
-
|
10
|
-
version: 0.0.
|
9
|
+
- 3
|
10
|
+
version: 0.0.3
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- hamajyotan
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2011-03-
|
18
|
+
date: 2011-03-31 00:00:00 +09:00
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|