msgr 0.3.0 → 0.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: 8948601f4dd6d56c3d502ec2c9898e7b0d6bd6d7
4
- data.tar.gz: 4f4bf4567945427f5296f5d9e38f064ed54b8769
3
+ metadata.gz: c5dac0bc0219f72492a4b9d740e884ed191e4aa5
4
+ data.tar.gz: 9342839d6f9b78908999952b29cb826dd958b0cb
5
5
  SHA512:
6
- metadata.gz: 70a9b7e8ed8a3e94dbb6fde5401954661911450f8ffadf7a19135d682f6088d81565151b5fcfe933bbb7b08392f35126c53aab957a1019ec9535d227fdfa06c9
7
- data.tar.gz: d8761f0ec024977fce524b91127f7728a302734ad88dfcaa6a40cd77df1aaf2168ea187007df33d9e9ac436c392a09add8bb709beac203dfe77c54064109c949
6
+ metadata.gz: 10afa11b46160f7b116db6b2ed99e10262789592bc6bf9e2beda5f646bdede1b8802c19ebc19c998e237b20f59e9a999d9a180246aa3aeb0404fbd8fc1ab3ac5
7
+ data.tar.gz: cb2b8e805a974aab1c94680982ebfe6bd23a073dc2c74acd554cf1a231c83a049c12f1cca7532de5b4f0b7ff680be7ab4bfc464b9ea678096e0774ad13169ae8
data/CHANGELOG.md CHANGED
@@ -1,5 +1,9 @@
1
1
  # Changelog
2
2
 
3
+ ## 0.4.0
4
+
5
+ * Improve railtie and autostart code
6
+
3
7
  ## 0.3.0
4
8
 
5
9
  * Support for forking web servers like unicorn
data/README.md CHANGED
@@ -95,7 +95,7 @@ common: &common
95
95
  autostart: false
96
96
  ```
97
97
 
98
- And call inside each worker `Msgr.client.start` - e.g. in an after-fork block
98
+ And call inside each worker `Msgr.start` - e.g. in an after-fork block
99
99
 
100
100
 
101
101
  ## Contributing
data/lib/msgr/client.rb CHANGED
@@ -109,6 +109,9 @@ module Msgr
109
109
  def init
110
110
  @bunny = Bunny.new @config
111
111
  @pool = Pool.new Dispatcher, size: @config[:size]
112
+ if @config[:routing_file]
113
+ routes.files << @config[:routing_file]
114
+ end
112
115
  end
113
116
 
114
117
  def launch
data/lib/msgr/railtie.rb CHANGED
@@ -12,54 +12,69 @@ module Msgr
12
12
  config.msgr.rabbitmq_config ||= Rails.root.join *%w(config rabbitmq.yml)
13
13
  end
14
14
 
15
- initializer 'msgr.routes_file' do
16
- config.msgr.routes_file ||= Rails.root.join *%w(config msgr.rb)
17
- end
18
-
19
15
  # Start msgr
20
16
  initializer 'msgr.start' do
21
17
  config.after_initialize do |app|
22
18
  Msgr.logger = app.config.msgr.logger
23
19
  Celluloid.logger = app.config.msgr.logger
24
20
 
25
- cfile = app.config.msgr.rabbitmq_config.to_s
26
- config = YAML.load ERB.new(File.read(cfile)).result
21
+ self.class.load app.config.msgr
22
+ end
23
+ end
27
24
 
28
- raise ArgumentError, 'Could not load rabbitmq config: Config must be a Hash' unless config.is_a? Hash
25
+ class << self
26
+ def load(rails_config)
27
+ cfg = parse_config load_config rails_config.rabbitmq_config.to_s
28
+ return unless cfg # no config given -> does not load Msgr
29
29
 
30
- if config[Rails.env].is_a?(Hash)
31
- cfg = HashWithIndifferentAccess.new config[Rails.env]
30
+ Msgr.config = cfg
32
31
 
33
- if cfg[:enabled].nil? || cfg[:enabled] == 'true'
34
- if cfg[:uri]
35
- client = Msgr::Client.new cfg
36
- client.routes.files << app.config.msgr.routes_file
37
- client.routes.reload
32
+ # later loading for e.g. unicorn
33
+ if Rails.env.development?
34
+ Msgr.after_load do |client|
35
+ setup_autoreload client
36
+ end
37
+ end
38
38
 
39
- if Rails.env.development? || config
40
- reloader = ActiveSupport::FileUpdateChecker.new client.routes.files do
41
- client.routes.reload
42
- client.reload
43
- end
39
+ Msgr.start if cfg[:autostart]
40
+ end
44
41
 
45
- ActionDispatch::Reloader.to_prepare do
46
- reloader.execute_if_updated
47
- end
48
- end
42
+ def setup_autoreload(client)
43
+ reloader = ActiveSupport::FileUpdateChecker.new client.routes.files do
44
+ client.routes.reload
45
+ client.reload
46
+ end
49
47
 
50
- Msgr.client = client
51
- client.start unless cfg[:autostart] and cfg[:autostart] == 'false'
52
- else
53
- raise ArgumentError, 'Could not load rabbitmq environment config: URI missing.'
54
- end
55
- end
48
+ ActionDispatch::Reloader.to_prepare do
49
+ reloader.execute_if_updated
50
+ end
51
+ end
52
+
53
+ def parse_config(cfg)
54
+ unless cfg.is_a? Hash
55
+ raise ArgumentError, 'Could not load rabbitmq config: Config must be a Hash'
56
+ end
57
+ unless cfg[Rails.env].is_a?(Hash)
58
+ raise ArgumentError, "Could not load rabbitmq config for environment \"#{Rails.env}\": is not a Hash"
59
+ end
60
+ cfg = HashWithIndifferentAccess.new cfg[Rails.env]
61
+ unless cfg[:uri]
62
+ raise ArgumentError, 'Could not load rabbitmq environment config: URI missing.'
63
+ end
64
+ case cfg[:autostart]
65
+ when true, 'true', 'enabled', nil
66
+ cfg[:autostart] = true
67
+ when false, 'false', 'disabled'
68
+ cfg[:autostart] = false
56
69
  else
57
- if Rails.env.production?
58
- raise ArgumentError, 'Could not load rabbitmq environment config: Not a hash.'
59
- else
60
- Rails.logger.warn 'Could not load rabbitmq environment config: Not a hash.'
61
- end
70
+ raise ArgumentError, "Invalid value for rabbitmq config autostart: \"#{cfg[:autostart]}\""
62
71
  end
72
+ cfg[:routing_file] ||= Rails.root.join('config/msgr.rb').to_s
73
+ cfg
74
+ end
75
+
76
+ def load_config(file)
77
+ YAML.load ERB.new(File.read(file)).result
63
78
  end
64
79
  end
65
80
  end
data/lib/msgr/version.rb CHANGED
@@ -1,7 +1,7 @@
1
1
  module Msgr
2
2
  module VERSION
3
3
  MAJOR = 0
4
- MINOR = 3
4
+ MINOR = 4
5
5
  PATCH = 0
6
6
  STAGE = nil
7
7
  STRING = [MAJOR, MINOR, PATCH, STAGE].reject(&:nil?).join('.')
data/lib/msgr.rb CHANGED
@@ -25,9 +25,17 @@ require 'msgr/railtie' if defined? Rails
25
25
  module Msgr
26
26
 
27
27
  class << self
28
- attr_accessor :client
28
+ attr_writer :client, :config
29
29
  delegate :publish, to: :client
30
30
 
31
+ def config
32
+ @config ||= {}
33
+ end
34
+
35
+ def client
36
+ @client ||= Msgr::Client.new config
37
+ end
38
+
31
39
  def logger
32
40
  if @logger.nil?
33
41
  @logger = Logger.new $stdout
@@ -40,5 +48,18 @@ module Msgr
40
48
  def logger=(logger)
41
49
  @logger = logger
42
50
  end
51
+
52
+ def after_load(&block)
53
+ @after_load_callbacks ||= []
54
+ @after_load_callbacks << block
55
+ end
56
+
57
+ def start
58
+ client.start
59
+ (@after_load_callbacks || []).each do |callback|
60
+ callback.call client
61
+ end
62
+ client
63
+ end
43
64
  end
44
65
  end
@@ -9,4 +9,90 @@ describe Msgr::Railtie do
9
9
  expect(config).to respond_to :msgr
10
10
  end
11
11
  end
12
+
13
+ describe 'msgr should have load config/msgr.rb' do
14
+ subject { Msgr.client.routes }
15
+ its(:files) { should eq [Rails.root.join('config/msgr.rb').to_s] }
16
+ end
17
+
18
+ describe '#parse_config' do
19
+ let(:settings) { {} }
20
+ let(:action) { described_class.parse_config settings }
21
+ subject { action }
22
+
23
+ context 'with incorrect settings' do
24
+ subject { -> { action } }
25
+ context 'without an hash config' do
26
+ let(:settings) { '' }
27
+
28
+ it { should raise_error 'Could not load rabbitmq config: Config must be a Hash' }
29
+ end
30
+
31
+ context 'without an hash for the current environment' do
32
+ let(:settings) { {} }
33
+
34
+ it { should raise_error 'Could not load rabbitmq config for environment "test": is not a Hash' }
35
+ end
36
+
37
+ context 'with config without url' do
38
+ let(:settings) { {"test" => { hans: 'otto'}} }
39
+
40
+ it { should raise_error 'Could not load rabbitmq environment config: URI missing.' }
41
+ end
42
+
43
+ context 'with invalid autostart value' do
44
+ let(:settings) { {"test" => { uri: 'hans', autostart: 'unvalid'}} }
45
+
46
+ it { should raise_error 'Invalid value for rabbitmq config autostart: "unvalid"'}
47
+ end
48
+ end
49
+
50
+ context 'without set routes file' do
51
+ let(:settings) { {"test" => { uri: 'test'}} }
52
+
53
+ its([:routing_file]) { should eq Rails.root.join('config/msgr.rb').to_s }
54
+ end
55
+
56
+ context 'with set routes file' do
57
+ let(:settings) { {"test" => { uri: 'test', 'routing_file' => 'my fancy file' }} }
58
+
59
+ its([:routing_file]) { should eq 'my fancy file' }
60
+ end
61
+
62
+ context 'with uri as symbol' do
63
+ let(:settings) { {"test" => { uri: "hans"}}}
64
+
65
+ its([:uri]) { should eq 'hans' }
66
+ end
67
+
68
+ context 'with uri as string' do
69
+ let(:settings) { {"test" => { 'uri' => "hans"}}}
70
+
71
+ its([:uri]) { should eq 'hans' }
72
+ end
73
+ end
74
+
75
+ describe '#load' do
76
+ let(:config) do
77
+ cfg = ActiveSupport::OrderedOptions.new
78
+ cfg.rabbitmq_config = Rails.root.join *%w(config rabbitmq.yml)
79
+ cfg
80
+ end
81
+
82
+ context 'with autostart = false' do
83
+ it 'should not start Msgr' do
84
+ expect(Msgr).not_to receive(:start)
85
+ expect(Msgr::Railtie).to receive(:load_config).and_return({ "test" => { uri: 'test', autostart: false} })
86
+ Msgr::Railtie.load config
87
+ end
88
+ end
89
+
90
+ context 'without autostart value' do
91
+ it 'should not start Msgr' do
92
+ expect(Msgr).to receive(:start)
93
+ expect(Msgr::Railtie).to receive(:load_config).and_return({ "test" => { uri: 'test' } })
94
+ Msgr::Railtie.load config
95
+ end
96
+ end
97
+ end
12
98
  end
@@ -47,5 +47,12 @@ describe Msgr::Client do
47
47
  Msgr::Client.new(uri: 'ampq://localhost', ssl: true, host: 'a.example.org').start
48
48
  end
49
49
  end
50
+
51
+ context 'routes' do
52
+ let(:params) { [ routing_file: 'config/msgr.rb']}
53
+ before { client.start }
54
+ subject { client.routes }
55
+ its(:files) { should eq ['config/msgr.rb'] }
56
+ end
50
57
  end
51
58
  end
@@ -33,4 +33,12 @@ describe Msgr do
33
33
 
34
34
  sleep 10
35
35
  end
36
+
37
+ describe '.after_load' do
38
+ before { allow_any_instance_of(Msgr::Client).to receive(:launch) }
39
+
40
+ it 'should yield the given block when Msgr.start is called' do
41
+ expect { |cb| Msgr.after_load &cb; Msgr.start }.to yield_with_args
42
+ end
43
+ end
36
44
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: msgr
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jan Graichen
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-03-03 00:00:00.000000000 Z
11
+ date: 2014-03-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport