reliable-msg-agent 0.0.3 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/History.txt CHANGED
@@ -1,4 +1,14 @@
1
1
 
2
+ == 0.1.0 2011-04-04
3
+
4
+ * Changed README.rdoc
5
+ * Added testcase.
6
+ * bugfix: The relative path was not correctly interpreted.
7
+ * Added commandline option daemon mode(-d).
8
+ * Added commandline option user(-u) and group(-g).
9
+ * Removed "user" and "group" from configurations.
10
+ * Changed "agent" config option changes arbitrarily from indispensability.
11
+
2
12
  == 0.0.3 2011-03-31
3
13
 
4
14
  * Added testcase for Service class.
data/README.rdoc CHANGED
@@ -24,20 +24,153 @@ in A and processes it is taken.
24
24
  | Agent | ... + Agent +
25
25
  +-------+ +-------+
26
26
 
27
- == Getting started
27
+ == Getting started 1
28
28
 
29
29
  === Install
30
30
 
31
31
  gem install reliable-msg-agent
32
32
 
33
- === Set configuration file
34
-
35
- * {GEM_INSTALL_DIR}/resources/agent.conf
36
- please arrange agent.conf in appropriate path.
37
- (default: /etc/reliable-msg-agent/agent.conf)
38
- * {GEM_INSTALL_DIR}/resources/agent.rb
39
- please arrange agent.conf in appropriate path.
40
- (default: /etc/reliable-msg-agent/agent.rb)
33
+ === Start reliable-msg
34
+
35
+ reliable-msg is started beforehand.
36
+
37
+ $ queues manager start
38
+ I, [2011-04-03T23:00:43.781875 #1856] INFO -- : Created queues configuration file in: /usr/loc...
39
+ I, [2011-04-03T23:00:43.781875 #1856] INFO -- : Using message store: disk
40
+ I, [2011-04-03T23:00:43.812875 #1856] INFO -- : Accepting requests at: druby://localhost:6438
41
+
42
+ === Start reliable-msg-agent
43
+
44
+ $ reliable-msg-agent start
45
+ *** Starting ReliableMsg-Agent...
46
+ I, [2011-04-03T23:02:11.390875 #2724] INFO -- : *** reliable-msg agent service starting...
47
+ I, [2011-04-03T23:02:11.390875 #2724] INFO -- : --- starting workers.
48
+ I, [2011-04-03T23:02:11.452875 #2724] INFO -- : *** reliable-msg agent service started.
49
+
50
+ === Push message to reliable-msg queue.
51
+
52
+ The message "foo" is push to queue of reliable-msg.
53
+
54
+ $ irb -rubygems -rreliable-msg
55
+ irb(main):001:0> ReliableMsg::Queue.new("queue.agent").put("foo")
56
+ => "0d98c020-402a-012e-f635-000ae47391e2"
57
+ irb(main):002:0> exit
58
+ $
59
+
60
+ === The console that executes reliable-msg-agent is seen.
61
+
62
+ The message is acquired from reliable-msg, and the log is output.
63
+
64
+ $ queues manager start
65
+ I, [2011-04-03T23:13:41.046875 #3860] INFO -- : *** reliable-msg agent service starting...
66
+ I, [2011-04-03T23:13:41.046875 #3860] INFO -- : --- starting workers.
67
+ I, [2011-04-03T23:13:41.093875 #3860] INFO -- : *** reliable-msg agent service started.
68
+ I, [2011-04-03T23:14:24.280875 #3860] INFO -- : message fetched - <9957e5a0-402a-012e-f635-000ae47391e2>
69
+ I, [2011-04-03T23:14:24.280875 #3860] INFO -- : message received
70
+ --- !ruby/object:ReliableMsg::Message
71
+ headers:
72
+ :delivery: :best_effort
73
+ :max_deliveries: 5
74
+ :created: 1301840063
75
+ :id: 9957e5a0-402a-012e-f635-000ae47391e2
76
+ :expires:
77
+ :priority: 0
78
+ id: 9957e5a0-402a-012e-f635-000ae47391e2
79
+ object: foo
80
+
81
+ == Getting started 2 - The behavior when the message is acquired is changed.
82
+
83
+ Default operation of reliable-msg-agent is only a log output.
84
+ This can be changed.
85
+
86
+ === Start reliable-msg
87
+
88
+ reliable-msg is started beforehand.
89
+
90
+ $ queues manager start
91
+ I, [2011-04-03T23:00:43.781875 #1856] INFO -- : Created queues configuration file in: /usr/loc...
92
+ I, [2011-04-03T23:00:43.781875 #1856] INFO -- : Using message store: disk
93
+ I, [2011-04-03T23:00:43.812875 #1856] INFO -- : Accepting requests at: druby://localhost:6438
94
+
95
+ === Preparation for configuration file
96
+
97
+ The file /etc/reliable-msg-agent.conf of the following content is prepared.
98
+
99
+ # /etc/reliable-msg-agent.conf
100
+ ---
101
+ agent: /etc/reliable-msg-agent.rb
102
+ consumers:
103
+ -
104
+ source_uri: druby://localhost:6438
105
+ every: 1.0
106
+ target: queue.agent
107
+ threads: 1
108
+
109
+ In addition, the file /etc/reliable-msg-agent.rb of the following content is prepared.
110
+ Here, the directory specified by the message is made.
111
+
112
+ # /etc/reliable-msg-agnet.rb
113
+ require "fileutils"
114
+
115
+ # The method of processing the message is defined.
116
+ #
117
+ # if the evaluation result is nil or false,
118
+ # it is considered that it failes.
119
+ #
120
+ # === Args
121
+ #
122
+ # +msg+ :: fetched message from reliable-msg queue.
123
+ # +conf+ :: consumer configurations.
124
+ # +options+ :: the options (it is still unused.)
125
+ #
126
+ def call msg, conf, options = {}
127
+ dir = File.join "/tmp", msg.object
128
+ FileUtils.mkdir_p dir
129
+ @logger.info { "directory created - #{dir}" }
130
+ end
131
+
132
+ === Start reliable-msg-agent
133
+
134
+ The configuration file is specified by commandline option.
135
+
136
+ $ reliable-msg-agent start -c /etc/reliable-msg-agent.conf
137
+ *** Starting ReliableMsg-Agent...
138
+ I, [2011-04-04T00:06:23.759875 #1804] INFO -- : *** reliable-msg agent service starting...
139
+ I, [2011-04-04T00:06:23.759875 #1804] INFO -- : --- starting workers.
140
+ I, [2011-04-04T00:06:23.783875 #1804] INFO -- : *** reliable-msg agent service started.
141
+
142
+ === Push message to reliable-msg queue.
143
+
144
+ The message "foo" and "bar" are push to queue of reliable-msg.
145
+
146
+ $ irb -rubygems -rreliable-msg
147
+ irb(main):001:0> ReliableMsg::Queue.new("queue.agent").put("foo")
148
+ => "f19aadd0-4032-012e-f635-000ae47391e2"
149
+ irb(main):002:0> ReliableMsg::Queue.new("queue.agent").put("bar")
150
+ => "f3c33320-4032-012e-f635-000ae47391e2"
151
+ irb(main):003:0> exit
152
+ $
153
+
154
+ === The console that executes reliable-msg-agent is seen.
155
+
156
+ The message is acquired from reliable-msg, and directory was made.
157
+
158
+ $ reliable-msg-agent start -c /etc/reliable-msg-agent.conf
159
+ *** Starting ReliableMsg-Agent...
160
+ I, [2011-04-04T00:06:23.759875 #1804] INFO -- : *** reliable-msg agent service starting...
161
+ I, [2011-04-04T00:06:23.759875 #1804] INFO -- : --- starting workers.
162
+ I, [2011-04-04T00:06:23.783875 #1804] INFO -- : *** reliable-msg agent service started.
163
+ I, [2011-04-04T00:14:07.921875 #1804] INFO -- : message fetched - <f19aadd0-4032-012e-f635-000ae47391e2>
164
+ I, [2011-04-04T00:14:07.936875 #1804] INFO -- : directory created - /tmp/foo
165
+ I, [2011-04-04T00:14:11.102875 #1804] INFO -- : message fetched - <f3c33320-4032-012e-f635-000ae47391e2>
166
+ I, [2011-04-04T00:14:11.106875 #1804] INFO -- : directory created - /tmp/bar
167
+
168
+ $ ls -l /tmp/{foo,bar}
169
+ /tmp/bar:
170
+ total 0
171
+
172
+ /tmp/foo:
173
+ total 0
41
174
 
42
175
  == Reliable-msg-agent commands
43
176
 
@@ -51,20 +184,29 @@ in A and processes it is taken.
51
184
  $ reliable-msg-agent start --help
52
185
  Usage: reliable-msg-agent start [options]
53
186
  -v, --verbose Verbose output
187
+ -d, --daemon Daemon mode
188
+ -u, --user=USER Effective user name
189
+ -g, --group=GROUP Effective group name
54
190
  -c, --conf=CONFFILE Config file
55
- -l, --log=LOGFILE Log file
56
- -p, --pid=PIDFILE PID file
191
+ -l, --log=LOGFILE Log file (only for the daemon mode)
192
+ -p, --pid=PIDFILE PID file (only for the daemon mode)
57
193
  -h, --help Show this message
58
194
  --version Show version
59
195
 
60
196
  * -v, --verbose
61
197
  verbose output.
198
+ * -d, --daemon
199
+ daemon mode.
200
+ * -u, --user
201
+ effective user name (The authority of seteuid is necessary.)
202
+ * -g, --group
203
+ effective group name (The authority of setegid is necessary.)
62
204
  * -c, --conf=CONFFILE
63
- path for config file. default is /etc/reliable-msg-agent/agent.conf
205
+ path for config file. default is {GEM_INSTALL_DIR}/resources/agent.conf
64
206
  * -l, --log=LOGFILE
65
- path for log file. default is /var/reliable-msg-agent/agent.log
207
+ path for log file. default is /var/reliable-msg-agent/agent.log (only for the daemon mode)
66
208
  * -p, --pid=PIDFILE
67
- path for pid file. default is /var/reliable-msg-agent/agent.pid
209
+ path for pid file. default is /var/reliable-msg-agent/agent.pid (only for the daemon mode)
68
210
  * -h, --help
69
211
  show this message.
70
212
  * --version
@@ -75,7 +217,7 @@ in A and processes it is taken.
75
217
  $ reliable-msg-agent stop --help
76
218
  Usage: reliable-msg-agent stop [options]
77
219
  -v, --verbose Verbose output
78
- -p, --pid=PIDFILE PID file (only for the daemon mode)
220
+ -p, --pid=PIDFILE PID file
79
221
  -h, --help Show this message
80
222
  --version Show version
81
223
 
@@ -14,14 +14,23 @@ parsers["start"] = OptionParser.new { |parser|
14
14
  parser.on("-v", "--verbose", "Verbose output") {
15
15
  options[:verbose] = true
16
16
  }
17
+ parser.on("-d", "--daemon", "Daemon mode") {
18
+ options[:daemon] = true
19
+ }
20
+ parser.on("-u", "--user=USER", "Effective user name") { |v|
21
+ options[:user] = v
22
+ }
23
+ parser.on("-g", "--group=GROUP", "Effective group name") { |v|
24
+ options[:group] = v
25
+ }
17
26
  parser.on("-c", "--conf=CONFFILE", "Config file") { |v|
18
- options[:conf] = v
27
+ options[:conf] = File.expand_path(v)
19
28
  }
20
- parser.on("-l", "--log=LOGFILE", "Log file") { |v|
21
- options[:log] = v
29
+ parser.on("-l", "--log=LOGFILE", "Log file (only for the daemon mode)") { |v|
30
+ options[:log] = File.expand_path(v)
22
31
  }
23
- parser.on("-p", "--pid=PIDFILE", "PID file") { |v|
24
- options[:pid] = v
32
+ parser.on("-p", "--pid=PIDFILE", "PID file (only for the daemon mode)") { |v|
33
+ options[:pid] = File.expand_path(v)
25
34
  }
26
35
  }
27
36
  parsers["stop"] = OptionParser.new { |parser|
@@ -30,8 +39,8 @@ parsers["stop"] = OptionParser.new { |parser|
30
39
  parser.on("-v", "--verbose", "Verbose output") {
31
40
  options[:verbose] = true
32
41
  }
33
- parser.on("-p", "--pid=PIDFILE", "PID file (only for the daemon mode)") { |v|
34
- options[:pid] = v
42
+ parser.on("-p", "--pid=PIDFILE", "PID file") { |v|
43
+ options[:pid] = File.expand_path(v)
35
44
  }
36
45
  }
37
46
  parsers.each { |k,v| parser = v
@@ -54,7 +63,13 @@ unless parsers.keys.include?(subcommand)
54
63
  $stderr.puts
55
64
  exit 1
56
65
  end
57
- parsers[subcommand].parse! ARGV
66
+
67
+ begin
68
+ parsers[subcommand].parse! ARGV
69
+ rescue
70
+ $stderr.puts parsers[subcommand].help
71
+ exit 1
72
+ end
58
73
 
59
74
  require "reliable-msg/agent/scripts/script_runner"
60
75
  ReliableMsg::Agent::ScriptRunner.new.send subcommand.to_sym, options
@@ -2,6 +2,8 @@
2
2
  require "rubygems"
3
3
  require "reliable-msg/agent"
4
4
 
5
+ require "yaml"
6
+
5
7
  module ReliableMsg::Agent #:nodoc:
6
8
  class Agent
7
9
  def initialize logger
@@ -20,7 +22,7 @@ module ReliableMsg::Agent #:nodoc:
20
22
  # +options+ :: the options (it is still unused.)
21
23
  #
22
24
  def call msg, conf, options = {}
23
- raise AgentError, "#call(msg,conf,options={}) not implemented!"
25
+ @logger.info { "message received\n#{msg.to_yaml}" }
24
26
  end
25
27
  end
26
28
  end
@@ -17,28 +17,43 @@ module ReliableMsg::Agent #:nodoc:
17
17
 
18
18
  default_options = {
19
19
  :verbose => false,
20
- :conf => "/etc/reliable-msg-agent/agent.conf",
20
+ :daemon => false,
21
+ :user => nil,
22
+ :group => nil,
23
+ :conf => File.expand_path(File.dirname(__FILE__) + "/../../../../resources/agent.conf"),
21
24
  :log => "/var/reliable-msg-agent/agent.log",
22
25
  :pid => "/var/reliable-msg-agent/agent.pid",
23
26
  }
24
27
  opt = default_options.merge options
25
28
 
26
29
  conf = load_configurations opt[:conf]
27
- change_privilege conf
28
- pidfile_accessible_test opt[:pid]
29
- logger = create_logger conf["logger"], opt[:log]
30
+ change_privilege opt[:user], opt[:group]
31
+ pidfile_accessible_test opt[:pid] if opt[:daemon]
32
+ logger = if opt[:daemon]
33
+ create_logger conf["logger"], opt[:log]
34
+ else
35
+ create_logger conf["logger"], $stdout
36
+ end
30
37
 
31
38
  agent_definition conf["agent"]
32
39
 
33
- daemonize(logger) {
40
+ if opt[:daemon]
41
+ daemonize(logger) {
42
+ service = Service.new logger, conf
43
+ register_signal_handler logger, service, opt[:pid]
44
+
45
+ service.start
46
+ write_pidfile opt[:pid]
47
+ while service.alive?; sleep 3; end
48
+ }
49
+ else
34
50
  service = Service.new logger, conf
35
-
36
- register_signal_handler logger, service, opt[:pid]
37
-
51
+ register_signal_handler logger, service
52
+
38
53
  service.start
39
- write_pidfile opt[:pid]
40
54
  while service.alive?; sleep 3; end
41
- }
55
+ exit 0
56
+ end
42
57
 
43
58
  rescue Exception => e
44
59
  $stderr.puts "--- ReliableMsg-Agent error! - #{e.message}"
@@ -80,22 +95,18 @@ module ReliableMsg::Agent #:nodoc:
80
95
  YAML.load(ERB.new(IO.read(conffile)).result)
81
96
  end
82
97
 
83
- def change_privilege conf
84
- user = conf["user"]
98
+ def change_privilege user, group
85
99
  uid = begin
86
- Etc.getpwnam(user.to_s).uid
100
+ user ? Etc.getpwnam(user.to_s).uid : Process.euid
87
101
  rescue ArgumentError
88
102
  raise "can't find user for #{user}"
89
103
  end
90
- group = conf["group"]
91
104
  gid = begin
92
- Etc.getgrnam(group.to_s).gid
105
+ group ? Etc.getgrnam(group.to_s).gid : Process.egid
93
106
  rescue ArgumentError
94
107
  raise "can't find group for #{group}"
95
108
  end
96
109
 
97
- raise "Dont't run as root user." if uid == 0
98
-
99
110
  Process::Sys.setegid(gid)
100
111
  Process::Sys.seteuid(uid)
101
112
  end
@@ -135,19 +146,19 @@ module ReliableMsg::Agent #:nodoc:
135
146
  }
136
147
  end
137
148
 
138
- def register_signal_handler logger, service, pidfile
149
+ def register_signal_handler logger, service, pidfile = nil
139
150
  stopping = false
140
- [:TERM].each { |sig|
151
+ [:INT, :TERM].each { |sig|
141
152
  Signal.trap(sig) {
142
153
  unless stopping
143
154
  begin
144
155
  stopping = true
145
156
  service.stop
146
- File.unlink pidfile if File.exist? pidfile
157
+ File.unlink pidfile if pidfile and File.exist? pidfile
147
158
  exit! 0
148
159
 
149
160
  rescue Exception => e
150
- File.unlink pidfile if File.exist? pidfile
161
+ File.unlink pidfile if pidfile and File.exist? pidfile
151
162
  logger.fatal { "ReliableMsg-Agent error! - #{e.message}" }
152
163
  logger.fatal { e.backtrace.join("\n\t") }
153
164
  exit! 1
@@ -162,7 +173,7 @@ module ReliableMsg::Agent #:nodoc:
162
173
  end
163
174
 
164
175
  def agent_definition deffile
165
- Agent.class_eval open(deffile, "r") { |f| f.read }
176
+ Agent.class_eval open(deffile, "r") { |f| f.read } if deffile
166
177
  end
167
178
 
168
179
  end
@@ -6,8 +6,8 @@ module ReliableMsg::Agent #:nodoc:
6
6
  module Version #:nodoc:
7
7
  unless defined? MAJOR
8
8
  MAJOR = 0
9
- MINOR = 0
10
- TINY = 3
9
+ MINOR = 1
10
+ TINY = 0
11
11
  PRE = nil
12
12
 
13
13
  STRING = [MAJOR, MINOR, TINY, PRE].compact.join(".")
data/resources/agent.conf CHANGED
@@ -1,8 +1,5 @@
1
1
  ---
2
- user: agent
3
- group: agent
4
2
  logger: " Proc.new { |file| l = Logger.new(file); l.level = Logger::DEBUG; l } "
5
- agent: /etc/reliable-msg-agent/agent.rb
6
3
 
7
4
  consumers:
8
5
  -
@@ -1,6 +1,4 @@
1
1
  ---
2
- user: agent
3
- group: agent
4
2
  logger: " Proc.new { |file| l = Logger.new(file); l.level = Logger::DEBUG; l } "
5
3
  agent: /etc/reliable-msg-agent/agent.rb
6
4
 
@@ -1,6 +1,4 @@
1
1
  ---
2
- user: agent
3
- group: agent
4
2
  logger: " Proc.new { |file| l = Logger.new(file); l.level = Logger::WARN; l } "
5
3
  agent: /etc/reliable-msg-agent/agent.rb
6
4
 
@@ -1,6 +1,4 @@
1
1
  ---
2
- user: agent
3
- group: agent
4
2
  logger: " Proc.new { |file| Logger.new(file, 'daily') } "
5
3
  agent: /etc/reliable-msg-agent/agent.rb
6
4
 
@@ -1,6 +1,4 @@
1
1
  ---
2
- user: agent
3
- group: agent
4
2
  logger: " Proc.new { |file| Logger.new(file, 7, 10*1024*1024) } "
5
3
  agent: /etc/reliable-msg-agent/agent.rb
6
4
 
@@ -7,13 +7,15 @@
7
7
  prog=reliable-msg-agentd
8
8
 
9
9
  base=reliable-msg-agent
10
+ user=root
11
+ group=root
10
12
  cnf=/etc/reliable-msg-agent/agent.conf
11
13
  pid=/var/reliable-msg-agent/agent.pid
12
14
  log=/var/reliable-msg-agent/agent.log
13
15
 
14
16
  start() {
15
17
  echo $"Start ReliableMsg-Agent ..."
16
- $base start -c $cnf -p $pid -l $log -d
18
+ $base start -u $user -g $group -c $cnf -p $pid -l $log -d
17
19
  }
18
20
 
19
21
  stop() {
data/spec/service_spec.rb CHANGED
@@ -80,9 +80,10 @@ describe ReliableMsg::Agent::Service do
80
80
  @consumers = mock ReliableMsg::Agent::Consumers
81
81
  @consumers.stub!(:new).and_return @consumers
82
82
 
83
- @consumers.stub!(:start)
84
- @consumers.stub!(:stop)
85
- @consumers.stub!(:alive?).and_return(true)
83
+ consumers_alive = false
84
+ @consumers.stub!(:start) { consumers_alive = true }
85
+ @consumers.stub!(:stop) { consumers_alive = false }
86
+ @consumers.stub!(:alive?).and_return { consumers_alive }
86
87
 
87
88
  ReliableMsg::Agent::Service.class_eval {
88
89
  public_class_method :dependency_classes
@@ -98,12 +99,27 @@ describe ReliableMsg::Agent::Service do
98
99
  exactly(1)
99
100
  ReliableMsg::Agent::Service.new @logger, @conf
100
101
  end
102
+
103
+ describe "#start" do
104
+ before do
105
+ @s = ReliableMsg::Agent::Service.new @logger, @conf
106
+ end
107
+
108
+ it "mockclass should receive #start exactly 1" do
109
+ @consumers.should_receive(:start).with(no_args).exactly(1)
110
+ @s.start
111
+ end
112
+
113
+ after do
114
+ @s.stop rescue nil
115
+ @s = nil
116
+ end
117
+ end
101
118
  end
102
119
 
103
120
  after do
104
121
  ReliableMsg::Agent::Service.dependency_classes_init
105
122
  end
106
123
  end
107
-
108
124
  end
109
125
 
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: 25
4
+ hash: 27
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
+ - 1
8
9
  - 0
9
- - 3
10
- version: 0.0.3
10
+ version: 0.1.0
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-31 00:00:00 +09:00
18
+ date: 2011-04-04 00:00:00 +09:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency