carnivore 0.2.10 → 0.2.12

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: df028b8724cf4c5392538b3c6f9b36615c2f1b0e
4
- data.tar.gz: 5b9eb357bf2357f57a7d41fa6d44a3fe01374d85
3
+ metadata.gz: 488354b5c88ba4e3992fa4ee8ec6b81c42e52ef2
4
+ data.tar.gz: 0b33704a653ae60d4733fd1a3bb4b4b98d420196
5
5
  SHA512:
6
- metadata.gz: 74081290aa5c3469e05c7bc5f0bb3369152e695dd45c8bc962efb5da39bf0eaaa70b31d5185c8691d2734943d424da7c1bd8e53b18d3bffc9003977ad3d6d051
7
- data.tar.gz: 16243f3f57017e037b4788843a8ee942b6905ca2dee703b91276d2205940a6afcc873642003eed92b80d820e2fdaa93f2e0f87abfe3034749d38010dae5281c7
6
+ metadata.gz: d5da72980c4bd8d3ee3b99a1348f264008a9dd0ab1549da85ac43ad215772152a38f6b1f54b583f12959a1e72a78bf61f3dfe5533a709595decc65a308b91171
7
+ data.tar.gz: a24fb16e43b8ffa06c6f3e50495c93b4756ba9b7c9a21f0f9f46aa458ee4c05ca1fd8e933096032b6854951513536bd684e41705d971ef591bb450bef3aa3355
@@ -1,3 +1,9 @@
1
+ # v0.2.12
2
+ * Prevent source configuration output at INFO level on build
3
+ * Keep all sources paused until all sources are built
4
+ * Add helper for performing hash conversions
5
+ * Support deep hash conversions (process through enumerables)
6
+
1
7
  # v0.2.10
2
8
  * Remove explicit JSON usage
3
9
 
@@ -12,6 +12,8 @@ module Carnivore
12
12
  include Celluloid
13
13
  include Carnivore::Utils::Logging
14
14
  # @!parse include Carnivore::Utils::Logging
15
+ include Carnivore::Utils::Failure
16
+ # @!parse include Carnivore::Utils::Failure
15
17
 
16
18
  # @return [String, Symbol] name of callback
17
19
  attr_reader :name
@@ -30,7 +32,9 @@ module Carnivore
30
32
  raise ArgumentError.new 'Block is required for dynamic callbacks!'
31
33
  end
32
34
  define_singleton_method(:execute, &block) if block
33
- setup
35
+ execute_and_retry_forever(:setup) do
36
+ setup
37
+ end
34
38
  end
35
39
 
36
40
  # Used by custom callback classes for setup
@@ -19,13 +19,24 @@ module Carnivore
19
19
  begin
20
20
  require 'carnivore/supervisor'
21
21
  supervisor = Carnivore::Supervisor.build!
22
+ Celluloid::Logger.info 'Initializing all registered sources.'
23
+ [].tap do |register|
24
+ Source.sources.each do |source|
25
+ register << Thread.new do
26
+ source.klass.reset_comms!
27
+ supervisor.supervise_as(
28
+ source.source_hash[:name],
29
+ source.klass,
30
+ source.source_hash.dup
31
+ )
32
+ end
33
+ end
34
+ end.map(&:join)
35
+ Celluloid::Logger.info 'Source initializations complete. Enabling message processing.'
22
36
  Source.sources.each do |source|
23
- source.klass.reset_comms!
24
- supervisor.supervise_as(
25
- source.source_hash[:name],
26
- source.klass,
27
- source.source_hash.dup
28
- )
37
+ if(source.source_hash.fetch(:auto_process, true))
38
+ supervisor[source.source_hash[:name]].start!
39
+ end
29
40
  end
30
41
  loop do
31
42
  # We do a sleep loop so we can periodically check on the
@@ -98,6 +98,8 @@ module Carnivore
98
98
  include Celluloid
99
99
  include Utils::Logging
100
100
  # @!parse include Carnivore::Utils::Logging
101
+ include Utils::Failure
102
+ # @!parse include Carnivore::Utils::Failure
101
103
 
102
104
  finalizer :teardown_cleanup
103
105
 
@@ -171,12 +173,11 @@ module Carnivore
171
173
  add_callback(name, block)
172
174
  end
173
175
  end
174
- setup(args)
175
- connect
176
- if(auto_process && !callbacks.empty?)
177
- async.process
178
- else
179
- warn 'Processing is disabled'
176
+ execute_and_retry_forever(:setup) do
177
+ setup(args)
178
+ end
179
+ execute_and_retry_forever(:connect) do
180
+ connect
180
181
  end
181
182
  info 'Source initialization is complete'
182
183
  rescue => e
@@ -184,6 +185,25 @@ module Carnivore
184
185
  raise
185
186
  end
186
187
 
188
+ # Start source if auto_process is enabled
189
+ #
190
+ # @return [TrueClass, FalseClass]
191
+ def start!
192
+ if(auto_process?)
193
+ info 'Message processing started via auto start'
194
+ async.process
195
+ true
196
+ else
197
+ warn 'Message processing is disabled via auto start'
198
+ false
199
+ end
200
+ end
201
+
202
+ # @return [TrueClass, FalseClass] auto processing enabled
203
+ def auto_process?
204
+ auto_process && !callbacks.empty?
205
+ end
206
+
187
207
  # Ensure we cleanup our internal supervisor before bailing out
188
208
  def teardown_cleanup
189
209
  warn 'Termination request received. Tearing down!'
@@ -335,47 +355,51 @@ module Carnivore
335
355
  # @param args [Object] list of arguments
336
356
  # @return [TrueClass]
337
357
  def process(*args)
338
- begin
339
- async.receive_messages
340
- @processing = true
341
- while(run_process && !callbacks.empty?)
342
- if(message_loop.empty? && message_remote.empty?)
343
- wait(:messages_available)
344
- end
345
- msgs = []
346
- msgs.push message_loop.pop unless message_loop.empty?
347
- msgs.push message_remote.pop unless message_remote.empty?
348
- msgs = [msgs].flatten.compact.map do |m|
349
- if(valid_message?(m))
350
- format(m)
358
+ unless(processing)
359
+ begin
360
+ async.receive_messages
361
+ @processing = true
362
+ while(run_process && !callbacks.empty?)
363
+ if(message_loop.empty? && message_remote.empty?)
364
+ wait(:messages_available)
351
365
  end
352
- end.compact
353
- msgs.each do |msg|
354
- if(multiple_callbacks? || respond_to?(:orphan_callback))
355
- valid_callbacks = callbacks.find_all do |name|
356
- callback_supervisor[callback_name(name)].valid?(msg)
366
+ msgs = []
367
+ msgs.push message_loop.pop unless message_loop.empty?
368
+ msgs.push message_remote.pop unless message_remote.empty?
369
+ msgs = [msgs].flatten.compact.map do |m|
370
+ if(valid_message?(m))
371
+ format(m)
357
372
  end
358
- else
359
- valid_callbacks = callbacks
360
- end
361
- if(valid_callbacks.empty?)
362
- warn "Received message was not processed through any callbacks on this source: #{msg}"
363
- orphan_callback(msg) if respond_to?(:orphan_callback)
364
- elsif(valid_callbacks.size > 1 && !multiple_callbacks?)
365
- error "Received message is valid for multiple callbacks but multiple callbacks are disabled: #{msg}"
366
- multiple_callback(msg) if respond_to?(:multiple_callback)
367
- else
368
- valid_callbacks.each do |name|
369
- debug "Dispatching message<#{msg[:message].object_id}> to callback<#{name} (#{callback_name(name)})>"
370
- callback_supervisor[callback_name(name)].async.call(msg)
373
+ end.compact
374
+ msgs.each do |msg|
375
+ if(multiple_callbacks? || respond_to?(:orphan_callback))
376
+ valid_callbacks = callbacks.find_all do |name|
377
+ callback_supervisor[callback_name(name)].valid?(msg)
378
+ end
379
+ else
380
+ valid_callbacks = callbacks
381
+ end
382
+ if(valid_callbacks.empty?)
383
+ warn "Received message was not processed through any callbacks on this source: #{msg}"
384
+ orphan_callback(msg) if respond_to?(:orphan_callback)
385
+ elsif(valid_callbacks.size > 1 && !multiple_callbacks?)
386
+ error "Received message is valid for multiple callbacks but multiple callbacks are disabled: #{msg}"
387
+ multiple_callback(msg) if respond_to?(:multiple_callback)
388
+ else
389
+ valid_callbacks.each do |name|
390
+ debug "Dispatching message<#{msg[:message].object_id}> to callback<#{name} (#{callback_name(name)})>"
391
+ callback_supervisor[callback_name(name)].async.call(msg)
392
+ end
371
393
  end
372
394
  end
373
395
  end
396
+ ensure
397
+ @processing = false
374
398
  end
375
- ensure
376
- @processing = false
399
+ true
400
+ else
401
+ false
377
402
  end
378
- true
379
403
  end
380
404
 
381
405
  # Receive messages from source
@@ -8,6 +8,7 @@ module Carnivore
8
8
  autoload :Logging, 'carnivore/utils/logging'
9
9
  autoload :MessageRegistry, 'carnivore/utils/message_registry'
10
10
  autoload :Smash, 'carnivore/utils/smash'
11
+ autoload :Failure, 'carnivore/utils/failure'
11
12
 
12
13
  extend Params
13
14
  extend Logging
@@ -0,0 +1,31 @@
1
+ require 'carnivore'
2
+
3
+ module Carnivore
4
+ module Utils
5
+
6
+ # Failure utilities
7
+ module Failure
8
+
9
+ # Attempt to execute provided block. If exception is raised, log
10
+ # and pause before retry. Do this until success.
11
+ #
12
+ # @param action [String, Symbol] display name for block
13
+ # @return [Object] result of yielded block
14
+ def execute_and_retry_forever(action)
15
+ begin
16
+ debug "Starting #{action} process"
17
+ result = yield
18
+ debug "Completed #{action} process"
19
+ result
20
+ rescue => e
21
+ error "#{action.to_s.capitalize} process encountered an error: #{e.class} - #{e}"
22
+ debug "#{e.class}: #{e}\n#{e.backtrace.join("\n")}"
23
+ warn "Pausing for 5 seconds then retrying #{action}"
24
+ sleep(5)
25
+ retry
26
+ end
27
+ end
28
+
29
+ end
30
+ end
31
+ end
@@ -75,6 +75,13 @@ module Carnivore
75
75
  value
76
76
  end
77
77
 
78
+ # Convert to Hash
79
+ #
80
+ # @return [Hash]
81
+ def to_hash
82
+ self.to_type_converter(::Hash, :to_hash)
83
+ end
84
+
78
85
  end
79
86
  end
80
87
 
@@ -87,13 +94,42 @@ class Hash
87
94
  #
88
95
  # @return [Smash]
89
96
  def to_smash
90
- ::Smash.new.tap do |smash|
97
+ self.to_type_converter(::Smash, :to_smash)
98
+ end
99
+ alias_method :hulk_smash, :to_smash
100
+
101
+ protected
102
+
103
+ # Convert to type
104
+ #
105
+ # @param type [Class] hash type
106
+ # @param convert_call [Symbol] builtin hash convert
107
+ # @return [Smash]
108
+ def to_type_converter(type, convert_call)
109
+ type.new.tap do |smash|
91
110
  self.each do |k,v|
92
- smash[k.is_a?(Symbol) ? k.to_s : k] = v.is_a?(::Hash) && !v.is_a?(::Smash) ? v.to_smash : v
111
+ smash[k.is_a?(Symbol) ? k.to_s : k] = smash_conversion(v, convert_call)
93
112
  end
94
113
  end
95
114
  end
96
- alias_method :hulk_smash, :to_smash
115
+
116
+ # Convert object to smash if applicable
117
+ #
118
+ # @param obj [Object]
119
+ # @param convert_call [Symbol] builtin hash convert
120
+ # @return [Smash, Object]
121
+ def smash_conversion(obj, convert_call)
122
+ case obj
123
+ when Hash
124
+ obj.send(convert_call)
125
+ when Array
126
+ obj.map do |i|
127
+ smash_conversion(i, convert_call)
128
+ end
129
+ else
130
+ obj
131
+ end
132
+ end
97
133
 
98
134
  end
99
135
 
@@ -1,4 +1,4 @@
1
1
  module Carnivore
2
2
  # Current version of library
3
- VERSION = Gem::Version.new('0.2.10')
3
+ VERSION = Gem::Version.new('0.2.12')
4
4
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: carnivore
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.10
4
+ version: 0.2.12
5
5
  platform: ruby
6
6
  authors:
7
7
  - Chris Roberts
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-08-15 00:00:00.000000000 Z
11
+ date: 2014-10-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: celluloid
@@ -88,6 +88,7 @@ files:
88
88
  - lib/carnivore/spec_helper.rb
89
89
  - lib/carnivore/supervisor.rb
90
90
  - lib/carnivore/utils.rb
91
+ - lib/carnivore/utils/failure.rb
91
92
  - lib/carnivore/utils/logging.rb
92
93
  - lib/carnivore/utils/message_registry.rb
93
94
  - lib/carnivore/utils/params.rb