rabbit_feed 0.3.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (145) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +11 -0
  3. data/.rspec +3 -0
  4. data/Brewfile +4 -0
  5. data/DEVELOPING.md +140 -0
  6. data/Gemfile +15 -0
  7. data/Gemfile.lock +121 -0
  8. data/LICENSE.txt +9 -0
  9. data/README.md +304 -0
  10. data/Rakefile +30 -0
  11. data/bin/bundle +3 -0
  12. data/bin/rabbit_feed +11 -0
  13. data/example/non_rails_app/.rspec +1 -0
  14. data/example/non_rails_app/Gemfile +7 -0
  15. data/example/non_rails_app/Gemfile.lock +56 -0
  16. data/example/non_rails_app/Rakefile +5 -0
  17. data/example/non_rails_app/bin/benchmark +63 -0
  18. data/example/non_rails_app/bin/bundle +3 -0
  19. data/example/non_rails_app/config/rabbit_feed.yml +8 -0
  20. data/example/non_rails_app/lib/non_rails_app.rb +32 -0
  21. data/example/non_rails_app/lib/non_rails_app/event_handler.rb +10 -0
  22. data/example/non_rails_app/log/.keep +0 -0
  23. data/example/non_rails_app/spec/lib/non_rails_app/event_handler_spec.rb +14 -0
  24. data/example/non_rails_app/spec/lib/non_rails_app/event_routing_spec.rb +14 -0
  25. data/example/non_rails_app/spec/spec_helper.rb +31 -0
  26. data/example/non_rails_app/tmp/pids/.keep +0 -0
  27. data/example/rails_app/.gitignore +17 -0
  28. data/example/rails_app/.node-version +1 -0
  29. data/example/rails_app/.rspec +1 -0
  30. data/example/rails_app/Gemfile +36 -0
  31. data/example/rails_app/Gemfile.lock +173 -0
  32. data/example/rails_app/README.rdoc +28 -0
  33. data/example/rails_app/Rakefile +6 -0
  34. data/example/rails_app/app/assets/images/.keep +0 -0
  35. data/example/rails_app/app/assets/javascripts/application.js +16 -0
  36. data/example/rails_app/app/assets/javascripts/beavers.js.coffee +3 -0
  37. data/example/rails_app/app/assets/stylesheets/application.css +15 -0
  38. data/example/rails_app/app/assets/stylesheets/beavers.css.scss +3 -0
  39. data/example/rails_app/app/assets/stylesheets/scaffolds.css.scss +69 -0
  40. data/example/rails_app/app/controllers/application_controller.rb +5 -0
  41. data/example/rails_app/app/controllers/beavers_controller.rb +81 -0
  42. data/example/rails_app/app/controllers/concerns/.keep +0 -0
  43. data/example/rails_app/app/helpers/application_helper.rb +2 -0
  44. data/example/rails_app/app/helpers/beavers_helper.rb +2 -0
  45. data/example/rails_app/app/mailers/.keep +0 -0
  46. data/example/rails_app/app/models/.keep +0 -0
  47. data/example/rails_app/app/models/beaver.rb +2 -0
  48. data/example/rails_app/app/models/concerns/.keep +0 -0
  49. data/example/rails_app/app/views/beavers/_form.html.erb +21 -0
  50. data/example/rails_app/app/views/beavers/edit.html.erb +6 -0
  51. data/example/rails_app/app/views/beavers/index.html.erb +25 -0
  52. data/example/rails_app/app/views/beavers/index.json.jbuilder +4 -0
  53. data/example/rails_app/app/views/beavers/new.html.erb +5 -0
  54. data/example/rails_app/app/views/beavers/show.html.erb +9 -0
  55. data/example/rails_app/app/views/beavers/show.json.jbuilder +1 -0
  56. data/example/rails_app/app/views/layouts/application.html.erb +14 -0
  57. data/example/rails_app/bin/bundle +3 -0
  58. data/example/rails_app/bin/rails +4 -0
  59. data/example/rails_app/bin/rake +4 -0
  60. data/example/rails_app/config.ru +4 -0
  61. data/example/rails_app/config/application.rb +25 -0
  62. data/example/rails_app/config/boot.rb +4 -0
  63. data/example/rails_app/config/database.yml +22 -0
  64. data/example/rails_app/config/environment.rb +5 -0
  65. data/example/rails_app/config/environments/development.rb +83 -0
  66. data/example/rails_app/config/environments/test.rb +39 -0
  67. data/example/rails_app/config/initializers/backtrace_silencers.rb +7 -0
  68. data/example/rails_app/config/initializers/cookies_serializer.rb +3 -0
  69. data/example/rails_app/config/initializers/filter_parameter_logging.rb +4 -0
  70. data/example/rails_app/config/initializers/inflections.rb +16 -0
  71. data/example/rails_app/config/initializers/mime_types.rb +4 -0
  72. data/example/rails_app/config/initializers/rabbit_feed.rb +43 -0
  73. data/example/rails_app/config/initializers/session_store.rb +3 -0
  74. data/example/rails_app/config/initializers/wrap_parameters.rb +14 -0
  75. data/example/rails_app/config/locales/en.yml +23 -0
  76. data/example/rails_app/config/rabbit_feed.yml +8 -0
  77. data/example/rails_app/config/routes.rb +58 -0
  78. data/example/rails_app/config/secrets.yml +18 -0
  79. data/example/rails_app/config/unicorn.rb +4 -0
  80. data/example/rails_app/db/migrate/20140424102400_create_beavers.rb +9 -0
  81. data/example/rails_app/db/schema.rb +22 -0
  82. data/example/rails_app/db/seeds.rb +7 -0
  83. data/example/rails_app/lib/assets/.keep +0 -0
  84. data/example/rails_app/lib/event_handler.rb +7 -0
  85. data/example/rails_app/lib/tasks/.keep +0 -0
  86. data/example/rails_app/log/.keep +0 -0
  87. data/example/rails_app/public/404.html +67 -0
  88. data/example/rails_app/public/422.html +67 -0
  89. data/example/rails_app/public/500.html +66 -0
  90. data/example/rails_app/public/favicon.ico +0 -0
  91. data/example/rails_app/public/robots.txt +5 -0
  92. data/example/rails_app/spec/controllers/beavers_controller_spec.rb +32 -0
  93. data/example/rails_app/spec/event_routing_spec.rb +15 -0
  94. data/example/rails_app/spec/spec_helper.rb +51 -0
  95. data/example/rails_app/test/controllers/.keep +0 -0
  96. data/example/rails_app/test/controllers/beavers_controller_test.rb +49 -0
  97. data/example/rails_app/test/fixtures/.keep +0 -0
  98. data/example/rails_app/test/fixtures/beavers.yml +7 -0
  99. data/example/rails_app/test/helpers/.keep +0 -0
  100. data/example/rails_app/test/helpers/beavers_helper_test.rb +4 -0
  101. data/example/rails_app/test/integration/.keep +0 -0
  102. data/example/rails_app/test/mailers/.keep +0 -0
  103. data/example/rails_app/test/models/.keep +0 -0
  104. data/example/rails_app/test/models/beaver_test.rb +7 -0
  105. data/example/rails_app/test/test_helper.rb +13 -0
  106. data/example/rails_app/tmp/pids/.keep +0 -0
  107. data/example/rails_app/vendor/assets/javascripts/.keep +0 -0
  108. data/example/rails_app/vendor/assets/stylesheets/.keep +0 -0
  109. data/lib/dsl.rb +9 -0
  110. data/lib/rabbit_feed.rb +41 -0
  111. data/lib/rabbit_feed/client.rb +181 -0
  112. data/lib/rabbit_feed/configuration.rb +50 -0
  113. data/lib/rabbit_feed/connection_concern.rb +95 -0
  114. data/lib/rabbit_feed/consumer.rb +14 -0
  115. data/lib/rabbit_feed/consumer_connection.rb +108 -0
  116. data/lib/rabbit_feed/event.rb +43 -0
  117. data/lib/rabbit_feed/event_definitions.rb +98 -0
  118. data/lib/rabbit_feed/event_routing.rb +90 -0
  119. data/lib/rabbit_feed/producer.rb +47 -0
  120. data/lib/rabbit_feed/producer_connection.rb +65 -0
  121. data/lib/rabbit_feed/testing_support/rspec_matchers/publish_event.rb +90 -0
  122. data/lib/rabbit_feed/testing_support/testing_helpers.rb +16 -0
  123. data/lib/rabbit_feed/version.rb +3 -0
  124. data/logo.png +0 -0
  125. data/rabbit_feed.gemspec +35 -0
  126. data/run_benchmark +35 -0
  127. data/run_example +62 -0
  128. data/run_recovery_test +26 -0
  129. data/spec/features/connectivity.feature +13 -0
  130. data/spec/features/step_definitions/connectivity_steps.rb +96 -0
  131. data/spec/fixtures/configuration.yml +14 -0
  132. data/spec/lib/rabbit_feed/client_spec.rb +116 -0
  133. data/spec/lib/rabbit_feed/configuration_spec.rb +121 -0
  134. data/spec/lib/rabbit_feed/connection_concern_spec.rb +116 -0
  135. data/spec/lib/rabbit_feed/consumer_connection_spec.rb +85 -0
  136. data/spec/lib/rabbit_feed/event_definitions_spec.rb +139 -0
  137. data/spec/lib/rabbit_feed/event_routing_spec.rb +121 -0
  138. data/spec/lib/rabbit_feed/event_spec.rb +33 -0
  139. data/spec/lib/rabbit_feed/producer_connection_spec.rb +72 -0
  140. data/spec/lib/rabbit_feed/producer_spec.rb +57 -0
  141. data/spec/lib/rabbit_feed/testing_support/rspec_matchers/publish_event_spec.rb +60 -0
  142. data/spec/lib/rabbit_feed/testing_support/testing_helper_spec.rb +34 -0
  143. data/spec/spec_helper.rb +58 -0
  144. data/spec/support/shared_examples_for_connections.rb +40 -0
  145. metadata +305 -0
@@ -0,0 +1,67 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>The change you wanted was rejected (422)</title>
5
+ <meta name="viewport" content="width=device-width,initial-scale=1">
6
+ <style>
7
+ body {
8
+ background-color: #EFEFEF;
9
+ color: #2E2F30;
10
+ text-align: center;
11
+ font-family: arial, sans-serif;
12
+ margin: 0;
13
+ }
14
+
15
+ div.dialog {
16
+ width: 95%;
17
+ max-width: 33em;
18
+ margin: 4em auto 0;
19
+ }
20
+
21
+ div.dialog > div {
22
+ border: 1px solid #CCC;
23
+ border-right-color: #999;
24
+ border-left-color: #999;
25
+ border-bottom-color: #BBB;
26
+ border-top: #B00100 solid 4px;
27
+ border-top-left-radius: 9px;
28
+ border-top-right-radius: 9px;
29
+ background-color: white;
30
+ padding: 7px 12% 0;
31
+ box-shadow: 0 3px 8px rgba(50, 50, 50, 0.17);
32
+ }
33
+
34
+ h1 {
35
+ font-size: 100%;
36
+ color: #730E15;
37
+ line-height: 1.5em;
38
+ }
39
+
40
+ div.dialog > p {
41
+ margin: 0 0 1em;
42
+ padding: 1em;
43
+ background-color: #F7F7F7;
44
+ border: 1px solid #CCC;
45
+ border-right-color: #999;
46
+ border-left-color: #999;
47
+ border-bottom-color: #999;
48
+ border-bottom-left-radius: 4px;
49
+ border-bottom-right-radius: 4px;
50
+ border-top-color: #DADADA;
51
+ color: #666;
52
+ box-shadow: 0 3px 8px rgba(50, 50, 50, 0.17);
53
+ }
54
+ </style>
55
+ </head>
56
+
57
+ <body>
58
+ <!-- This file lives in public/422.html -->
59
+ <div class="dialog">
60
+ <div>
61
+ <h1>The change you wanted was rejected.</h1>
62
+ <p>Maybe you tried to change something you didn't have access to.</p>
63
+ </div>
64
+ <p>If you are the application owner check the logs for more information.</p>
65
+ </div>
66
+ </body>
67
+ </html>
@@ -0,0 +1,66 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>We're sorry, but something went wrong (500)</title>
5
+ <meta name="viewport" content="width=device-width,initial-scale=1">
6
+ <style>
7
+ body {
8
+ background-color: #EFEFEF;
9
+ color: #2E2F30;
10
+ text-align: center;
11
+ font-family: arial, sans-serif;
12
+ margin: 0;
13
+ }
14
+
15
+ div.dialog {
16
+ width: 95%;
17
+ max-width: 33em;
18
+ margin: 4em auto 0;
19
+ }
20
+
21
+ div.dialog > div {
22
+ border: 1px solid #CCC;
23
+ border-right-color: #999;
24
+ border-left-color: #999;
25
+ border-bottom-color: #BBB;
26
+ border-top: #B00100 solid 4px;
27
+ border-top-left-radius: 9px;
28
+ border-top-right-radius: 9px;
29
+ background-color: white;
30
+ padding: 7px 12% 0;
31
+ box-shadow: 0 3px 8px rgba(50, 50, 50, 0.17);
32
+ }
33
+
34
+ h1 {
35
+ font-size: 100%;
36
+ color: #730E15;
37
+ line-height: 1.5em;
38
+ }
39
+
40
+ div.dialog > p {
41
+ margin: 0 0 1em;
42
+ padding: 1em;
43
+ background-color: #F7F7F7;
44
+ border: 1px solid #CCC;
45
+ border-right-color: #999;
46
+ border-left-color: #999;
47
+ border-bottom-color: #999;
48
+ border-bottom-left-radius: 4px;
49
+ border-bottom-right-radius: 4px;
50
+ border-top-color: #DADADA;
51
+ color: #666;
52
+ box-shadow: 0 3px 8px rgba(50, 50, 50, 0.17);
53
+ }
54
+ </style>
55
+ </head>
56
+
57
+ <body>
58
+ <!-- This file lives in public/500.html -->
59
+ <div class="dialog">
60
+ <div>
61
+ <h1>We're sorry, but something went wrong.</h1>
62
+ </div>
63
+ <p>If you are the application owner check the logs for more information.</p>
64
+ </div>
65
+ </body>
66
+ </html>
File without changes
@@ -0,0 +1,5 @@
1
+ # See http://www.robotstxt.org/robotstxt.html for documentation on how to use the robots.txt file
2
+ #
3
+ # To ban all spiders from the entire site uncomment the next two lines:
4
+ # User-agent: *
5
+ # Disallow: /
@@ -0,0 +1,32 @@
1
+ require 'spec_helper'
2
+
3
+ describe BeaversController do
4
+
5
+ describe 'POST create' do
6
+ it 'publishes a create event' do
7
+ expect{
8
+ post :create, beaver: { name: 'beaver' }
9
+ }.to publish_event('user_creates_beaver', { 'beaver_name' => 'beaver' })
10
+ end
11
+ end
12
+
13
+ context 'for an existing beaver' do
14
+ let(:beaver) { Beaver.create name: 'beaver' }
15
+
16
+ describe 'PUT update' do
17
+ it 'publishes an update event' do
18
+ expect{
19
+ put :update, id: beaver.id, beaver: { name: 'beaver_updated' }
20
+ }.to publish_event('user_updates_beaver', { 'beaver_name' => 'beaver_updated' })
21
+ end
22
+ end
23
+
24
+ describe 'DELETE destroy' do
25
+ it 'publishes an delete event' do
26
+ expect{
27
+ delete :destroy, id: beaver.id
28
+ }.to publish_event('user_deletes_beaver', { 'beaver_name' => 'beaver' })
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,15 @@
1
+ require_relative 'spec_helper'
2
+ require_relative '../config/initializers/rabbit_feed'
3
+
4
+ module RailsApp
5
+ describe 'Event Routing' do
6
+
7
+ include RabbitFeed::TestingSupport::TestingHelpers
8
+
9
+ let(:event) { {'application' => 'non_rails_app', 'name' => 'application_acknowledges_event'} }
10
+ it 'routes events correctly' do
11
+ expect(::EventHandler).to receive(:handle_event)
12
+ rabbit_feed_consumer.consume_event(event)
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,51 @@
1
+ # This file is copied to spec/ when you run 'rails generate rspec:install'
2
+ ENV["RAILS_ENV"] ||= 'test'
3
+ require File.expand_path("../../config/environment", __FILE__)
4
+ require 'rspec/rails'
5
+
6
+ # Requires supporting ruby files with custom matchers and macros, etc, in
7
+ # spec/support/ and its subdirectories. Files matching `spec/**/*_spec.rb` are
8
+ # run as spec files by default. This means that files in spec/support that end
9
+ # in _spec.rb will both be required and run as specs, causing the specs to be
10
+ # run twice. It is recommended that you do not name files matching this glob to
11
+ # end with _spec.rb. You can configure this pattern with with the --pattern
12
+ # option on the command line or in ~/.rspec, .rspec or `.rspec-local`.
13
+ Dir[Rails.root.join("spec/support/**/*.rb")].each { |f| require f }
14
+
15
+ # Checks for pending migrations before tests are run.
16
+ # If you are not using ActiveRecord, you can remove this line.
17
+ ActiveRecord::Migration.check_pending! if defined?(ActiveRecord::Migration)
18
+
19
+ RSpec.configure do |config|
20
+ # ## Mock Framework
21
+ #
22
+ # If you prefer to use mocha, flexmock or RR, uncomment the appropriate line:
23
+ #
24
+ # config.mock_with :mocha
25
+ # config.mock_with :flexmock
26
+ # config.mock_with :rr
27
+
28
+ # Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
29
+ config.fixture_path = "#{::Rails.root}/spec/fixtures"
30
+
31
+ # If you're not using ActiveRecord, or you'd prefer not to run each of your
32
+ # examples within a transaction, remove the following line or assign false
33
+ # instead of true.
34
+ config.use_transactional_fixtures = true
35
+
36
+ # Run specs in random order to surface order dependencies. If you find an
37
+ # order dependency and want to debug it, you can fix the order by providing
38
+ # the seed, which is printed after each run.
39
+ # --seed 1234
40
+ config.order = "random"
41
+
42
+ config.mock_with :rspec do |c|
43
+ c.syntax = [:should, :expect]
44
+ end
45
+
46
+ config.before :each do
47
+ RabbitFeed::Producer.stub!
48
+ end
49
+
50
+ config.include(RabbitFeed::TestingSupport::RSpecMatchers)
51
+ end
File without changes
@@ -0,0 +1,49 @@
1
+ require 'test_helper'
2
+
3
+ class BeaversControllerTest < ActionController::TestCase
4
+ setup do
5
+ @beaver = beavers(:one)
6
+ end
7
+
8
+ test "should get index" do
9
+ get :index
10
+ assert_response :success
11
+ assert_not_nil assigns(:beavers)
12
+ end
13
+
14
+ test "should get new" do
15
+ get :new
16
+ assert_response :success
17
+ end
18
+
19
+ test "should create beaver" do
20
+ assert_difference('Beaver.count') do
21
+ post :create, beaver: { name: @beaver.name }
22
+ end
23
+
24
+ assert_redirected_to beaver_path(assigns(:beaver))
25
+ end
26
+
27
+ test "should show beaver" do
28
+ get :show, id: @beaver
29
+ assert_response :success
30
+ end
31
+
32
+ test "should get edit" do
33
+ get :edit, id: @beaver
34
+ assert_response :success
35
+ end
36
+
37
+ test "should update beaver" do
38
+ patch :update, id: @beaver, beaver: { name: @beaver.name }
39
+ assert_redirected_to beaver_path(assigns(:beaver))
40
+ end
41
+
42
+ test "should destroy beaver" do
43
+ assert_difference('Beaver.count', -1) do
44
+ delete :destroy, id: @beaver
45
+ end
46
+
47
+ assert_redirected_to beavers_path
48
+ end
49
+ end
File without changes
@@ -0,0 +1,7 @@
1
+ # Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
2
+
3
+ one:
4
+ name: MyString
5
+
6
+ two:
7
+ name: MyString
File without changes
@@ -0,0 +1,4 @@
1
+ require 'test_helper'
2
+
3
+ class BeaversHelperTest < ActionView::TestCase
4
+ end
File without changes
File without changes
File without changes
@@ -0,0 +1,7 @@
1
+ require 'test_helper'
2
+
3
+ class BeaverTest < ActiveSupport::TestCase
4
+ # test "the truth" do
5
+ # assert true
6
+ # end
7
+ end
@@ -0,0 +1,13 @@
1
+ ENV['RAILS_ENV'] ||= 'test'
2
+ require File.expand_path('../../config/environment', __FILE__)
3
+ require 'rails/test_help'
4
+
5
+ class ActiveSupport::TestCase
6
+ # Setup all fixtures in test/fixtures/*.yml for all tests in alphabetical order.
7
+ #
8
+ # Note: You'll currently still have to declare fixtures explicitly in integration tests
9
+ # -- they do not yet inherit this setting
10
+ fixtures :all
11
+
12
+ # Add more helper methods to be used by all tests here...
13
+ end
File without changes
File without changes
File without changes
data/lib/dsl.rb ADDED
@@ -0,0 +1,9 @@
1
+ def EventRouting &block
2
+ RabbitFeed::Consumer.event_routing ||= RabbitFeed::EventRouting.new
3
+ RabbitFeed::Consumer.event_routing.instance_eval &block
4
+ end
5
+
6
+ def EventDefinitions &block
7
+ RabbitFeed::Producer.event_definitions ||= RabbitFeed::EventDefinitions.new
8
+ RabbitFeed::Producer.event_definitions.instance_eval &block
9
+ end
@@ -0,0 +1,41 @@
1
+ require 'active_support/all'
2
+ require 'active_model'
3
+ require 'avro'
4
+ require 'bunny'
5
+ require 'connection_pool'
6
+ require 'yaml'
7
+ require 'dsl'
8
+ require 'rabbit_feed/version'
9
+ require 'rabbit_feed/client'
10
+ require 'rabbit_feed/configuration'
11
+ require 'rabbit_feed/connection_concern'
12
+ require 'rabbit_feed/event'
13
+ require 'rabbit_feed/consumer_connection'
14
+ require 'rabbit_feed/consumer'
15
+ require 'rabbit_feed/event_routing'
16
+ require 'rabbit_feed/producer_connection'
17
+ require 'rabbit_feed/producer'
18
+ require 'rabbit_feed/event_definitions'
19
+ require 'rabbit_feed/testing_support/rspec_matchers/publish_event'
20
+ require 'rabbit_feed/testing_support/testing_helpers'
21
+
22
+ module RabbitFeed
23
+ extend self
24
+ class Error < StandardError; end
25
+ class ConfigurationError < Error; end
26
+ class RoutingError < Error; end
27
+ class ReturnedMessageError < Error; end
28
+
29
+ attr_accessor :log, :environment, :configuration_file_path
30
+
31
+ def configuration
32
+ raise ConfigurationError.new 'rabbit feed log is not set' unless log.present?
33
+ @configuration ||= (Configuration.load RabbitFeed.configuration_file_path, RabbitFeed.environment)
34
+ end
35
+
36
+ def exception_notify exception
37
+ if defined? Airbrake
38
+ (Airbrake.notify_or_ignore exception) if Airbrake.configuration.public?
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,181 @@
1
+ require 'optparse'
2
+ require 'pidfile'
3
+
4
+ module RabbitFeed
5
+ class Client
6
+ include ActiveModel::Validations
7
+
8
+ DEFAULTS = {
9
+ payload: 'test',
10
+ require_path: '.',
11
+ config_file: 'config/rabbit_feed.yml',
12
+ logfile: 'log/rabbit_feed.log',
13
+ pidfile: 'tmp/pids/rabbit_feed.pid',
14
+ }
15
+ DEFAULTS.freeze
16
+
17
+ attr_reader :command, :options
18
+ validates_presence_of :command, :options
19
+ validates :command, inclusion: { in: %w(consume produce), message: "%{value} is not a valid command" }
20
+ validate :log_file_path_exists
21
+ validate :config_file_exists
22
+ validate :require_path_valid
23
+ validate :pidfile_path_exists, if: :daemonize?
24
+ validate :environment_specified
25
+
26
+ def initialize arguments=ARGV
27
+ @command = arguments[0]
28
+ @options = parse_options arguments
29
+ validate!
30
+
31
+ set_logging
32
+ set_configuration
33
+ load_dependancies
34
+ end
35
+
36
+ def run
37
+ send(command)
38
+ end
39
+
40
+ private
41
+
42
+ def validate!
43
+ raise Error.new errors.messages if invalid?
44
+ end
45
+
46
+ def log_file_path_exists
47
+ errors.add(:options, "log file path not found: '#{options[:logfile]}', specify this using the --logfile option") unless Dir.exists?(File.dirname(options[:logfile]))
48
+ end
49
+
50
+ def config_file_exists
51
+ errors.add(:options, "configuration file not found: '#{options[:config_file]}', specify this using the --config option") unless File.exists?(options[:config_file])
52
+ end
53
+
54
+ def require_path_valid
55
+ if require_rails? && !File.exist?("#{options[:require_path]}/config/application.rb")
56
+ errors.add(:options, 'point rabbit_feed to a Rails 3/4 application or a Ruby file to load your worker classes with --require')
57
+ end
58
+ end
59
+
60
+ def pidfile_path_exists
61
+ errors.add(:options, "pid file path not found: '#{options[:pidfile]}', specify this using the --pidfile option") unless Dir.exists?(File.dirname(options[:pidfile]))
62
+ end
63
+
64
+ def environment_specified
65
+ errors.add(:options, '--environment not specified') unless environment.present?
66
+ end
67
+
68
+ def consume
69
+ daemonize if daemonize?
70
+ RabbitFeed::Consumer.run
71
+ end
72
+
73
+ def produce
74
+ RabbitFeed::Producer.publish_event options[:name], options[:payload]
75
+ end
76
+
77
+ def set_logging
78
+ RabbitFeed.log = Logger.new(options[:logfile])
79
+ RabbitFeed.log.level = verbose? ? Logger::DEBUG : Logger::INFO
80
+ end
81
+
82
+ def set_configuration
83
+ RabbitFeed.environment = environment
84
+ RabbitFeed.configuration_file_path = options[:config_file]
85
+ ENV['RACK_ENV'] = ENV['RAILS_ENV'] = RabbitFeed.environment
86
+ end
87
+
88
+ def load_dependancies
89
+ if require_rails?
90
+ require 'rails'
91
+ require File.expand_path("#{options[:require_path]}/config/environment.rb")
92
+ ::Rails.application.eager_load!
93
+ else
94
+ require options[:require_path]
95
+ end
96
+ end
97
+
98
+ def daemonize
99
+ Process.daemon(true, true)
100
+ pid_path = File.split options[:pidfile]
101
+ PidFile.new(piddir: pid_path[0], pidfile: pid_path[1])
102
+ end
103
+
104
+ def environment
105
+ if options[:environment].present?
106
+ options[:environment]
107
+ else
108
+ ENV['RACK_ENV'] || ENV['RAILS_ENV']
109
+ end
110
+ end
111
+
112
+ def daemonize?
113
+ options[:daemon]
114
+ end
115
+
116
+ def verbose?
117
+ options[:verbose]
118
+ end
119
+
120
+ def require_rails?
121
+ File.directory?(options[:require_path])
122
+ end
123
+
124
+ def parse_options argv
125
+ opts = {}
126
+
127
+ parser = OptionParser.new do |o|
128
+
129
+ o.on '-m', '--payload VAL', 'Payload of event to produce' do |arg|
130
+ opts[:payload] = arg
131
+ end
132
+
133
+ o.on '-n', '--name VAL', 'Name of event to produce' do |arg|
134
+ opts[:name] = arg
135
+ end
136
+
137
+ o.on '-d', '--daemon', 'Daemonize process' do |arg|
138
+ opts[:daemon] = arg
139
+ end
140
+
141
+ o.on '-e', '--environment ENV', 'Application environment' do |arg|
142
+ opts[:environment] = arg
143
+ end
144
+
145
+ o.on '-r', '--require [PATH|DIR]', 'Location of Rails application with workers or file to require' do |arg|
146
+ opts[:require_path] = arg
147
+ end
148
+
149
+ o.on '-v', '--verbose', 'Print more verbose output' do |arg|
150
+ opts[:verbose] = arg
151
+ end
152
+
153
+ o.on '-C', '--config PATH', 'Path to YAML config file' do |arg|
154
+ opts[:config_file] = arg
155
+ end
156
+
157
+ o.on '-L', '--logfile PATH', 'Path to writable logfile' do |arg|
158
+ opts[:logfile] = arg
159
+ end
160
+
161
+ o.on '-P', '--pidfile PATH', 'Path to pidfile' do |arg|
162
+ opts[:pidfile] = arg
163
+ end
164
+
165
+ o.on '-V', '--version', 'Print version and exit' do |arg|
166
+ puts "RabbitFeed #{RabbitFeed::VERSION}"
167
+ exit 0
168
+ end
169
+ end
170
+
171
+ parser.banner = 'rabbit_feed action [options]'
172
+ parser.on_tail '-h', '--help', 'Show help' do
173
+ puts parser
174
+ exit 1
175
+ end
176
+ parser.parse! argv
177
+ DEFAULTS.merge opts
178
+ end
179
+
180
+ end
181
+ end