rabbit_feed 2.3.10 → 2.4.0

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: 5b141fd052af473176629d66db5d86963fe4c403
4
- data.tar.gz: 38f362690f98f836cab2caef5fad99a4a8bca27a
3
+ metadata.gz: b027daa2e82c9b3cca12bcec5df1cf82a9d27eec
4
+ data.tar.gz: a95205ed61bfc7a16b7a6fd2f5e6f33f1f4bf8f3
5
5
  SHA512:
6
- metadata.gz: 88e147ca7b35bf34bb703b9041eb94cbe9d4042e38df8742c55fbea6091c3e2575a64e04214ebe9010a42ae514d60fc1f2c84e6b9a46a4ad6190889722c6141a
7
- data.tar.gz: 30e4acb794167f0d505b65a3f64363f8b66f88ed6ddb77fd1d1b61aed121f44d355f5fe92576880029bc39f364dbbe26c7b378df3f478f4894f861ed0713a34e
6
+ metadata.gz: 2ac970507750197992c5fed39203a12073e0e7ac4a0cce6fe35b5363f6bbc3a52e39c3545601e3d596655378771ec689200eeda0a0de265cf442490d5e44b6dd
7
+ data.tar.gz: 18949fc9dc50a089032465ddf03b293ef4b759d05cd17bcd2a19afa46a86948812cd08fb52aa21b251939bf52ac5bb4f0a047bbd6e49172be6ead5200756d838
data/README.md CHANGED
@@ -270,6 +270,38 @@ Starts a consumer. Note: until you've specified the [event routing](https://gith
270
270
 
271
271
  _This only applies if you've started the consumer with the `--daemon` option._
272
272
 
273
+ ### Console Consumer
274
+
275
+ bundle exec bin/rabbit_feed console --environment development --config spec/fixtures/configuration.yml --logfile development.log --pidfile rabbit_feed.pid --verbose
276
+
277
+ The console consumer will accept any event from any application and will print the event metadata and payload to the console. This is useful during development to get instant feedback on the events being published.
278
+
279
+ #### Example Console Output
280
+
281
+ ```
282
+ RabbitFeed console starting at 2016-02-08 16:22:22 UTC...
283
+ Environment: development
284
+ Queue: development.rabbit_feed_console
285
+ There are currently 4 message(s) in the console's queue.
286
+ Would you like to purge the queue before proceeding? (y/N)>
287
+ n
288
+ Ready. Press CTRL+C to exit.
289
+ ----------------------------user_creates_beaver: 2016-02-08 16:19:30 UTC----------------------------
290
+ #Event metadata
291
+ application: rails_app
292
+ created_at_utc: 2016-02-08T16:19:30.530210Z
293
+ environment: development
294
+ host: localhost
295
+ name: user_creates_beaver
296
+ schema_version: 2.0.0
297
+ version: 1.0.0
298
+ ****************************************************************************************************
299
+ #Event payload
300
+ beaver_name: 02/08/16 16:19:30
301
+ ----------------------------------------------------------------------------------------------------
302
+ 1 events received.
303
+ ```
304
+
273
305
  ## Event Definitions DSL
274
306
 
275
307
  Provides a means to define all events that are published by an application. Defines the event names and the payload associated with each event. The DSL is converted into a schema that is serialized along with the event payload, meaning the events are self-describing. This is accomplished using Apache [Avro](http://avro.apache.org/docs/current/). This also validates the event payload against its schema before it is published.
data/Rakefile CHANGED
@@ -6,12 +6,12 @@ task :default => [:all]
6
6
 
7
7
  desc 'Run specs and features'
8
8
  RSpec::Core::RakeTask.new(:all) do |t|
9
- t.pattern = './spec/{**/*_spec.rb,features/**/*.feature}'
9
+ t.pattern = "spec/{**/*_spec.rb,features/**/*.feature}"
10
10
  end
11
11
 
12
12
  desc 'Run specs'
13
13
  RSpec::Core::RakeTask.new(:spec) do |t|
14
- t.pattern = './spec/{**/*_spec.rb}'
14
+ t.pattern = "spec/**/*_spec.rb"
15
15
  end
16
16
 
17
17
  def gemspec
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: ../../
3
3
  specs:
4
- rabbit_feed (2.3.10)
4
+ rabbit_feed (2.4.0)
5
5
  activemodel (>= 3.2.0, < 5.0.0)
6
6
  activesupport (>= 3.2.0, < 5.0.0)
7
7
  avro (>= 1.5.4, < 1.8.0)
@@ -59,4 +59,4 @@ DEPENDENCIES
59
59
  rspec
60
60
 
61
61
  BUNDLED WITH
62
- 1.10.5
62
+ 1.10.6
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: ../../
3
3
  specs:
4
- rabbit_feed (2.3.10)
4
+ rabbit_feed (2.4.0)
5
5
  activemodel (>= 3.2.0, < 5.0.0)
6
6
  activesupport (>= 3.2.0, < 5.0.0)
7
7
  avro (>= 1.5.4, < 1.8.0)
@@ -189,4 +189,4 @@ DEPENDENCIES
189
189
  unicorn
190
190
 
191
191
  BUNDLED WITH
192
- 1.10.5
192
+ 1.10.6
@@ -16,10 +16,10 @@ module RabbitFeed
16
16
 
17
17
  attr_reader :command, :options
18
18
  validates_presence_of :command, :options
19
- validates :command, inclusion: { in: %w(consume produce shutdown), message: "%{value} is not a valid command" }
19
+ validates :command, inclusion: { in: %w(consume produce shutdown console), message: "%{value} is not a valid command" }
20
20
  validate :log_file_path_exists
21
21
  validate :config_file_exists
22
- validate :require_path_valid
22
+ validate :require_path_valid, unless: :console?
23
23
  validate :pidfile_path_exists, if: :daemonize?
24
24
  validate :environment_specified
25
25
 
@@ -32,7 +32,7 @@ module RabbitFeed
32
32
 
33
33
  set_logging
34
34
  set_configuration
35
- load_dependancies
35
+ load_dependancies unless console?
36
36
  end
37
37
  end
38
38
 
@@ -73,6 +73,12 @@ module RabbitFeed
73
73
  RabbitFeed::Consumer.run
74
74
  end
75
75
 
76
+ def console
77
+ require_relative 'console_consumer'
78
+ ConsoleConsumer.init
79
+ consume
80
+ end
81
+
76
82
  def shutdown
77
83
  `kill -TERM #{pid}`
78
84
  end
@@ -127,6 +133,10 @@ module RabbitFeed
127
133
  options[:daemon]
128
134
  end
129
135
 
136
+ def console?
137
+ command == 'console'
138
+ end
139
+
130
140
  def verbose?
131
141
  options[:verbose]
132
142
  end
@@ -0,0 +1,119 @@
1
+ module RabbitFeed
2
+ module ConsoleConsumer
3
+ extend self
4
+
5
+ APPLICATION_NAME = 'rabbit_feed_console'
6
+
7
+ def init
8
+ @event_count = 0
9
+ set_application
10
+ route_all_events
11
+ puts welcome_message
12
+ ask_to_purge_queue unless queue_empty?
13
+ puts "Ready. Press CTRL+C to exit."
14
+ end
15
+
16
+ def formatted event
17
+ Formatter.new(event).to_s
18
+ end
19
+
20
+ def event_count_message
21
+ "#{@event_count} events received."
22
+ end
23
+
24
+ def increment_event_count
25
+ @event_count += 1
26
+ end
27
+
28
+ private
29
+
30
+ def welcome_message
31
+ """RabbitFeed console starting at #{Time.now.utc}...
32
+ Environment: #{RabbitFeed.environment}
33
+ Queue: #{RabbitFeed.configuration.queue}
34
+ """
35
+ end
36
+
37
+ def queue_empty?
38
+ ConsumerConnection.instance.queue_depth.zero?
39
+ end
40
+
41
+ def ask_to_purge_queue
42
+ puts "There are currently #{ConsumerConnection.instance.queue_depth} message(s) in the console's queue.\n"+
43
+ "Would you like to purge the queue before proceeding? (y/N)>"
44
+ response = STDIN.gets.chomp
45
+ purge_queue if response == 'y'
46
+ end
47
+
48
+ def purge_queue
49
+ ConsumerConnection.instance.purge_queue
50
+ puts "Queue purged."
51
+ end
52
+
53
+ def route_all_events
54
+ scope = self
55
+ EventRouting do
56
+ accept_from(:any) do
57
+ event(:any) do |event|
58
+ scope.increment_event_count
59
+ puts (scope.formatted event)
60
+ puts scope.event_count_message
61
+ end
62
+ end
63
+ end
64
+ end
65
+
66
+ def set_application
67
+ RabbitFeed.application = APPLICATION_NAME
68
+ end
69
+
70
+ class Formatter
71
+
72
+ BORDER_WIDTH = 100
73
+ BORDER_CHAR = "-"
74
+ DIVIDER_CHAR = "*"
75
+ NEWLINE = "\n"
76
+
77
+ attr_reader :event
78
+
79
+ def initialize event
80
+ @event = event
81
+ end
82
+
83
+ def to_s
84
+ [header, metadata, divider, payload, footer].join(NEWLINE)
85
+ end
86
+
87
+ private
88
+
89
+ def header
90
+ event_detail = "#{event.name}: #{event.created_at_utc}"
91
+ border_filler = BORDER_CHAR*((BORDER_WIDTH - event_detail.length)/2)
92
+ border_filler+event_detail+border_filler
93
+ end
94
+
95
+ def footer
96
+ BORDER_CHAR*BORDER_WIDTH
97
+ end
98
+
99
+ def metadata
100
+ pretty_print_hash 'Event metadata', event.metadata
101
+ end
102
+
103
+ def divider
104
+ DIVIDER_CHAR*BORDER_WIDTH
105
+ end
106
+
107
+ def payload
108
+ pretty_print_hash 'Event payload', event.payload
109
+ end
110
+
111
+ def pretty_print_hash description, hash
112
+ '#' + description + NEWLINE +
113
+ hash.keys.sort.map do |key|
114
+ "#{key}: #{hash[key]}"
115
+ end.join(NEWLINE)
116
+ end
117
+ end
118
+ end
119
+ end
@@ -45,6 +45,14 @@ module RabbitFeed
45
45
  end
46
46
  end
47
47
 
48
+ def queue_depth
49
+ @queue.message_count
50
+ end
51
+
52
+ def purge_queue
53
+ @queue.purge
54
+ end
55
+
48
56
  private
49
57
 
50
58
  attr_reader :queue
@@ -1,3 +1,3 @@
1
1
  module RabbitFeed
2
- VERSION = '2.3.10'
2
+ VERSION = '2.4.0'
3
3
  end
@@ -1,3 +1,10 @@
1
+ development:
2
+ host: localhost
3
+ port: 5672
4
+ user: guest
5
+ password: guest
6
+ application: rabbit_feed
7
+ auto_delete_queue: false
1
8
  test:
2
9
  host: localhost
3
10
  port: 5672
@@ -0,0 +1,60 @@
1
+ require 'rabbit_feed/console_consumer'
2
+
3
+ module RabbitFeed
4
+ describe ConsoleConsumer do
5
+ let(:purge) { 'n' }
6
+ let(:queue_depth) { 0 }
7
+ let(:connection) { double(:connection, queue_depth: queue_depth) }
8
+ before do
9
+ allow(ConsumerConnection).to receive(:instance).and_return(connection)
10
+ allow(STDIN).to receive(:gets).and_return(purge)
11
+ end
12
+
13
+ describe '#init' do
14
+
15
+ it 'prints a welcome message' do
16
+ expect{ subject.init }.to output(
17
+ /RabbitFeed console starting at .* UTC\.\.\.
18
+ Environment: test
19
+ Queue: test\.rabbit_feed_console
20
+ Ready\. Press CTRL\+C to exit\./).to_stdout
21
+ end
22
+
23
+ context 'when there are events on the rabbit_feed_console queue' do
24
+ let(:queue_depth) { 1 }
25
+
26
+ it 'asks to purge the queue' do
27
+ expect{ subject.init }.to output(
28
+ /There are currently 1 message\(s\) in the console's queue\.
29
+ Would you like to purge the queue before proceeding\? \(y\/N\)>/).to_stdout
30
+ end
31
+
32
+ context 'when the user wishes to purge the queue' do
33
+ let(:purge) { 'y' }
34
+
35
+ it 'purges the queue' do
36
+ expect(connection).to receive(:purge_queue)
37
+ expect{ subject.init }.to output(/Queue purged\./).to_stdout
38
+ end
39
+ end
40
+ end
41
+ end
42
+
43
+ describe 'receiving an event' do
44
+ let(:event) { Event.new({name: 'name'},{key: :value}) }
45
+ before { subject.init }
46
+
47
+ it 'prints the event' do
48
+ expect{ rabbit_feed_consumer.consume_event event }.to output(
49
+ /-----------------------------------------------name: -----------------------------------------------
50
+ #Event metadata
51
+ name: name
52
+ (\*)+
53
+ #Event payload
54
+ key: value
55
+ ----------------------------------------------------------------------------------------------------
56
+ 1 events received\./).to_stdout
57
+ end
58
+ end
59
+ end
60
+ end
data/spec/spec_helper.rb CHANGED
@@ -31,7 +31,8 @@ RSpec.configure do |config|
31
31
  end
32
32
 
33
33
  config.after(connectivity: true) do
34
- Thread.kill @consumer_thread if @consumer_thread.present?
34
+ @consumer_thread.kill if @consumer_thread.present?
35
+ @consumer_thread.join if @consumer_thread.present?
35
36
  end
36
37
 
37
38
  RabbitFeed::TestingSupport.include_support config
@@ -39,6 +40,7 @@ end
39
40
 
40
41
  def reset_environment
41
42
  RabbitFeed.log = RabbitFeed.default_logger
43
+ RabbitFeed.application = nil
42
44
  RabbitFeed.environment = 'test'
43
45
  RabbitFeed.configuration_file_path = 'spec/fixtures/configuration.yml'
44
46
  RabbitFeed.instance_variable_set('@configuration', nil)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rabbit_feed
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.3.10
4
+ version: 2.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Simply Business
@@ -313,6 +313,7 @@ files:
313
313
  - lib/rabbit_feed/client.rb
314
314
  - lib/rabbit_feed/configuration.rb
315
315
  - lib/rabbit_feed/connection.rb
316
+ - lib/rabbit_feed/console_consumer.rb
316
317
  - lib/rabbit_feed/consumer.rb
317
318
  - lib/rabbit_feed/consumer_connection.rb
318
319
  - lib/rabbit_feed/event.rb
@@ -337,6 +338,7 @@ files:
337
338
  - spec/fixtures/configuration.yml
338
339
  - spec/lib/rabbit_feed/client_spec.rb
339
340
  - spec/lib/rabbit_feed/configuration_spec.rb
341
+ - spec/lib/rabbit_feed/console_consumer_spec.rb
340
342
  - spec/lib/rabbit_feed/consumer_connection_spec.rb
341
343
  - spec/lib/rabbit_feed/event_definitions_spec.rb
342
344
  - spec/lib/rabbit_feed/event_routing_spec.rb
@@ -377,6 +379,7 @@ test_files:
377
379
  - spec/fixtures/configuration.yml
378
380
  - spec/lib/rabbit_feed/client_spec.rb
379
381
  - spec/lib/rabbit_feed/configuration_spec.rb
382
+ - spec/lib/rabbit_feed/console_consumer_spec.rb
380
383
  - spec/lib/rabbit_feed/consumer_connection_spec.rb
381
384
  - spec/lib/rabbit_feed/event_definitions_spec.rb
382
385
  - spec/lib/rabbit_feed/event_routing_spec.rb