workers_loader 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ MTVlM2IzMzNhYjRjZmFlYzBiYTBhNWY3NDZmMTAyNjg0ZTJmY2Q4YQ==
5
+ data.tar.gz: !binary |-
6
+ MmYzOGJmOGY3MWUxZTEzMDg1OTNiYmE0ODNiODViNWEzOTNhMTQyMQ==
7
+ SHA512:
8
+ metadata.gz: !binary |-
9
+ MzExODcxMWIwYTEyYzI5MWYyZWI1ZDNmOGQ4YmUxNDg5MGEyMmQ3NDQ2MGJj
10
+ NjM2Y2I4YjkwMDVkMzhjMDc1ODMwMGJkYTFlZDUyYWY1NjU3NTI0ZDkzMmEy
11
+ YWRmYmJmMmM0NTJkMDRjZTc2MjJiNzhhY2U2ZjY2MTcwZTI4ZTk=
12
+ data.tar.gz: !binary |-
13
+ NzAxNzA5ZDE1ZjEyMDcxZTgwZTgyNjJhM2VhMWVjNDlhZjRmODM3NzY3ZTgw
14
+ YzRkNGEwYzBmZmI1YWVkMzQ2OTY2MTMzNmVhNWJkMzQ1Y2U5NDAyYTM3YTc0
15
+ OTRlNTJlMjBjYjgyNzgzYTUxMmZmZjI5MjY2NTNjZWJlZGNkY2E=
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2015 Ricard Forniol
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README ADDED
@@ -0,0 +1,26 @@
1
+ This library is intended to let workers modularization.
2
+
3
+
4
+ It lets you load workers queue names from a given directory.
5
+
6
+ Let's say you have some engines with workers in folder /my_engine/app/workers
7
+
8
+ You can place an initializer like follow in your engine so then the container app can collect all queues from engines:
9
+
10
+ ## Engine
11
+
12
+ module MyApp
13
+ class Engine < ::Rails::Engine
14
+ initializer 'my_app.wokers_path' do |app|
15
+ WorkersLoader.add_path(MyApp::Engine.root.join('app', workers))
16
+ end
17
+ end
18
+ end
19
+
20
+ ## Container App initializer
21
+
22
+ You can get the complete list of queues by doing the following:
23
+
24
+ WorkersLoader.load_workers!
25
+ WorkersLoader.workers
26
+
data/Rakefile ADDED
@@ -0,0 +1,25 @@
1
+ #!/usr/bin/env rake
2
+ begin
3
+ require 'bundler/setup'
4
+ rescue LoadError
5
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
6
+ end
7
+ begin
8
+ require 'rdoc/task'
9
+ rescue LoadError
10
+ require 'rdoc/rdoc'
11
+ require 'rake/rdoctask'
12
+ RDoc::Task = Rake::RDocTask
13
+ end
14
+
15
+ Bundler::GemHelper.install_tasks
16
+
17
+ Dir[File.join(File.dirname(__FILE__), 'tasks/**/*.rake')].each {|f| load f }
18
+
19
+ require 'rspec/core'
20
+ require 'rspec/core/rake_task'
21
+
22
+ desc 'Run all specs in spec directory (excluding plugin specs)'
23
+ RSpec::Core::RakeTask.new(spec: 'app:db:test:prepare')
24
+
25
+ task default: :spec
@@ -0,0 +1,40 @@
1
+ # encoding: utf-8
2
+ require 'resque'
3
+
4
+ module WorkersLoader
5
+ class Path
6
+ attr_reader :base, :parent
7
+
8
+ def initialize(base, parent = true)
9
+ base = match[1] if match = /(.*)\/$/.match(base)
10
+ @base = base
11
+
12
+ @parent = base.split('/').last if parent
13
+ end
14
+
15
+ def files
16
+ path = File.join(base, '{**/*.rb}')
17
+ Dir[path]
18
+ .map { |file| /#{base}\/(.*).rb/.match(file)[1] }
19
+ .map { |relative_path| relative_path_for(relative_path) }
20
+ end
21
+
22
+ def class_for(relative_path)
23
+ relative_path.split('/').map(&:camelize).join('::').constantize
24
+ rescue NameError
25
+ file = File.join(base, "#{relative_path}.rb")
26
+ raise "File not found: #{file}" unless File.exist?(file)
27
+ load file
28
+ class_for(relative_path)
29
+ end
30
+
31
+ def queue_for(relative_path)
32
+ Resque.queue_from_class(class_for(relative_path))
33
+ end
34
+
35
+ def relative_path_for(path)
36
+ parent.nil? ? path : File.join(parent, path)
37
+ end
38
+ private :relative_path_for
39
+ end
40
+ end
@@ -0,0 +1,5 @@
1
+ # encoding: utf-8
2
+
3
+ module WorkersLoader
4
+ VERSION = '0.0.1'
5
+ end
@@ -0,0 +1,42 @@
1
+ # encoding: utf-8
2
+ require 'active_support/core_ext/module/attribute_accessors'
3
+
4
+ # Resque workers strategy
5
+ # loading system
6
+ module WorkersLoader
7
+ mattr_accessor :workers_paths
8
+ @@workers_paths = []
9
+
10
+ mattr_accessor :workers
11
+ @@workers = []
12
+
13
+ class << self
14
+ def add_path(path)
15
+ 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?)
23
+ end
24
+
25
+ def load_workers!
26
+ workers_paths.each do |path|
27
+ workers_in_path = find(path)
28
+ next if workers_in_path.empty?
29
+
30
+ duplacates = workers_in_path
31
+ .select { |worker| workers.include?(worker) }
32
+ if duplacates.any?
33
+ fail("Workers already present! #{duplacates.join(', ')}")
34
+ end
35
+
36
+ self.workers += workers_in_path
37
+ end
38
+ end
39
+ end
40
+
41
+ autoload :Path, 'workers_loader/path'
42
+ end
@@ -0,0 +1,24 @@
1
+ # encoding: utf-8
2
+ require 'spec_helper'
3
+
4
+ describe WorkersLoader::Path do
5
+ let(:base) do
6
+ File.join(File.dirname(__FILE__), '..', '..', 'support', 'dummy')
7
+ end
8
+ subject { described_class.new(base) }
9
+
10
+ describe '#files' do
11
+ let(:files) { %w(dummy/bar/base dummy/foo dummy/bar/baz).sort }
12
+ it { expect(subject.files.sort).to eq(files) }
13
+ end
14
+
15
+ describe '#class_for' do
16
+ it { expect(subject.class_for('dummy/foo')).to eq(Dummy::Foo) }
17
+ it { expect(subject.class_for('dummy/bar/baz')).to eq(Dummy::Bar::Baz) }
18
+ end
19
+
20
+ describe '#queue_for' do
21
+ it { expect(subject.queue_for('dummy/foo')).to eq(:dummy_foo) }
22
+ it { expect(subject.queue_for('dummy/bar/baz')).to eq(:baz_queue) }
23
+ end
24
+ end
@@ -0,0 +1,46 @@
1
+ # encoding: utf-8
2
+ require 'spec_helper'
3
+
4
+ describe WorkersLoader do
5
+ before do
6
+ described_class.workers_paths = []
7
+ described_class.workers = []
8
+ end
9
+ let(:workers_path) do
10
+ File.join(File.dirname(__FILE__), '..', 'support', 'dummy')
11
+ end
12
+ subject { described_class.workers_paths }
13
+
14
+ it { expect(subject).to eq([]) }
15
+
16
+ describe '::add_path' do
17
+ before { described_class.add_path(workers_path) }
18
+
19
+ it { expect(described_class.workers_paths).to eq([workers_path]) }
20
+
21
+ context 'dubplicate worker' do
22
+ let(:message) { 'Directory not found: `foo`' }
23
+ it { expect { described_class.add_path('foo') }.to raise_error(message) }
24
+ end
25
+ end
26
+
27
+ describe '::find' do
28
+ subject { described_class.find(workers_path) }
29
+
30
+ it { expect(subject.sort).to eq([:dummy_foo, :baz_queue].sort) }
31
+ end
32
+
33
+ describe '::load_workers!' do
34
+ before do
35
+ described_class.add_path(workers_path)
36
+ described_class.load_workers!
37
+ end
38
+
39
+ it { expect(described_class.workers).to eq([:baz_queue, :dummy_foo]) }
40
+
41
+ context 'prevent duplicates' do
42
+ let(:message) { 'Workers already present! baz_queue, dummy_foo' }
43
+ it { expect { described_class.load_workers! }.to raise_error(message) }
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,16 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rspec'
4
+ require 'factory_girl_rails'
5
+ require 'workers_loader'
6
+
7
+ Rails.backtrace_cleaner.remove_silencers!
8
+
9
+ # Load support files
10
+ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each { |f| require f }
11
+
12
+ RSpec.configure do |config|
13
+ config.mock_with :rspec
14
+
15
+ config.order = 'random'
16
+ end
@@ -0,0 +1,9 @@
1
+ # encoding: utf-8
2
+
3
+ module Dummy
4
+ module Bar
5
+ class Base
6
+ # not a worker
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,9 @@
1
+ # encoding: utf-8
2
+
3
+ module Dummy
4
+ module Bar
5
+ class Baz
6
+ @queue = :baz_queue
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,7 @@
1
+ # encoding: utf-8
2
+
3
+ module Dummy
4
+ class Foo
5
+ @queue = :dummy_foo
6
+ end
7
+ end
metadata ADDED
@@ -0,0 +1,104 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: workers_loader
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Ricard Forniol
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-01-30 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: 3.2.21
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: 3.2.21
27
+ - !ruby/object:Gem::Dependency
28
+ name: rspec-rails
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ! '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ! '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: factory_girl_rails
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ! '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: Resque workers loading strategy system
56
+ email:
57
+ - ricard.forniol@pushtech.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - MIT-LICENSE
63
+ - README
64
+ - Rakefile
65
+ - lib/workers_loader.rb
66
+ - lib/workers_loader/path.rb
67
+ - lib/workers_loader/version.rb
68
+ - spec/lib/workers_loader/path_spec.rb
69
+ - spec/lib/workers_loader_spec.rb
70
+ - spec/spec_helper.rb
71
+ - spec/support/dummy/bar/base.rb
72
+ - spec/support/dummy/bar/baz.rb
73
+ - spec/support/dummy/foo.rb
74
+ homepage: http://www.pushtech.com
75
+ licenses: []
76
+ metadata: {}
77
+ post_install_message:
78
+ rdoc_options: []
79
+ require_paths:
80
+ - lib
81
+ required_ruby_version: !ruby/object:Gem::Requirement
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ required_rubygems_version: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - ! '>='
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ requirements: []
92
+ rubyforge_project:
93
+ rubygems_version: 2.4.5
94
+ signing_key:
95
+ specification_version: 4
96
+ summary: Resque loading system
97
+ test_files:
98
+ - spec/lib/workers_loader/path_spec.rb
99
+ - spec/lib/workers_loader_spec.rb
100
+ - spec/spec_helper.rb
101
+ - spec/support/dummy/bar/base.rb
102
+ - spec/support/dummy/bar/baz.rb
103
+ - spec/support/dummy/foo.rb
104
+ has_rdoc: