smith 0.7.1 → 0.7.2

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 8f1a64e720f6d09734b66a3bf1f39b56ec81aed8
4
- data.tar.gz: baafcf0aa8e1f048ea9ef250adc8475d8aa53408
3
+ metadata.gz: 60291ee6d180d1883c743e69e17c590f350c29ee
4
+ data.tar.gz: 9c2e93dad014cdc16a813c38c3ffc3402d68a81e
5
5
  SHA512:
6
- metadata.gz: afca0d267739a592b25876133fa70fd0f2f03f962e22a3a4af35efa67b28cb9b47068526142e707db7afa7a496734c8f396c8b884ec0d3295c1706a5f7ab7f94
7
- data.tar.gz: 0365407a67ab65dc32d41bbf91b316db1dde05851f6191df1a49bfcba611246efc09291f06abba2286c499f60f0807c484226ec5314b6fc315d773b712279a41
6
+ metadata.gz: 0ade57bcee554e10eac994c33f3ce32660d19e8938d25fb49e91c1e6c3c7494823f7c2bb3bad0734e9852a076ad04ad46b4f015d0822e6768f77ed8ed7cb951c
7
+ data.tar.gz: d3c2507f28b378a2d74924ee0d04ff03dd6605e2153587284cd0526ccd64549855eab4267f132e506b4e2a8fa452b984972fdf659a6940c641a0b869ba021683
@@ -195,7 +195,6 @@ module Smith
195
195
  end
196
196
  end
197
197
  end
198
-
199
198
  end
200
199
  end
201
200
 
@@ -41,7 +41,8 @@ module Smith
41
41
 
42
42
  def load_agent
43
43
  path = agent_directories(@agent_name)
44
- logger.debug { "Loading #{@agent_name} from: #{path.dirname}" }
44
+ logger.info { "Loading #{@agent_name}" }
45
+ logger.debug { "Loading #{@agent_name} from: #{path}" }
45
46
  add_agent_load_path(path)
46
47
  load path
47
48
 
@@ -49,7 +50,7 @@ module Smith
49
50
  @agent = class_from_name(@agent_name).new(@agent_uuid)
50
51
  rescue NameError => e
51
52
  # TODO: include the class name from the path.
52
- logger.fatal { "Cannot instantiate agent. The class name: #{@agent_name} probably didn't match the path" }
53
+ logger.fatal { "Cannot instantiate agent. File #{path} exists but doesn't contain the Class: #{@agent_name}." }
53
54
  terminate!
54
55
  false
55
56
  end
@@ -66,9 +67,7 @@ module Smith
66
67
  # See the note at the in main.
67
68
  def terminate!(exception=nil)
68
69
 
69
- @agent.__send__(:__exception_handler, exception) if @agent
70
- logger.error { format_exception(exception) }
71
- logger.error { "Terminating: #{@agent_uuid}." }
70
+ run_exception_handler(exception)
72
71
 
73
72
  if Smith.running?
74
73
  send_dead_message
@@ -111,12 +110,23 @@ module Smith
111
110
  end
112
111
  end
113
112
 
114
- def format_exception(exception)
115
- str = "#{exception.class.to_s}: #{exception.message}\n\t"
116
- if exception.backtrace
117
- str << exception.backtrace[0..-1].join("\n\t")
113
+ # Format any excptions
114
+ # @param e [Exception] the exeption to format
115
+ # @return [String] formated exception
116
+ def format_exception(e)
117
+ "#{e.class.to_s}: #{e.inspect}\n\t".tap do
118
+ str << e.backtrace[0..-1].join("\n\t") if e.backtrace
119
+ end
120
+ end
121
+
122
+ # Run the on_exception proc defined in the agent.
123
+ # @param e [Exception] the exeption to handle
124
+ def run_exception_handler(exception)
125
+ if exception && @agent
126
+ @agent.__send__(:__exception_handler, exception)
127
+ logger.error { format_exception(exception) }
128
+ logger.error { "Terminating: #{@agent_uuid}." }
118
129
  end
119
- str
120
130
  end
121
131
 
122
132
  # Add the ../lib to the load path. This assumes the directory
@@ -131,10 +141,15 @@ module Smith
131
141
  # TODO think this through some more.
132
142
  def add_agent_load_path(path)
133
143
  path = path.dirname.dirname.join('lib')
134
- # The load path may be a pathname or a string. Change to strings.
135
- unless $:.detect { |p| p.to_s == path.to_s }
136
- logger.debug { "Adding #{path} to load path" }
137
- $: << path
144
+
145
+ Smith.agent_directories.each do |path|
146
+ lib_path = path.parent.join('lib')
147
+ if lib_path.exist?
148
+ logger.info { "Adding #{lib_path} to load path" }
149
+ $LOAD_PATH << path
150
+ else
151
+ logger.info { "No lib directory for: #{path.parent}." }
152
+ end
138
153
  end
139
154
  end
140
155
  end
@@ -2,12 +2,28 @@
2
2
  module Smith
3
3
  module Commands
4
4
  module Common
5
+
6
+ # Returns the fully qualified class of all agents in a group. The group
7
+ # //must// be a sub-directory in the agents directory. Only sym-links are
8
+ # considered. The group directory is recursively searched and addeed to
9
+ # the ilst of agents for that group if it passed all checks.
10
+ #
11
+ # @param group [String] the group name
12
+ #
13
+ # @return [Array<Class>] the class of each agent in the group
5
14
  def agent_group(group)
6
- agents = Smith.agent_directories.map do |path|
7
- group_dir = path.join(group)
8
- if group_dir.exist? && group_dir.directory?
9
- agents = Pathname.glob(group_dir.join("*_agent.rb"))
10
- agents.map {|a| Extlib::Inflection.camelize(a.basename(".rb").to_s)}
15
+ agents = Smith.agent_directories.map do |agent_directory|
16
+ group_directory = agent_directory.join(group)
17
+
18
+ if group_directory.exist? && group_directory.directory?
19
+ agents = Pathname.glob(group_directory.join("*.rb")).map(&:expand_path)
20
+
21
+ agents.each_with_object([]) do |agent, acc|
22
+ if agent.symlink?
23
+ expanded_agent_path = resolve_agent_path(group_directory, agent)
24
+ acc << Utils.class_name_from_path(expanded_agent_path, agent_directory)
25
+ end
26
+ end
11
27
  else
12
28
  nil
13
29
  end
@@ -16,6 +32,12 @@ module Smith
16
32
  raise RuntimeError, "Group does not exist: #{group}" if agents == [nil]
17
33
  agents.compact.flatten
18
34
  end
35
+
36
+ private
37
+
38
+ def resolve_agent_path(group_directory, agent)
39
+ agent.readlink.expand_path(group_directory)
40
+ end
19
41
  end
20
42
  end
21
43
  end
@@ -53,10 +53,11 @@ module Smith
53
53
  end
54
54
  end
55
55
 
56
- # Delete an queue.
56
+ # Delete a queue.
57
57
  #
58
- # @param name [String] name of the queue.
58
+ # @param queue_name [String] name of the queue.
59
59
  # @yield calls the block when the queue has been deleted
60
+ #
60
61
  # @yieldparam [AMQ::Protocol::Channel::Close] the amqp close message
61
62
  # FIXME: remove duplication
62
63
  def delete_queue(queue_name, &blk)
@@ -152,16 +152,16 @@ module Smith
152
152
 
153
153
  # Loads and merges multiple toml files.
154
154
  #
155
- # @params [String] the default toml file
156
- # @params [String] the user supplied toml file
155
+ # @param default [String] the default toml file
156
+ # @param secondary [String] the user supplied toml file
157
157
  # @return [ConfigHash] the merge toml files.
158
- def load_tomls(default, main)
159
- load_toml(default).deep_merge(load_toml(main))
158
+ def load_tomls(default, secondary)
159
+ load_toml(default).deep_merge(load_toml(secondary))
160
160
  end
161
161
 
162
162
  # Load the toml file specified
163
163
  #
164
- # @param [Pathname] the path of the toml file
164
+ # @param path [Pathname] the path of the toml file
165
165
  # @return [ConfigHash] the toml file
166
166
  def load_toml(path)
167
167
  ConfigHash.new(TOML.parse(path.read, :symbolize_keys => true))
@@ -24,12 +24,16 @@ module Smith
24
24
  parts[-1] = "#{parts[-1]}.rb"
25
25
  Pathname.new(root).join(*parts)
26
26
  end
27
+ module_function :path_from_class
27
28
 
28
- def class_name_from_path(root, path)
29
+ # Returns a Constant based on the pathname.
30
+ def class_name_from_path(path, root=Pathname.new('.'))
29
31
  relative_path = path.relative_path_from(root)
30
- parts = relative_path.split
32
+ parts = split_path(relative_path.sub_ext(''))
33
+
31
34
  parts.map { |p| p.to_s.camel_case }.join('::')
32
35
  end
36
+ module_function :class_name_from_path
33
37
 
34
38
  # Performs a Kernel.const_get on each element of the class.
35
39
  #
@@ -38,7 +42,19 @@ module Smith
38
42
  def class_from_name(name)
39
43
  name.to_s.split(/::/).inject(Kernel) { |acc, t| acc.const_get(t) }
40
44
  end
45
+ module_function :class_from_name
46
+
47
+
48
+ # Slipts a path into it's component parts.
49
+ #
50
+ # @param pathname [Pathname] the path to split.
51
+ def split_path(pathname)
52
+ pathname.each_filename.inject([]) { |acc, p| acc << p }
53
+ end
54
+ module_function :split_path
41
55
 
56
+ # Check for the existance of a directory and create if it doesn't exist.
57
+ # @param dir [Pathname]
42
58
  def check_and_create_directory(dir)
43
59
  dir.tap do
44
60
  dir.exist? || dir.mkpath
@@ -1,3 +1,3 @@
1
1
  module Smith
2
- VERSION = "0.7.1"
2
+ VERSION = "0.7.2"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: smith
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.1
4
+ version: 0.7.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Richard Heycock
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-04-10 00:00:00.000000000 Z
11
+ date: 2015-05-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: oj
@@ -323,4 +323,4 @@ signing_key:
323
323
  specification_version: 4
324
324
  summary: Multi-agent framework
325
325
  test_files: []
326
- has_rdoc: false
326
+ has_rdoc: