bumbleworks 0.0.44 → 0.0.45

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.
@@ -65,7 +65,7 @@ module Bumbleworks
65
65
  # two require the bumbleworks-redis and bumbleworks-sequel gems, respectively.
66
66
  # You can set the storage as follows:
67
67
  #
68
- # @Exammple: Redis
68
+ # @Example: Redis
69
69
  # Bumbleworks.storage = Redis.new(:host => '127.0.0.1', :db => 0, :thread_safe => true)
70
70
  #
71
71
  # @Example: Sequel with Postgres db
@@ -73,6 +73,17 @@ module Bumbleworks
73
73
  #
74
74
  define_setting :storage
75
75
 
76
+ # Normally, the first adapter in the storage_adapters list that can #use? the
77
+ # configured storage will be used automatically. This may not be what you want;
78
+ # if you have multiple adapters that can use a Redis database or a Hash, for
79
+ # example, you may want to specify explicitly which one to use. Use this setting
80
+ # to override the automatic adapter selection.
81
+ #
82
+ # @Example:
83
+ # Bumbleworks.storage_adapter = Bumbleworks::OtherHashStorage
84
+ #
85
+ define_setting :storage_adapter
86
+
76
87
  # Bumbleworks will attempt to log a variety of events (tasks becoming
77
88
  # available, being claimed/released/completed, etc), and to do so it uses
78
89
  # the logger registered in configuration. If no logger has been registered,
@@ -186,12 +197,27 @@ module Bumbleworks
186
197
  #
187
198
  def add_storage_adapter(adapter)
188
199
  raise ArgumentError, "#{adapter} is not a Bumbleworks storage adapter" unless
189
- [:driver, :use?, :storage_class, :display_name].all? { |m| adapter.respond_to?(m) }
200
+ [:driver, :use?, :new_storage, :allow_history_storage?, :storage_class, :display_name].all? { |m| adapter.respond_to?(m) }
190
201
 
191
202
  @storage_adapters << adapter
192
203
  @storage_adapters
193
204
  end
194
205
 
206
+ # If storage_adapter is not explicitly set, find first registered adapter that
207
+ # can use Bumbleworks.storage.
208
+ #
209
+ def storage_adapter
210
+ @storage_adapter ||= begin
211
+ all_adapters = storage_adapters
212
+ raise UndefinedSetting, "No storage adapters configured" if all_adapters.empty?
213
+ adapter = all_adapters.detect do |potential_adapter|
214
+ potential_adapter.use?(storage)
215
+ end
216
+ raise UndefinedSetting, "Storage is missing or not supported. Supported: #{all_adapters.map(&:display_name).join(', ')}" unless adapter
217
+ adapter
218
+ end
219
+ end
220
+
195
221
  def logger
196
222
  @logger ||= Bumbleworks::SimpleLogger
197
223
  end
@@ -114,7 +114,7 @@ module Bumbleworks
114
114
  end
115
115
 
116
116
  def set_up_storage_history
117
- if storage_adapter.allow_history_storage?
117
+ if Bumbleworks.storage_adapter.allow_history_storage?
118
118
  dashboard.add_service('history', 'ruote/log/storage_history', 'Ruote::StorageHistory')
119
119
  end
120
120
  end
@@ -128,19 +128,8 @@ module Bumbleworks
128
128
  end
129
129
  end
130
130
 
131
- def storage_adapter
132
- @storage_adapter ||= begin
133
- all_adapters = Bumbleworks.configuration.storage_adapters
134
- adapter = all_adapters.detect do |potential_adapter|
135
- potential_adapter.use?(Bumbleworks.storage)
136
- end
137
- raise UndefinedSetting, "Storage is missing or not supported. Supported: #{all_adapters.map(&:display_name).join(', ')}" unless adapter
138
- adapter
139
- end
140
- end
141
-
142
131
  def storage
143
- @storage ||= storage_adapter.driver.new(Bumbleworks.storage)
132
+ @storage ||= Bumbleworks.storage_adapter.new_storage(Bumbleworks.storage)
144
133
  end
145
134
 
146
135
  def reset!
@@ -149,7 +138,6 @@ module Bumbleworks
149
138
  @storage.shutdown
150
139
  end
151
140
  @dashboard.shutdown if @dashboard && @dashboard.respond_to?(:shutdown)
152
- @storage_adapter = nil
153
141
  @storage = nil
154
142
  @dashboard = nil
155
143
  end
@@ -1,5 +1,6 @@
1
1
  module Bumbleworks
2
2
  class StorageAdapter
3
+ class UnsupportedStorage < StandardError; end
3
4
  class << self
4
5
  attr_accessor :auto_register
5
6
 
@@ -11,6 +12,11 @@ module Bumbleworks
11
12
  raise "Subclass responsibility"
12
13
  end
13
14
 
15
+ def new_storage(storage)
16
+ raise UnsupportedStorage unless use?(storage)
17
+ driver.new(storage)
18
+ end
19
+
14
20
  def use?(storage)
15
21
  storage.is_a? storage_class
16
22
  end
@@ -1,3 +1,3 @@
1
1
  module Bumbleworks
2
- VERSION = "0.0.44"
2
+ VERSION = "0.0.45"
3
3
  end
@@ -10,5 +10,4 @@ end
10
10
 
11
11
  Bumbleworks.register_tasks
12
12
  Bumbleworks.load_definitions!
13
- Bumbleworks.dashboard.add_service('history', 'ruote/log/storage_history', 'Ruote::StorageHistory')
14
13
  Bumbleworks.start_worker!
@@ -0,0 +1,52 @@
1
+ describe 'History storage' do
2
+ let(:app_root) {
3
+ File.expand_path(File.join(fixtures_path, 'apps', 'with_default_directories'))
4
+ }
5
+
6
+ class HashStorageWithHistory < Bumbleworks::HashStorage
7
+ def self.allow_history_storage?
8
+ true
9
+ end
10
+ end
11
+
12
+ before :each do
13
+ Bumbleworks.reset!
14
+ end
15
+
16
+ context 'when storage allows storing history' do
17
+ before :each do
18
+ Bumbleworks.stub(:storage_adapter => HashStorageWithHistory)
19
+ load File.join(app_root, 'full_initializer.rb')
20
+ end
21
+
22
+ it 'uses storage for history' do
23
+ Bumbleworks.dashboard.history.should be_a(::Ruote::StorageHistory)
24
+ end
25
+
26
+ it 'keeps history of messages' do
27
+ Bumbleworks::Ruote.storage.get_many('history').should be_empty
28
+ wfid = Bumbleworks.launch!('make_honey')
29
+ Bumbleworks.dashboard.wait_for(:dave)
30
+ Bumbleworks::Ruote.storage.get_many('history').should_not be_empty
31
+ Bumbleworks.dashboard.history.wfids.should include(wfid)
32
+ end
33
+ end
34
+
35
+ context 'when storage does not allow storing history' do
36
+ before :each do
37
+ load File.join(app_root, 'full_initializer.rb')
38
+ end
39
+
40
+ it 'does not use storage for history' do
41
+ Bumbleworks.dashboard.history.should be_a(::Ruote::DefaultHistory)
42
+ end
43
+
44
+ it 'keeps history of messages' do
45
+ Bumbleworks.dashboard.history.by_date(Date.today).should be_empty
46
+ wfid = Bumbleworks.launch!('make_honey')
47
+ Bumbleworks.dashboard.wait_for(:dave)
48
+ Bumbleworks.dashboard.history.by_date(Date.today).should_not be_empty
49
+ Bumbleworks.dashboard.history.wfids.should include(wfid)
50
+ end
51
+ end
52
+ end
@@ -212,11 +212,41 @@ describe Bumbleworks::Configuration do
212
212
  end
213
213
  end
214
214
 
215
+ describe "#storage_adapter" do
216
+ it 'defaults to first adapter in registered list that uses storage' do
217
+ right_adapter = double('right', :use? => true)
218
+ wrong_adapter_1 = double('wrong1', :use? => false)
219
+ wrong_adapter_2 = double('wrong2', :use? => false)
220
+ subject.stub(:storage_adapters => [wrong_adapter_1, right_adapter, wrong_adapter_2])
221
+ subject.storage_adapter.should == right_adapter
222
+ end
223
+
224
+ it 'can be set storage directly' do
225
+ storage = double("Storage Adapter")
226
+ subject.storage_adapter = storage
227
+ subject.storage_adapter.should == storage
228
+ end
229
+
230
+ it 'raises UndefinedSetting if no matching storage adapter' do
231
+ wrong_adapter = double('wrong1', :use? => false, :display_name => 'Wrong')
232
+ subject.stub(:storage_adapters => [wrong_adapter])
233
+ expect {
234
+ subject.storage_adapter
235
+ }.to raise_error(Bumbleworks::UndefinedSetting,
236
+ "Storage is missing or not supported. Supported: Wrong")
237
+ end
238
+
239
+ it 'raises UndefinedSetting if no storage adapters' do
240
+ expect {
241
+ subject.storage_adapter
242
+ }.to raise_error(Bumbleworks::UndefinedSetting,
243
+ "No storage adapters configured")
244
+ end
245
+ end
246
+
215
247
  describe '#add_storage_adapter' do
216
248
  it 'adds storage adapter to registered list' do
217
- GoodForNothingStorage = OpenStruct.new(
218
- :driver => nil, :storage_class => 'Dummy', :display_name => 'Dummy', :use? => true
219
- )
249
+ GoodForNothingStorage = double('fake_storage', :respond_to? => true)
220
250
  configuration.storage_adapters.should be_empty
221
251
  configuration.add_storage_adapter(GoodForNothingStorage)
222
252
  configuration.add_storage_adapter(Bumbleworks::HashStorage)
@@ -243,7 +243,7 @@ describe Bumbleworks::Ruote do
243
243
  describe '.set_up_storage_history' do
244
244
  it 'adds a storage history service to the dashboard if storage adapter allows it' do
245
245
  storage_adapter = double('adapter', :allow_history_storage? => true)
246
- described_class.stub(:storage_adapter => storage_adapter)
246
+ Bumbleworks.storage_adapter = storage_adapter
247
247
  described_class.stub(:storage => Ruote::HashStorage.new({}))
248
248
  Bumbleworks.dashboard.should_receive(:add_service).with(
249
249
  'history', 'ruote/log/storage_history', 'Ruote::StorageHistory'
@@ -253,7 +253,7 @@ describe Bumbleworks::Ruote do
253
253
 
254
254
  it 'does not add a storage history service to the dashboard if not allowed' do
255
255
  storage_adapter = double('adapter', :allow_history_storage? => false)
256
- described_class.stub(:storage_adapter => storage_adapter)
256
+ Bumbleworks.storage_adapter = storage_adapter
257
257
  described_class.stub(:storage => Ruote::HashStorage.new({}))
258
258
  Bumbleworks.dashboard.should_receive(:add_service).never
259
259
  described_class.set_up_storage_history
@@ -323,14 +323,17 @@ describe Bumbleworks::Ruote do
323
323
  describe '.storage' do
324
324
  it 'raise error when storage is not defined' do
325
325
  Bumbleworks.storage = nil
326
- expect { described_class.send(:storage) }.to raise_error Bumbleworks::UndefinedSetting
326
+ expect { described_class.storage }.to raise_error Bumbleworks::UndefinedSetting
327
327
  end
328
328
 
329
- it 'handles Hash storage' do
329
+ it 'returns new storage from configured adapter' do
330
+ driven_storage = ::Ruote::HashStorage.new({})
330
331
  storage = {}
332
+ adapter = double('Adapter')
333
+ adapter.stub(:new_storage).with(storage).and_return(driven_storage)
331
334
  Bumbleworks.storage = storage
332
- Ruote::HashStorage.should_receive(:new).with(storage)
333
- described_class.send(:storage)
335
+ Bumbleworks.storage_adapter = adapter
336
+ described_class.storage.should == driven_storage
334
337
  end
335
338
  end
336
339
 
@@ -353,12 +356,6 @@ describe Bumbleworks::Ruote do
353
356
  end
354
357
 
355
358
  describe '.reset!' do
356
- it 'clears storage_adapter' do
357
- described_class.instance_variable_set(:@storage_adapter, 'the adapter')
358
- described_class.reset!
359
- described_class.instance_variable_get(:@storage_adapter).should be_nil
360
- end
361
-
362
359
  it 'purges and shuts down storage, then resets storage' do
363
360
  old_storage = described_class.storage
364
361
  old_storage.should_receive(:purge!)
@@ -51,4 +51,24 @@ describe Bumbleworks::StorageAdapter do
51
51
  described_class.use?(:not_a_string).should be_false
52
52
  end
53
53
  end
54
+
55
+ describe '.new_storage' do
56
+ before :each do
57
+ storage_driver = double('storage_driver')
58
+ storage_driver.stub(:new).with(:awesome_stuff).and_return(:new_storage)
59
+ described_class.stub(:driver => storage_driver)
60
+ end
61
+
62
+ it 'returns driven storage if driver can use storage' do
63
+ described_class.stub(:use?).with(:awesome_stuff).and_return(true)
64
+ described_class.new_storage(:awesome_stuff).should == :new_storage
65
+ end
66
+
67
+ it "raises UnsupportedStorage if driver can't use storage" do
68
+ described_class.stub(:use?).with(:awesome_stuff).and_return(false)
69
+ expect {
70
+ described_class.new_storage(:awesome_stuff)
71
+ }.to raise_error(Bumbleworks::StorageAdapter::UnsupportedStorage)
72
+ end
73
+ end
54
74
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bumbleworks
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.44
4
+ version: 0.0.45
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -205,7 +205,8 @@ files:
205
205
  - spec/fixtures/definitions/a_list_of_jams.rb
206
206
  - spec/fixtures/definitions/nested_folder/test_nested_process.rb
207
207
  - spec/fixtures/definitions/test_process.rb
208
- - spec/integration/configuration_spec.rb
208
+ - spec/integration/example_configurations_spec.rb
209
+ - spec/integration/history_storage_spec.rb
209
210
  - spec/integration/sample_application_spec.rb
210
211
  - spec/lib/bumbleworks/configuration_spec.rb
211
212
  - spec/lib/bumbleworks/error_handler_participant_spec.rb
@@ -245,7 +246,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
245
246
  version: '0'
246
247
  segments:
247
248
  - 0
248
- hash: 1016861213985986002
249
+ hash: 2245939095029135345
249
250
  required_rubygems_version: !ruby/object:Gem::Requirement
250
251
  none: false
251
252
  requirements:
@@ -254,7 +255,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
254
255
  version: '0'
255
256
  segments:
256
257
  - 0
257
- hash: 1016861213985986002
258
+ hash: 2245939095029135345
258
259
  requirements: []
259
260
  rubyforge_project:
260
261
  rubygems_version: 1.8.23
@@ -277,7 +278,8 @@ test_files:
277
278
  - spec/fixtures/definitions/a_list_of_jams.rb
278
279
  - spec/fixtures/definitions/nested_folder/test_nested_process.rb
279
280
  - spec/fixtures/definitions/test_process.rb
280
- - spec/integration/configuration_spec.rb
281
+ - spec/integration/example_configurations_spec.rb
282
+ - spec/integration/history_storage_spec.rb
281
283
  - spec/integration/sample_application_spec.rb
282
284
  - spec/lib/bumbleworks/configuration_spec.rb
283
285
  - spec/lib/bumbleworks/error_handler_participant_spec.rb