resque-async-method 1.0.0

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.
data/MIT-LICENSE ADDED
@@ -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.
data/README.md ADDED
@@ -0,0 +1,50 @@
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 preprocess_avatar_for_a_long_time
16
+ # do stuff
17
+ end
18
+ async_method :preprocess_avatar_for_a_long_time
19
+
20
+ def send_a_very_long_email
21
+ # do stuff
22
+ end
23
+ async_method :send_a_very_long_email, queue: 'emails'
24
+ end
25
+
26
+ u = User.find(1)
27
+
28
+ u.preprocess_avatar_for_a_long_time # => queued in 'users' queue
29
+ u.send_a_very_long_email # => queued in 'emails' queue
30
+ u.sync_send_a_very_long_email # => happens right away!
31
+
32
+ Note that in the test environment, none of this magic happens. You can test the expected output immediately.
33
+
34
+ Method return values will change. `Resque.enqueue` will return `[]` from an async'ed method.
35
+
36
+ Sometimes it's nice to async a method that you're including from a module:
37
+
38
+ module MyExtension
39
+ extend ActiveSupport::Concern
40
+
41
+ include Resque::Plugins::Async::Method
42
+
43
+ included do
44
+ async_method :generate_matrix, queue: 'matrices'
45
+ end
46
+
47
+ def generate_matrix
48
+ # do stuff
49
+ end
50
+ end
data/Rakefile ADDED
@@ -0,0 +1,34 @@
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
+ RDoc::Task.new(:rdoc) do |rdoc|
16
+ rdoc.rdoc_dir = 'rdoc'
17
+ rdoc.title = 'Resque::Plugins::Async::Method'
18
+ rdoc.options << '--line-numbers' << '--inline-source'
19
+ rdoc.rdoc_files.include('README.rdoc')
20
+ rdoc.rdoc_files.include('lib/**/*.rb')
21
+ end
22
+
23
+
24
+ require 'rake/testtask'
25
+
26
+ Rake::TestTask.new(:test) do |t|
27
+ t.libs << 'lib'
28
+ t.libs << 'test'
29
+ t.pattern = 'test/**/*_test.rb'
30
+ t.verbose = false
31
+ end
32
+
33
+
34
+ task :default => :test
@@ -0,0 +1,14 @@
1
+ require 'active_support/dependencies'
2
+
3
+ module Resque
4
+ module Plugins
5
+ module Async
6
+ autoload :Method, 'resque/plugins/async/method'
7
+ autoload :Worker, 'resque/plugins/async/worker'
8
+ end
9
+ end
10
+ end
11
+
12
+ ActiveSupport.on_load(:active_record) do
13
+ include Resque::Plugins::Async::Method
14
+ end
@@ -0,0 +1,29 @@
1
+ require 'resque/plugins/async/worker'
2
+
3
+ module Resque::Plugins::Async::Method
4
+ extend ActiveSupport::Concern
5
+
6
+ module ClassMethods
7
+ def async_method(method_name, opts={})
8
+ # Allow tests to call sync_ methods ...
9
+ alias_method :"sync_#{method_name}", method_name
10
+
11
+ # ... but don't actually make them asynchronous
12
+ return if Rails.env.test?
13
+
14
+ define_method "#{method_name}" do |*args|
15
+ my_klass = Resque::Plugins::Async::Worker
16
+ my_klass.queue = opts[:queue] ||
17
+ send(:class).name.underscore.pluralize
18
+
19
+ Resque.enqueue(
20
+ my_klass,
21
+ send(:class).name,
22
+ send(:id),
23
+ :"sync_#{method_name}",
24
+ *args
25
+ )
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,15 @@
1
+ class Resque::Plugins::Async::Worker
2
+ @queue = :async_methods
3
+
4
+ def self.queue=(name)
5
+ @queue = name
6
+ end
7
+
8
+ def self.queue
9
+ @queue
10
+ end
11
+
12
+ def self.perform(klass, *args)
13
+ klass.constantize.find(args.shift).send(args.shift, *args)
14
+ end
15
+ end
metadata ADDED
@@ -0,0 +1,85 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: resque-async-method
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Nick Ragaz
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2011-06-14 00:00:00.000000000 -04:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: resque
17
+ requirement: &2161486000 !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: *2161486000
26
+ - !ruby/object:Gem::Dependency
27
+ name: activesupport
28
+ requirement: &2161485560 !ruby/object:Gem::Requirement
29
+ none: false
30
+ requirements:
31
+ - - ! '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: *2161485560
37
+ - !ruby/object:Gem::Dependency
38
+ name: sqlite3
39
+ requirement: &2161485140 !ruby/object:Gem::Requirement
40
+ none: false
41
+ requirements:
42
+ - - ! '>='
43
+ - !ruby/object:Gem::Version
44
+ version: '0'
45
+ type: :development
46
+ prerelease: false
47
+ version_requirements: *2161485140
48
+ description: Make Active Record instance methods asynchronous using resque.
49
+ email: nick.ragaz@gmail.com
50
+ executables: []
51
+ extensions: []
52
+ extra_rdoc_files: []
53
+ files:
54
+ - lib/resque/plugins/async/method.rb
55
+ - lib/resque/plugins/async/worker.rb
56
+ - lib/resque-async-method.rb
57
+ - MIT-LICENSE
58
+ - Rakefile
59
+ - README.md
60
+ has_rdoc: true
61
+ homepage: http://github.com/nragaz/resque-async-method
62
+ licenses: []
63
+ post_install_message:
64
+ rdoc_options: []
65
+ require_paths:
66
+ - lib
67
+ required_ruby_version: !ruby/object:Gem::Requirement
68
+ none: false
69
+ requirements:
70
+ - - ! '>='
71
+ - !ruby/object:Gem::Version
72
+ version: '0'
73
+ required_rubygems_version: !ruby/object:Gem::Requirement
74
+ none: false
75
+ requirements:
76
+ - - ! '>='
77
+ - !ruby/object:Gem::Version
78
+ version: '0'
79
+ requirements: []
80
+ rubyforge_project:
81
+ rubygems_version: 1.6.2
82
+ signing_key:
83
+ specification_version: 3
84
+ summary: Make Active Record instance methods asynchronous using resque.
85
+ test_files: []