resque-async-method-enhanced 1.3.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1 @@
1
+ *.gem
data/.rvmrc ADDED
@@ -0,0 +1 @@
1
+ rvm use ruby-1.9.3-p286@resque-async-method --create
@@ -0,0 +1,16 @@
1
+ # Changelog VERSION = '1.3.1'
2
+
3
+ * Change name due to the reluctance of owner of gem, Nick Ragaz to integrate this improved version of his *beautiful* code, bullshit...
4
+
5
+ # Changelog VERSION = '1.3'
6
+
7
+ * Switch to plugin resque-lock-timeout instead of sef implementation
8
+ * Add timeout feature
9
+
10
+ # Changelog VERSION = '1.2'
11
+
12
+ * Add lock flag
13
+
14
+ # Changelog VERSION = '1.1.1'
15
+
16
+ * Switch testunit to rspec
@@ -0,0 +1,9 @@
1
+ bundle && bundle exec rake spec
2
+ gem build resque-async-method-enhanced.gemspec
3
+ git tag -a v1.3.1 -m 'version 1.3.1'
4
+ git push --tags
5
+ gem push resque-async-method-enhanced-1.3.1.gem
6
+ git push origin master
7
+
8
+ extra:
9
+ bundle exec rspec spec/resque-async-method-enhanced/method_spec.rb -e 'Resque::Plugins::Async::Method for valid record with loner'
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in resque-async-method-enhanced.gemspec
4
+ gemspec
@@ -0,0 +1,54 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ resque-async-method-enhanced (1.3.1)
5
+ activesupport
6
+ resque
7
+ resque-lock-timeout
8
+
9
+ GEM
10
+ remote: https://rubygems.org/
11
+ specs:
12
+ activesupport (3.2.9)
13
+ i18n (~> 0.6)
14
+ multi_json (~> 1.0)
15
+ diff-lcs (1.1.3)
16
+ i18n (0.6.1)
17
+ multi_json (1.3.7)
18
+ rack (1.4.1)
19
+ rack-protection (1.2.0)
20
+ rack
21
+ rake (0.9.2.2)
22
+ redis (3.0.2)
23
+ redis-namespace (1.2.1)
24
+ redis (~> 3.0.0)
25
+ resque (1.23.0)
26
+ multi_json (~> 1.0)
27
+ redis-namespace (~> 1.0)
28
+ sinatra (>= 0.9.2)
29
+ vegas (~> 0.1.2)
30
+ resque-lock-timeout (0.4.1)
31
+ resque (>= 1.8.0)
32
+ rspec (2.11.0)
33
+ rspec-core (~> 2.11.0)
34
+ rspec-expectations (~> 2.11.0)
35
+ rspec-mocks (~> 2.11.0)
36
+ rspec-core (2.11.1)
37
+ rspec-expectations (2.11.3)
38
+ diff-lcs (~> 1.1.3)
39
+ rspec-mocks (2.11.3)
40
+ sinatra (1.3.3)
41
+ rack (~> 1.3, >= 1.3.6)
42
+ rack-protection (~> 1.2)
43
+ tilt (~> 1.3, >= 1.3.3)
44
+ tilt (1.3.3)
45
+ vegas (0.1.11)
46
+ rack (>= 1.0.0)
47
+
48
+ PLATFORMS
49
+ ruby
50
+
51
+ DEPENDENCIES
52
+ rake
53
+ resque-async-method-enhanced!
54
+ rspec
@@ -0,0 +1,20 @@
1
+ Copyright 2011 Nick Ragaz
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.
@@ -0,0 +1,86 @@
1
+ Resque::Plugins::Async::Method
2
+ ==============================
3
+
4
+ Make Active Record instance methods asynchronous using [resque](http://www.github.com/defunkt/resque).
5
+
6
+ Works with Ruby ~> 1.9, Rails ~> 3 and Resque ~> 1.17. (Probably works with earlier versions too -- but why?)
7
+
8
+ Usage
9
+ -----
10
+
11
+ class User < ActiveRecord::Base
12
+ # Not needed! This is done using a hook on ActiveRecord::Base.
13
+ # include Resque::Plugins::Async::Method
14
+
15
+ def process_avatar
16
+ # do stuff
17
+ end
18
+ async_method :process_avatar
19
+
20
+ end
21
+
22
+ u = User.find(1)
23
+
24
+ u.process_avatar # => queued in 'users' queue
25
+
26
+ # You can call previous method in sync mode by :
27
+ u.sync_process_avatar # => happens right away!
28
+
29
+ Note that in the test environment, none of this magic happens. You can test the expected output immediately.
30
+
31
+ Method return values will change. `Resque.enqueue` will return `[]` from an async'ed method.
32
+
33
+ ## In Module extension
34
+
35
+ Sometimes it's nice to async a method that you're including from a module:
36
+
37
+ module MyExtension
38
+ extend ActiveSupport::Concern
39
+
40
+ include Resque::Plugins::Async::Method
41
+
42
+ included do
43
+ async_method :generate_matrix, queue: 'matrices'
44
+ end
45
+
46
+ def generate_matrix
47
+ # do stuff
48
+ end
49
+ end
50
+
51
+ # Concurrently protection
52
+
53
+ You can protect for concurrently processing in background
54
+
55
+ For this, you must indicate which classes should not make any treatment concurrently :
56
+
57
+ class User < ActiveRecord::Base
58
+ ...
59
+
60
+ def process_avatar
61
+ # do stuff
62
+ end
63
+ async_method :process_avatar, loner: true
64
+
65
+ end
66
+
67
+ You can also set a timeout in seconds, when you pass key :lock_timeout the key :loner is useless but you can set it anyway.
68
+
69
+ class User < ActiveRecord::Base
70
+ ...
71
+
72
+ def process_avatar
73
+ # do stuff
74
+ end
75
+ async_method :process_avatar, lock_timeout: 3600
76
+
77
+ end
78
+
79
+
80
+ Changelog
81
+ ---------
82
+ * 1.3: Technical enhancement, switch to plugin resque-lock-timeout
83
+ * 1.2: Add flaging system
84
+ * 1.1.1: Switch to Rspec test suite.
85
+ * 1.0.1: Update for latest Resque API (true returned from successful queue)
86
+ * 1.0.0: Initial release
@@ -0,0 +1,27 @@
1
+ #!/usr/bin/env rake
2
+
3
+ require 'bundler/gem_tasks'
4
+
5
+ begin
6
+ require 'rdoc/task'
7
+ rescue LoadError
8
+ require 'rdoc/rdoc'
9
+ require 'rake/rdoctask'
10
+ RDoc::Task = Rake::RDocTask
11
+ end
12
+
13
+ RDoc::Task.new(:rdoc) do |rdoc|
14
+ rdoc.rdoc_dir = 'rdoc'
15
+ rdoc.title = 'Resque::Plugins::Async::Method'
16
+ rdoc.options << '--line-numbers' << '--inline-source'
17
+ rdoc.rdoc_files.include('README.rdoc')
18
+ rdoc.rdoc_files.include('lib/**/*.rb')
19
+ end
20
+
21
+ require 'rspec/core/rake_task'
22
+
23
+ RSpec::Core::RakeTask.new(:spec) do |spec|
24
+ spec.rspec_opts = ['--color --format nested --fail-fast']
25
+ end
26
+
27
+ task :default => :spec
@@ -0,0 +1,16 @@
1
+ require 'resque-async-method-enhanced/version'
2
+ require 'active_support/concern'
3
+ require 'active_support/dependencies/autoload'
4
+
5
+ module Resque
6
+ module Plugins
7
+ module Async
8
+ autoload :Method, 'resque/plugins/async/method'
9
+ autoload :Worker, 'resque/plugins/async/worker'
10
+ end
11
+ end
12
+ end
13
+
14
+ ActiveSupport.on_load(:active_record) do
15
+ include Resque::Plugins::Async::Method
16
+ end
@@ -0,0 +1,3 @@
1
+ module ResqueAsyncMethodEnhanced
2
+ VERSION = '1.3.1'
3
+ end
@@ -0,0 +1,36 @@
1
+ require 'resque/plugins/async/worker'
2
+
3
+ module Resque::Plugins::Async::Method
4
+ extend ActiveSupport::Concern
5
+
6
+ class NotPersistedError < StandardError; end
7
+
8
+ module ClassMethods
9
+
10
+ def async_method method_name, options = {}
11
+
12
+ puts "options => #{options}"
13
+ alias_method :"sync_#{method_name}", method_name # Allow tests to call sync_ methods ...
14
+
15
+ return if Rails.env.test? # ... but don't actually make them asynchronous
16
+
17
+ define_method method_name do |*args|
18
+ raise NotPersistedError, "Methods can only be async'ed on persisted records (currently: #{inspect})" unless self.persisted?
19
+
20
+ my_klass = Resque::Plugins::Async::Worker
21
+ my_klass.queue = options[:queue] || __send__(:class).name.underscore.pluralize.to_sym
22
+ my_klass.loner = options[:loner] || false
23
+ my_klass.lock_timeout = options[:lock_timeout] || 0
24
+ my_klass.loner = true if my_klass.lock_timeout > 0 # Magick config
25
+
26
+ Resque.enqueue(
27
+ my_klass,
28
+ __send__(:class).name,
29
+ __send__(:id),
30
+ "sync_#{method_name}".to_sym,
31
+ *args
32
+ )
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,20 @@
1
+ require 'resque-lock-timeout'
2
+
3
+ class Resque::Plugins::Async::Worker
4
+ extend Resque::Plugins::LockTimeout
5
+
6
+ @queue = :async_methods
7
+ @loner = false
8
+ @lock_timeout = 0
9
+
10
+ class << self
11
+
12
+ attr_accessor :queue
13
+ attr_accessor :loner
14
+ attr_accessor :lock_timeout
15
+
16
+ def perform klass, *args
17
+ klass.constantize.find(args.shift).send(args.shift, *args)
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,23 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/resque-async-method-enhanced/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ['Joel AZEMAR', 'Nick Ragaz']
6
+ gem.email = ['joel.azemar@gmail.com', 'nick.ragaz@gmail.com']
7
+ gem.description = %q{Make Active Support instance methods asynchronous using resque.}
8
+ gem.summary = %q{Make Active Support instance methods asynchronous using resque.}
9
+ gem.homepage = 'http://github.com/joel/resque-async-method-enhanced'
10
+ gem.licenses = [ 'MIT' ]
11
+ gem.files = `git ls-files`.split($\)
12
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
13
+ gem.test_files = gem.files.grep(%r{^(spec)/})
14
+ gem.name = 'resque-async-method-enhanced'
15
+ gem.require_paths = ['lib']
16
+ gem.version = ResqueAsyncMethodEnhanced::VERSION
17
+
18
+ gem.add_runtime_dependency 'resque'
19
+ gem.add_runtime_dependency 'resque-lock-timeout'
20
+ gem.add_runtime_dependency 'activesupport'
21
+ gem.add_development_dependency 'rspec'
22
+ gem.add_development_dependency 'rake'
23
+ end
@@ -0,0 +1,56 @@
1
+ require 'spec_helper'
2
+
3
+ describe Resque::Plugins::Async::Method do
4
+
5
+ context 'for valid record' do
6
+
7
+ subject { MyKlass.new }
8
+
9
+ before { Rails.env.stub test?: false }
10
+
11
+ context 'with simple class' do
12
+
13
+ class MyKlass
14
+ include Resque::Plugins::Async::Method
15
+
16
+ def foo
17
+ 'bar'
18
+ end
19
+ async_method :foo
20
+
21
+ def persisted?
22
+ true
23
+ end
24
+
25
+ def id
26
+ 42
27
+ end
28
+ end
29
+
30
+ it { MyKlass.respond_to?(:async_method).should be_true }
31
+
32
+ context 'have defalut value' do
33
+ it { Resque::Plugins::Async::Worker.queue.should eql(:async_methods) }
34
+ it { Resque::Plugins::Async::Worker.loner.should be_false }
35
+ it { Resque::Plugins::Async::Worker.lock_timeout.should eql(0) }
36
+ end
37
+
38
+ context 'alsways can be call method on sync mode' do
39
+ it { subject.respond_to?(:sync_foo).should be_true }
40
+ it { subject.sync_foo.should eql('bar') }
41
+ end
42
+
43
+ context 'and on async mode' do
44
+
45
+ before do
46
+ Resque.should_receive(:enqueue).with(Resque::Plugins::Async::Worker, 'MyKlass', 42, :sync_foo).and_return('it works')
47
+ end
48
+
49
+ its(:foo) { should eql 'it works' }
50
+
51
+ end
52
+
53
+ end
54
+
55
+ end
56
+ end
@@ -0,0 +1,43 @@
1
+ require 'spec_helper'
2
+
3
+ describe Resque::Plugins::Async::Method do
4
+
5
+ context 'for valid record' do
6
+
7
+ subject { MyAwesomeKlass.new }
8
+
9
+ before { Rails.env.stub test?: false }
10
+
11
+ context 'with options' do
12
+
13
+ before { Resque.stub enqueue: nil }
14
+
15
+ class MyAwesomeKlass
16
+ include Resque::Plugins::Async::Method
17
+
18
+ def foo
19
+ 'bar'
20
+ end
21
+ async_method :foo, queue: :my_queue, loner: true, lock_timeout: 60
22
+
23
+ def persisted?
24
+ true
25
+ end
26
+
27
+ def id
28
+ 42
29
+ end
30
+ end
31
+
32
+ context 'should have options values' do
33
+
34
+ before { subject.foo }
35
+
36
+ it { Resque::Plugins::Async::Worker.queue.should eql(:my_queue) }
37
+ it { Resque::Plugins::Async::Worker.loner.should be_true }
38
+ it { Resque::Plugins::Async::Worker.lock_timeout.should eql(60) }
39
+ end
40
+
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,29 @@
1
+ require 'spec_helper'
2
+
3
+ describe Resque::Plugins::Async::Worker do
4
+
5
+ subject { Resque::Plugins::Async::Worker }
6
+
7
+ context 'queue' do
8
+ context 'should be change of value' do
9
+ before { subject.queue = :foo }
10
+ describe 'when set :foo' do
11
+ its(:queue) { should eql :foo }
12
+ end
13
+ end
14
+ end
15
+
16
+ class MyClass; end
17
+
18
+ context '#perform' do
19
+ let(:my_class_instance) { MyClass.new }
20
+
21
+ before do
22
+ MyClass.should_receive(:find).with(42).and_return(my_class_instance)
23
+ my_class_instance.should_receive(:foo).with('bar')
24
+ end
25
+
26
+ it('should be call corretly Resque#perform') { subject.perform('MyClass', 42, 'foo', 'bar') }
27
+ end
28
+
29
+ end
@@ -0,0 +1,27 @@
1
+ $:.unshift File.expand_path('..', __FILE__)
2
+ $:.unshift File.expand_path('../../lib', __FILE__)
3
+
4
+ require 'rspec'
5
+ require 'resque'
6
+ require 'active_support/concern'
7
+ require 'active_support/inflector'
8
+ require 'active_support/dependencies/autoload'
9
+ require 'resque-lock-timeout'
10
+ require 'resque-async-method-enhanced'
11
+
12
+ RSpec.configure do |config|
13
+ config.mock_with :rspec
14
+ end
15
+
16
+ RSpec.configuration.after(:all) do
17
+ Resque.inline = true
18
+ end
19
+
20
+ # Avoid call Rails.env.test? => false
21
+ class Env; def test?; false; end; end
22
+ class Logger; def info(s); puts(s); end; end
23
+
24
+ class Rails
25
+ def self.env; Env.new; end
26
+ def self.logger; Logger.new; end
27
+ end
metadata ADDED
@@ -0,0 +1,150 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: resque-async-method-enhanced
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.3.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Joel AZEMAR
9
+ - Nick Ragaz
10
+ autorequire:
11
+ bindir: bin
12
+ cert_chain: []
13
+ date: 2012-11-22 00:00:00.000000000 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: resque
17
+ requirement: !ruby/object:Gem::Requirement
18
+ none: false
19
+ requirements:
20
+ - - ! '>='
21
+ - !ruby/object:Gem::Version
22
+ version: '0'
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ none: false
27
+ requirements:
28
+ - - ! '>='
29
+ - !ruby/object:Gem::Version
30
+ version: '0'
31
+ - !ruby/object:Gem::Dependency
32
+ name: resque-lock-timeout
33
+ requirement: !ruby/object:Gem::Requirement
34
+ none: false
35
+ requirements:
36
+ - - ! '>='
37
+ - !ruby/object:Gem::Version
38
+ version: '0'
39
+ type: :runtime
40
+ prerelease: false
41
+ version_requirements: !ruby/object:Gem::Requirement
42
+ none: false
43
+ requirements:
44
+ - - ! '>='
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
47
+ - !ruby/object:Gem::Dependency
48
+ name: activesupport
49
+ requirement: !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ type: :runtime
56
+ prerelease: false
57
+ version_requirements: !ruby/object:Gem::Requirement
58
+ none: false
59
+ requirements:
60
+ - - ! '>='
61
+ - !ruby/object:Gem::Version
62
+ version: '0'
63
+ - !ruby/object:Gem::Dependency
64
+ name: rspec
65
+ requirement: !ruby/object:Gem::Requirement
66
+ none: false
67
+ requirements:
68
+ - - ! '>='
69
+ - !ruby/object:Gem::Version
70
+ version: '0'
71
+ type: :development
72
+ prerelease: false
73
+ version_requirements: !ruby/object:Gem::Requirement
74
+ none: false
75
+ requirements:
76
+ - - ! '>='
77
+ - !ruby/object:Gem::Version
78
+ version: '0'
79
+ - !ruby/object:Gem::Dependency
80
+ name: rake
81
+ requirement: !ruby/object:Gem::Requirement
82
+ none: false
83
+ requirements:
84
+ - - ! '>='
85
+ - !ruby/object:Gem::Version
86
+ version: '0'
87
+ type: :development
88
+ prerelease: false
89
+ version_requirements: !ruby/object:Gem::Requirement
90
+ none: false
91
+ requirements:
92
+ - - ! '>='
93
+ - !ruby/object:Gem::Version
94
+ version: '0'
95
+ description: Make Active Support instance methods asynchronous using resque.
96
+ email:
97
+ - joel.azemar@gmail.com
98
+ - nick.ragaz@gmail.com
99
+ executables: []
100
+ extensions: []
101
+ extra_rdoc_files: []
102
+ files:
103
+ - .gitignore
104
+ - .rvmrc
105
+ - CHANGELOG.md
106
+ - COMMANDS
107
+ - Gemfile
108
+ - Gemfile.lock
109
+ - MIT-LICENSE
110
+ - README.md
111
+ - Rakefile
112
+ - lib/resque-async-method-enhanced.rb
113
+ - lib/resque-async-method-enhanced/version.rb
114
+ - lib/resque/plugins/async/method.rb
115
+ - lib/resque/plugins/async/worker.rb
116
+ - resque-async-method-enhanced.gemspec
117
+ - spec/resque-async-method-enhanced/method_spec.rb
118
+ - spec/resque-async-method-enhanced/method_with_options_spec.rb
119
+ - spec/resque-async-method-enhanced/worker_spec.rb
120
+ - spec/spec_helper.rb
121
+ homepage: http://github.com/joel/resque-async-method-enhanced
122
+ licenses:
123
+ - MIT
124
+ post_install_message:
125
+ rdoc_options: []
126
+ require_paths:
127
+ - lib
128
+ required_ruby_version: !ruby/object:Gem::Requirement
129
+ none: false
130
+ requirements:
131
+ - - ! '>='
132
+ - !ruby/object:Gem::Version
133
+ version: '0'
134
+ required_rubygems_version: !ruby/object:Gem::Requirement
135
+ none: false
136
+ requirements:
137
+ - - ! '>='
138
+ - !ruby/object:Gem::Version
139
+ version: '0'
140
+ requirements: []
141
+ rubyforge_project:
142
+ rubygems_version: 1.8.24
143
+ signing_key:
144
+ specification_version: 3
145
+ summary: Make Active Support instance methods asynchronous using resque.
146
+ test_files:
147
+ - spec/resque-async-method-enhanced/method_spec.rb
148
+ - spec/resque-async-method-enhanced/method_with_options_spec.rb
149
+ - spec/resque-async-method-enhanced/worker_spec.rb
150
+ - spec/spec_helper.rb