resque-async 0.0.1

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 555fdeb1336e28605c85c5f7dbf18f9404268b68
4
+ data.tar.gz: 943b9de28b6b313eab38095af0409ef66d9e283f
5
+ SHA512:
6
+ metadata.gz: cd4638a92cd91566c94f69ec7010649b427ea65c5e1d54443e821700bb711e8e3740a8249f10089d03ea54a1a6e3413e81d5d01145b7ac5a2a65161633beee13
7
+ data.tar.gz: bc7bffbe9b6690a889065ea235d6f43a0b7b3236bd567c12e03011491d6aac0f97025607b45c19438f8fad141eb62a6c87a8106bbfedd40523901612ea3d991f
data/.gitignore ADDED
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --require spec_helper
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in resque-async.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Greg Dean
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,54 @@
1
+ # ResqueAsync
2
+
3
+ A simple way to invoke methods asynchronously using Resque. There are a few gems that provide similar functionality.
4
+ However We like the more declarative syntax with this implementation. As it gives the caller control over the invocation
5
+
6
+ ## Installation
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ ```ruby
11
+ gem 'resque-async'
12
+ ```
13
+
14
+ And then execute:
15
+
16
+ $ bundle
17
+
18
+ Or install it yourself as:
19
+
20
+ $ gem install resque-async
21
+
22
+ ## Usage
23
+
24
+ Supports Class Methods and ActiveRecord instance methods
25
+
26
+ ```ruby
27
+ MyObject.async(:high).slow_class_method
28
+ record_instance.async(:medium).slow_instance_method
29
+ ```
30
+
31
+ Support high, medium, low priority out of the box. As well as custom work/queue
32
+
33
+ ```ruby
34
+ MyObject.async(:high).high_priority
35
+ MyObject.async(:medium).medium_priority
36
+ MyObject.async(:low).low_priority
37
+ ```
38
+ OR
39
+ ```ruby
40
+ class MyCustomerWorker
41
+ extend ResqueAsync::Workers::AsyncClassWorker
42
+ @queue :crazy
43
+ end
44
+ MyObject.async(MyCustomerWorker).crazy_priority
45
+ ```
46
+
47
+
48
+ ## Contributing
49
+
50
+ 1. Fork it ( https://github.com/[my-github-username]/resque-async/fork )
51
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
52
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
53
+ 4. Push to the branch (`git push origin my-new-feature`)
54
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,55 @@
1
+ require 'resque'
2
+
3
+ module ResqueAsync
4
+ module Enqueuers
5
+
6
+ class ClassEnqueuer
7
+ attr_reader :host_class, :priority
8
+ def initialize(host_class, priority)
9
+ @host_class = host_class
10
+ @priority = priority
11
+ end
12
+
13
+ def method_missing(methId, *args)
14
+ # call super unless the host responds to the method
15
+ return super unless @host_class.respond_to?(methId.id2name.to_sym)
16
+
17
+ case @priority
18
+ when Class
19
+ worker = @priority
20
+ when :high
21
+ worker = Workers::HighPriorityClassMethod
22
+ when :medium
23
+ worker = Workers::MediumPriorityClassMethod
24
+ when :low
25
+ worker = Workers::LowPriorityClassMethod
26
+ else
27
+ raise StandardError('Unknown priority')
28
+ end
29
+ # enqueue
30
+ Resque.enqueue(worker, @host_class.name, methId.id2name, args)
31
+ end
32
+ end
33
+
34
+ class ActiveRecordEnqueuer < ClassEnqueuer
35
+ def initialize(host_class, priority, id)
36
+ super(host_class, priority)
37
+ @id = id
38
+ end
39
+
40
+ def method_missing(methId, *args)
41
+ return super(methId, args) unless @host_class.method_defined?(methId)
42
+ self.class::async(@priority).find_and_send(@host_class.name, @id, methId.id2name, args)
43
+ end
44
+
45
+ def self.find_and_send(class_name, id, method_name, args)
46
+ record = class_name.constantize.find(id)
47
+ if args.empty?
48
+ record.send(method_name)
49
+ else
50
+ record.send(method_name, *args)
51
+ end
52
+ end
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,17 @@
1
+ require 'resque-async/enqueuers'
2
+
3
+ module ResqueAsync
4
+ module Integrations
5
+ module ActiveRecord
6
+ def async(priority)
7
+ Enqueuers::ActiveRecordEnqueuer.new(self.class, priority, self.id)
8
+ end
9
+ end
10
+
11
+ module Core
12
+ def async(priority)
13
+ Enqueuers::ClassEnqueuer.new(self, priority)
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,3 @@
1
+ module ResqueAsync
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,29 @@
1
+ module ResqueAsync
2
+ module Workers
3
+
4
+ module AsyncClassWorker
5
+ def perform(klass, method, args = [])
6
+ if args.empty?
7
+ klass.constantize.send(method)
8
+ else
9
+ klass.constantize.send(method, *args)
10
+ end
11
+ end
12
+ end
13
+
14
+ class HighPriorityClassMethod
15
+ extend AsyncClassWorker
16
+ @queue = :high
17
+ end
18
+
19
+ class LowPriorityClassMethod
20
+ extend AsyncClassWorker
21
+ @queue = :low
22
+ end
23
+
24
+ class MediumPriorityClassMethod
25
+ extend AsyncClassWorker
26
+ @queue = :medium
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,20 @@
1
+ require 'resque-async/version'
2
+ require 'resque-async/integrations'
3
+ require 'resque-async/workers'
4
+
5
+
6
+ module ResqueAsync
7
+
8
+ end
9
+
10
+ class Module
11
+ include ResqueAsync::Integrations::Core
12
+ end
13
+
14
+ if defined? ::ActiveRecord
15
+ class ActiveRecord::Base
16
+ include ResqueAsync::Integrations::ActiveRecord
17
+ end
18
+ end
19
+
20
+
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'resque-async/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "resque-async"
8
+ spec.version = ResqueAsync::VERSION
9
+ spec.authors = ["Greg Dean"]
10
+ spec.email = ["dean.greg@gmail.com"]
11
+ spec.summary = %q{Simple way of invoking methods asynchronously using resque.}
12
+ spec.homepage = ""
13
+ spec.license = "MIT"
14
+
15
+ spec.files = `git ls-files -z`.split("\x0")
16
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
17
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
18
+ spec.require_paths = ["lib"]
19
+
20
+ spec.add_dependency "resque"
21
+ spec.add_development_dependency "bundler", "~> 1.7"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+ spec.add_development_dependency "rspec"
24
+ spec.add_development_dependency "resque_spec"
25
+ end
@@ -0,0 +1,6 @@
1
+ require 'support/async'
2
+
3
+ describe ActiveRecord::Base do
4
+ it_behaves_like 'async'
5
+ it { expect(subject.async(:anything)).to be_kind_of(ResqueAsync::Enqueuers::ActiveRecordEnqueuer)}
6
+ end
@@ -0,0 +1,10 @@
1
+ require 'support/enqueur'
2
+ require 'support/mock_host'
3
+
4
+ describe 'Enqueuers' do
5
+ describe ResqueAsync::Enqueuers::ClassEnqueuer do
6
+ it_behaves_like 'enqueuer' do
7
+ subject{ ResqueAsync::Enqueuers::ClassEnqueuer.new(MockHost, :high)}
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,6 @@
1
+ require 'support/async'
2
+
3
+ describe Module do
4
+ it_behaves_like 'async'
5
+ it { expect(subject.async(:anything)).to be_kind_of(ResqueAsync::Enqueuers::ClassEnqueuer)}
6
+ end
@@ -0,0 +1,106 @@
1
+
2
+ # This is kind of an ugly way to mock ActiveRecord for specs
3
+ # The real ActiveRecord comes with tons of dependencies that are hard to mock.
4
+ # For our purposes this seems like a reasonable compromise
5
+ # NOTE: I tried with no avail to mock rails and db dependencies
6
+ module ActiveRecord
7
+ class Base
8
+ def id
9
+ 0
10
+ end
11
+ end
12
+ end
13
+
14
+
15
+ require 'resque-async'
16
+ require 'resque_spec'
17
+
18
+ # This file was generated by the `rspec --init` command. Conventionally, all
19
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
20
+ # The generated `.rspec` file contains `--require spec_helper` which will cause this
21
+ # file to always be loaded, without a need to explicitly require it in any files.
22
+ #
23
+ # Given that it is always loaded, you are encouraged to keep this file as
24
+ # light-weight as possible. Requiring heavyweight dependencies from this file
25
+ # will add to the boot time of your test suite on EVERY test run, even for an
26
+ # individual file that may not need all of that loaded. Instead, consider making
27
+ # a separate helper file that requires the additional dependencies and performs
28
+ # the additional setup, and require it from the spec files that actually need it.
29
+ #
30
+ # The `.rspec` file also contains a few flags that are not defaults but that
31
+ # users commonly want.
32
+ #
33
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
34
+ RSpec.configure do |config|
35
+ # rspec-expectations config goes here. You can use an alternate
36
+ # assertion/expectation library such as wrong or the stdlib/minitest
37
+ # assertions if you prefer.
38
+ config.expect_with :rspec do |expectations|
39
+ # This option will default to `true` in RSpec 4. It makes the `description`
40
+ # and `failure_message` of custom matchers include text for helper methods
41
+ # defined using `chain`, e.g.:
42
+ # be_bigger_than(2).and_smaller_than(4).description
43
+ # # => "be bigger than 2 and smaller than 4"
44
+ # ...rather than:
45
+ # # => "be bigger than 2"
46
+ expectations.include_chain_clauses_in_custom_matcher_descriptions = true
47
+ end
48
+
49
+ # rspec-mocks config goes here. You can use an alternate test double
50
+ # library (such as bogus or mocha) by changing the `mock_with` option here.
51
+ config.mock_with :rspec do |mocks|
52
+ # Prevents you from mocking or stubbing a method that does not exist on
53
+ # a real object. This is generally recommended, and will default to
54
+ # `true` in RSpec 4.
55
+ mocks.verify_partial_doubles = true
56
+ end
57
+
58
+ # The settings below are suggested to provide a good initial experience
59
+ # with RSpec, but feel free to customize to your heart's content.
60
+ =begin
61
+ # These two settings work together to allow you to limit a spec run
62
+ # to individual examples or groups you care about by tagging them with
63
+ # `:focus` metadata. When nothing is tagged with `:focus`, all examples
64
+ # get run.
65
+ config.filter_run :focus
66
+ config.run_all_when_everything_filtered = true
67
+
68
+ # Limits the available syntax to the non-monkey patched syntax that is recommended.
69
+ # For more details, see:
70
+ # - http://myronmars.to/n/dev-blog/2012/06/rspecs-new-expectation-syntax
71
+ # - http://teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
72
+ # - http://myronmars.to/n/dev-blog/2014/05/notable-changes-in-rspec-3#new__config_option_to_disable_rspeccore_monkey_patching
73
+ config.disable_monkey_patching!
74
+
75
+ # This setting enables warnings. It's recommended, but in some cases may
76
+ # be too noisy due to issues in dependencies.
77
+ config.warnings = true
78
+
79
+ # Many RSpec users commonly either run the entire suite or an individual
80
+ # file, and it's useful to allow more verbose output when running an
81
+ # individual spec file.
82
+ if config.files_to_run.one?
83
+ # Use the documentation formatter for detailed output,
84
+ # unless a formatter has already been configured
85
+ # (e.g. via a command-line flag).
86
+ config.default_formatter = 'doc'
87
+ end
88
+
89
+ # Print the 10 slowest examples and example groups at the
90
+ # end of the spec run, to help surface which specs are running
91
+ # particularly slow.
92
+ config.profile_examples = 10
93
+
94
+ # Run specs in random order to surface order dependencies. If you find an
95
+ # order dependency and want to debug it, you can fix the order by providing
96
+ # the seed, which is printed after each run.
97
+ # --seed 1234
98
+ config.order = :random
99
+
100
+ # Seed global randomization in this process using the `--seed` CLI option.
101
+ # Setting this allows you to use `--seed` to deterministically reproduce
102
+ # test failures related to randomization by passing the same `--seed` value
103
+ # as the one that triggered the failure.
104
+ Kernel.srand config.seed
105
+ =end
106
+ end
@@ -0,0 +1,3 @@
1
+ shared_examples 'async' do
2
+ it { expect(subject).to respond_to(:async) }
3
+ end
@@ -0,0 +1,4 @@
1
+ shared_examples 'enqueuer' do
2
+ it { expect{ subject.not_a_method }.to raise_error(NoMethodError)}
3
+ it { expect{ subject.async_class_method }.to_not raise_error }
4
+ end
@@ -0,0 +1,5 @@
1
+ class MockHost
2
+ def self.async_class_method
3
+
4
+ end
5
+ end
@@ -0,0 +1,3 @@
1
+ shared_examples 'worker' do
2
+ it { expect(described_class).to respond_to(:perform)}
3
+ end
@@ -0,0 +1,17 @@
1
+ require 'support/worker'
2
+
3
+ describe 'Workers' do
4
+
5
+ describe ResqueAsync::Workers::HighPriorityClassMethod do
6
+ it_behaves_like 'worker'
7
+ it { expect(Resque.queue_from_class(described_class)).to be(:high)}
8
+ end
9
+ describe ResqueAsync::Workers::MediumPriorityClassMethod do
10
+ it_behaves_like 'worker'
11
+ it { expect(Resque.queue_from_class(described_class)).to be(:medium)}
12
+ end
13
+ describe ResqueAsync::Workers::LowPriorityClassMethod do
14
+ it_behaves_like 'worker'
15
+ it { expect(Resque.queue_from_class(described_class)).to be(:low)}
16
+ end
17
+ end
metadata ADDED
@@ -0,0 +1,145 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: resque-async
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Greg Dean
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-11-06 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: resque
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ! '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ! '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: '1.7'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: '1.7'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ! '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: resque_spec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ! '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ! '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description:
84
+ email:
85
+ - dean.greg@gmail.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - .gitignore
91
+ - .rspec
92
+ - Gemfile
93
+ - LICENSE.txt
94
+ - README.md
95
+ - Rakefile
96
+ - lib/resque-async.rb
97
+ - lib/resque-async/enqueuers.rb
98
+ - lib/resque-async/integrations.rb
99
+ - lib/resque-async/version.rb
100
+ - lib/resque-async/workers.rb
101
+ - resque-async.gemspec
102
+ - spec/active_record_spec.rb
103
+ - spec/enqueuers_spec.rb
104
+ - spec/module_spec.rb
105
+ - spec/spec_helper.rb
106
+ - spec/support/async.rb
107
+ - spec/support/enqueur.rb
108
+ - spec/support/mock_host.rb
109
+ - spec/support/worker.rb
110
+ - spec/workers_spec.rb
111
+ homepage: ''
112
+ licenses:
113
+ - MIT
114
+ metadata: {}
115
+ post_install_message:
116
+ rdoc_options: []
117
+ require_paths:
118
+ - lib
119
+ required_ruby_version: !ruby/object:Gem::Requirement
120
+ requirements:
121
+ - - ! '>='
122
+ - !ruby/object:Gem::Version
123
+ version: '0'
124
+ required_rubygems_version: !ruby/object:Gem::Requirement
125
+ requirements:
126
+ - - ! '>='
127
+ - !ruby/object:Gem::Version
128
+ version: '0'
129
+ requirements: []
130
+ rubyforge_project:
131
+ rubygems_version: 2.0.3
132
+ signing_key:
133
+ specification_version: 4
134
+ summary: Simple way of invoking methods asynchronously using resque.
135
+ test_files:
136
+ - spec/active_record_spec.rb
137
+ - spec/enqueuers_spec.rb
138
+ - spec/module_spec.rb
139
+ - spec/spec_helper.rb
140
+ - spec/support/async.rb
141
+ - spec/support/enqueur.rb
142
+ - spec/support/mock_host.rb
143
+ - spec/support/worker.rb
144
+ - spec/workers_spec.rb
145
+ has_rdoc: