workers_loader 0.0.3 → 0.0.4

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.
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- MzM2NTBjNDBlOGZhZTVhMjI5MDUzMDJkYTA5MTNkYmY4ZjcxZjBhMA==
4
+ YzQ0ZjM0ZmJhYWNjZmYwNDhkOWU0YzgwZTVkY2NlMGY5MWNmZjBjNg==
5
5
  data.tar.gz: !binary |-
6
- NmQ0YzNmMTc5ZWJiZDZmYTYwNTlmMmM1OWQwZGExY2E0NjU0MWQwZA==
6
+ MDYyZjhiMTZiOWU1ZTgxYzBiY2ExMzQ0MTY3MzhhM2NhNGRmMjlmZg==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- N2ViMDA2YjliNzUwMjEzYTViODFjNmMyOWRjODQwNjkyYjg5YjM3NTQwNDg0
10
- YjkxZWY1ZjY2NjA0NmU4OWJkOGYxMTRhYzkwNDlkYmNiZWIwZWQ2NGM1ZWNj
11
- NmUyNmE4MDU3ZGNkNGFlNWE0ZDk3ZDlkMDkyMjk3NWYwZjk2ZGI=
9
+ ODIxZDMyMGVlMzQ2NTIzN2JmOGFmNjllOGY0YWQ2NmJlZDc4N2UxNzFiZWNi
10
+ YTk2N2Q1ODA0ZjQ2ZWIyOThlMzczZDY1ZGE5NDM1ZWIxMGRiMmMwZDdhNzNm
11
+ NWM4ZWY3MjU0YmVkYWM3YmExM2YwNGE2Y2Y1NDczNmZjMTcxMjg=
12
12
  data.tar.gz: !binary |-
13
- MGMyNzU4N2RjZWY5ZWE2OTczMTMzNDYwOWU5MWU0MjVhNjE1NGQwMjE4MDU5
14
- YTI0MTQ2OWEzN2RmYjBlMGZiMGVhZjQ4NjlhNzM3N2M1NDQ5NjYwMDU5Mjc2
15
- ZTA4ZGJlYWY5NzM1ZTE3MGFjMTAzZTdmZDEyNmZkYTc3NGU2Nzg=
13
+ ZTkzMDI3OTM3MGFhYjFhNDJhMjU4ZmY1MTU5OWMwZDIzMzY0MTliMDY4MGNl
14
+ Mjc0NjgwYzEwODNlYTA1M2E0NzIyYmI5NGJlMzc3MzAwNGRkY2EzYjBkZWY3
15
+ YTk0MWRkMDZiMWQxNWUzZWUwMTEzZTg3Y2NhY2M1MjYwZTg2NjA=
data/README CHANGED
@@ -1,6 +1,5 @@
1
1
  This library is intended to let workers modularization.
2
2
 
3
-
4
3
  It lets you load workers queue names from a given directory.
5
4
 
6
5
  Let's say you have some engines with workers in folder /my_engine/app/workers
@@ -12,7 +11,9 @@ You can place an initializer like follow in your engine so then the container ap
12
11
  module MyApp
13
12
  class Engine < ::Rails::Engine
14
13
  initializer 'my_app.wokers_path' do |app|
15
- WorkersLoader.add_path(MyApp::Engine.root.join('app', workers))
14
+ WorkersLoader.resque_mailer!
15
+ workers_path = MyApp::Engine.root.join('app', 'workers', 'my_app')
16
+ WorkersLoader.add_path(workers_path)
16
17
  end
17
18
  end
18
19
  end
@@ -24,3 +25,4 @@ You can get the complete list of queues by doing the following:
24
25
  WorkersLoader.load_workers!
25
26
  WorkersLoader.workers
26
27
 
28
+ With this list you can tell resque what queues to listen to.
@@ -18,6 +18,11 @@ module WorkersLoader
18
18
  .map { |file| /#{base}\/(.*).rb/.match(file)[1] }
19
19
  end
20
20
 
21
+ def find
22
+ files.map { |file| queue_for(file) }
23
+ .reject(&:blank?)
24
+ end
25
+
21
26
  def class_for(relative_path)
22
27
  relative_path.split('/').map(&:camelize).join('::').constantize
23
28
  rescue NameError
@@ -1,5 +1,5 @@
1
1
  # encoding: utf-8
2
2
 
3
3
  module WorkersLoader
4
- VERSION = '0.0.3'
4
+ VERSION = '0.0.4'
5
5
  end
@@ -10,31 +10,42 @@ module WorkersLoader
10
10
  mattr_accessor :workers
11
11
  @@workers = []
12
12
 
13
+ mattr_accessor :resque_mailer
14
+ @@resque_mailer = false
15
+
13
16
  class << self
14
- def add_path(path)
17
+ def add_path(path, parent = true)
15
18
  fail "Directory not found: `#{path}`" unless Dir.exist?(path)
16
- @@workers_paths << path
17
- end
18
-
19
- def find(path)
20
- path = Path.new(path)
21
- path.files.map { |file| path.queue_for(file) }
22
- .reject(&:blank?)
19
+ @@workers_paths << Path.new(path, parent)
23
20
  end
24
21
 
25
22
  def load_workers!
26
23
  workers_paths.each do |path|
27
- workers_in_path = find(path)
24
+ workers_in_path = path.find
28
25
  next if workers_in_path.empty?
29
26
 
30
27
  duplacates = workers_in_path
31
28
  .select { |worker| workers.include?(worker) }
32
- if duplacates.any?
33
- fail("Workers already present! #{duplacates.sort.join(', ')}")
34
- end
29
+ .sort.join(', ')
30
+ fail("Workers already present! #{duplacates}") unless duplacates.blank?
35
31
 
36
32
  self.workers += workers_in_path
37
33
  end
34
+
35
+ resque_mailer_install
36
+ end
37
+
38
+ def resque_mailer_install
39
+ return if !resque_mailer? || self.workers.include?(:mailer)
40
+ self.workers << :mailer
41
+ end
42
+
43
+ def resque_mailer!
44
+ @@resque_mailer = true
45
+ end
46
+
47
+ def resque_mailer?
48
+ @@resque_mailer
38
49
  end
39
50
  end
40
51
 
@@ -23,19 +23,25 @@ describe WorkersLoader::Path do
23
23
  end
24
24
 
25
25
  describe '#files' do
26
- let(:files) { %w(user reports/usage).sort }
26
+ let(:files) { %w(not_a_worker user reports/usage).sort }
27
27
  it { expect(subject.files.sort).to eq(files) }
28
28
  end
29
29
 
30
30
  describe '#class_for' do
31
+ it { expect(subject.class_for('not_a_worker')).to eq(NotAWorker) }
31
32
  it { expect(subject.class_for('user')).to eq(User) }
32
33
  it { expect(subject.class_for('reports/usage')).to eq(Reports::Usage) }
33
34
  end
34
35
 
35
36
  describe '#queue_for' do
37
+ it { expect(subject.queue_for('not_a_worker')).to be(false) }
36
38
  it { expect(subject.queue_for('user')).to eq(:user_queue) }
37
39
  it { expect(subject.queue_for('reports/usage')).to eq(:usage_queue) }
38
40
  end
41
+
42
+ describe '::find' do
43
+ it { expect(subject.find.sort).to eq([:user_queue, :usage_queue].sort) }
44
+ end
39
45
  end
40
46
 
41
47
  context 'with parent' do
@@ -70,5 +76,9 @@ describe WorkersLoader::Path do
70
76
  it { expect(subject.queue_for('dummy/foo')).to eq(:dummy_foo) }
71
77
  it { expect(subject.queue_for('dummy/bar/baz')).to eq(:baz_queue) }
72
78
  end
79
+
80
+ describe '::find' do
81
+ it { expect(subject.find.sort).to eq([:dummy_foo, :baz_queue].sort) }
82
+ end
73
83
  end
74
84
  end
@@ -15,8 +15,8 @@ describe WorkersLoader do
15
15
 
16
16
  describe '::add_path' do
17
17
  before { described_class.add_path(workers_path) }
18
-
19
- it { expect(described_class.workers_paths).to eq([workers_path]) }
18
+ subject { described_class.workers_paths.first }
19
+ it { expect(subject).to be_a(WorkersLoader::Path) }
20
20
 
21
21
  context 'dubplicate worker' do
22
22
  let(:message) { 'Directory not found: `foo`' }
@@ -24,21 +24,33 @@ describe WorkersLoader do
24
24
  end
25
25
  end
26
26
 
27
- describe '::find' do
28
- subject { described_class.find(workers_path) }
27
+ describe '::load_workers!' do
28
+ let(:workers) { [:baz_queue, :dummy_foo].sort }
29
29
 
30
- it { expect(subject.sort).to eq([:dummy_foo, :baz_queue].sort) }
31
- end
30
+ context 'load workers' do
31
+ before do
32
+ described_class.add_path(workers_path)
33
+ described_class.load_workers!
34
+ end
32
35
 
33
- describe '::load_workers!' do
34
- before do
35
- described_class.add_path(workers_path)
36
- described_class.load_workers!
36
+ it { expect(described_class.workers.sort).to eq(workers) }
37
+ end
38
+
39
+ context 'load mailer' do
40
+ before do
41
+ described_class.resque_mailer!
42
+ described_class.load_workers!
43
+ end
44
+
45
+ it { expect(described_class.workers.sort).to include(:mailer) }
37
46
  end
38
- let(:workers) { [:baz_queue, :dummy_foo].sort }
39
- it { expect(described_class.workers.sort).to eq(workers) }
40
47
 
41
48
  context 'prevent duplicates' do
49
+ before do
50
+ described_class.add_path(workers_path)
51
+ described_class.load_workers!
52
+ end
53
+
42
54
  let(:message) { "Workers already present! #{workers.join(', ')}" }
43
55
  it { expect { described_class.load_workers! }.to raise_error(message) }
44
56
  end
data/spec/spec_helper.rb CHANGED
@@ -13,4 +13,9 @@ RSpec.configure do |config|
13
13
  config.mock_with :rspec
14
14
 
15
15
  config.order = 'random'
16
+
17
+ config.before(:each) do
18
+ WorkersLoader.workers = []
19
+ WorkersLoader.resque_mailer = false
20
+ end
16
21
  end
@@ -0,0 +1,4 @@
1
+ # encoding: utf-8
2
+
3
+ class NotAWorker
4
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: workers_loader
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ricard Forniol
@@ -96,6 +96,7 @@ files:
96
96
  - spec/lib/workers_loader/path_spec.rb
97
97
  - spec/lib/workers_loader_spec.rb
98
98
  - spec/spec_helper.rb
99
+ - spec/support/data/not_a_worker.rb
99
100
  - spec/support/data/reports/usage.rb
100
101
  - spec/support/data/user.rb
101
102
  - spec/support/dummy/bar/base.rb
@@ -128,6 +129,7 @@ test_files:
128
129
  - spec/lib/workers_loader/path_spec.rb
129
130
  - spec/lib/workers_loader_spec.rb
130
131
  - spec/spec_helper.rb
132
+ - spec/support/data/not_a_worker.rb
131
133
  - spec/support/data/reports/usage.rb
132
134
  - spec/support/data/user.rb
133
135
  - spec/support/dummy/bar/base.rb