resque-delayable 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENSE.txt +20 -0
- data/README.md +57 -0
- data/lib/resque-delayable/delayed_method.rb +54 -0
- data/lib/resque-delayable/serializer/active_record_serializer.rb +30 -0
- data/lib/resque-delayable/serializer/mongo_mapper_serializer.rb +34 -0
- data/lib/resque-delayable/serializer/mongoid_serializer.rb +30 -0
- data/lib/resque-delayable/version.rb +3 -0
- data/lib/resque-delayable.rb +76 -0
- metadata +132 -0
data/LICENSE.txt
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2011 James Smith
|
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,57 @@
|
|
1
|
+
resque-delayable
|
2
|
+
================
|
3
|
+
|
4
|
+
A resque plugin which adds a new method `rdelay` to objects. Methods called
|
5
|
+
with `rdelay` are executed asynchronously by resque, allowing your code to
|
6
|
+
continue.
|
7
|
+
|
8
|
+
Features
|
9
|
+
--------
|
10
|
+
- Adds `rdelay` instance and class methods to your models. Works with
|
11
|
+
ActiveRecord, Mongoid and MongoMapper objects out of the box
|
12
|
+
- Works with resque-scheduler. Schedule method execution for the future,
|
13
|
+
`obj.rdelay(:run_in => 5.minutes).method`
|
14
|
+
- Get `rdelay` on any class methods with `include ResqueDelayable`
|
15
|
+
- Create a custom seralizer plugin to get `rdelay` on your own instance methods
|
16
|
+
|
17
|
+
Installation
|
18
|
+
------------
|
19
|
+
- Add `gem "resque-delayable"` to your `Gemfile` or `gem install resque-delayable`
|
20
|
+
|
21
|
+
Examples
|
22
|
+
--------
|
23
|
+
|
24
|
+
class User
|
25
|
+
include ResqueDelayable
|
26
|
+
|
27
|
+
def self.recompute_caches() ...
|
28
|
+
def get(name) ...
|
29
|
+
def send_welcome_email ...
|
30
|
+
end
|
31
|
+
|
32
|
+
# Works with any serializable instance. ActiveRecord, Mongoid & MongoMapper
|
33
|
+
# are supported out of the box
|
34
|
+
u = User.get("james")
|
35
|
+
u.rdelay.send_welcome_email
|
36
|
+
|
37
|
+
# Works with all class methods
|
38
|
+
User.rdelay.recompute_caches
|
39
|
+
|
40
|
+
|
41
|
+
Contributing to resque-delayable
|
42
|
+
--------------------------------
|
43
|
+
|
44
|
+
* Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
|
45
|
+
* Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
|
46
|
+
* Fork the project
|
47
|
+
* Start a feature/bugfix branch
|
48
|
+
* Commit and push until you are happy with your contribution
|
49
|
+
* Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
|
50
|
+
* Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
|
51
|
+
|
52
|
+
Copyright
|
53
|
+
---------
|
54
|
+
|
55
|
+
Copyright (c) 2011 James Smith. See LICENSE.txt for
|
56
|
+
further details.
|
57
|
+
|
@@ -0,0 +1,54 @@
|
|
1
|
+
module ResqueDelayable
|
2
|
+
require "resque"
|
3
|
+
|
4
|
+
begin
|
5
|
+
require "resque/scheduler"
|
6
|
+
rescue LoadError
|
7
|
+
end
|
8
|
+
|
9
|
+
class DelayedMethod
|
10
|
+
attr_accessor :klass
|
11
|
+
attr_accessor :instance
|
12
|
+
attr_accessor :options
|
13
|
+
|
14
|
+
def initialize(klass, instance = nil, options = {})
|
15
|
+
unless instance.nil? || ResqueDelayable::ACTIVE_SERIALIZERS.any? {|klass| klass.serialize_match(instance) }
|
16
|
+
raise "Cannot serialize instances of this type (#{instance.class}). Consider creating a serializer."
|
17
|
+
end
|
18
|
+
|
19
|
+
self.klass = klass
|
20
|
+
self.instance = instance
|
21
|
+
self.options = options
|
22
|
+
end
|
23
|
+
|
24
|
+
def dequeue(method, *parameters)
|
25
|
+
::Resque.dequeue(self.klass, method, ::ResqueDelayable::serialize_object(self.instance), *::ResqueDelayable::serialize_object(parameters))
|
26
|
+
end
|
27
|
+
|
28
|
+
def method_missing(method, *parameters)
|
29
|
+
if instance.nil?
|
30
|
+
raise NoMethodError.new("undefined method '#{method}' for #{self.klass}", method, parameters) unless klass.respond_to?(method)
|
31
|
+
else
|
32
|
+
raise NoMethodError.new("undefined method '#{method}' for #{self.instance}", method, parameters) unless instance.respond_to?(method)
|
33
|
+
end
|
34
|
+
|
35
|
+
if self.options[:run_at]
|
36
|
+
if ::Resque.respond_to?(:enqueue_at)
|
37
|
+
::Resque.enqueue_at(self.options[:run_at], self.klass, method, ::ResqueDelayable::serialize_object(self.instance), *::ResqueDelayable::serialize_object(parameters))
|
38
|
+
else
|
39
|
+
raise "Cannot use run_at, resque-scheduler not installed"
|
40
|
+
end
|
41
|
+
elsif self.options[:run_in]
|
42
|
+
if ::Resque.respond_to?(:enqueue_in)
|
43
|
+
::Resque.enqueue_in(self.options[:run_in], self.klass, method, ::ResqueDelayable::serialize_object(self.instance), *::ResqueDelayable::serialize_object(parameters))
|
44
|
+
else
|
45
|
+
raise "Cannot use run_in, resque-scheduler not installed"
|
46
|
+
end
|
47
|
+
else
|
48
|
+
::Resque.enqueue(self.klass, method, ::ResqueDelayable::serialize_object(self.instance), *::ResqueDelayable::serialize_object(parameters))
|
49
|
+
end
|
50
|
+
|
51
|
+
true
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
module ResqueDelayable
|
2
|
+
module Serializer
|
3
|
+
class ActiveRecordSerializer
|
4
|
+
PREFIX = "ActiveRecord"
|
5
|
+
MATCHER = Regexp.new("^#{PREFIX}_(\w+)_(\d+)$")
|
6
|
+
|
7
|
+
class << self
|
8
|
+
def serialize_match(object)
|
9
|
+
object.is_a?(ActiveRecord::Base)
|
10
|
+
end
|
11
|
+
|
12
|
+
def deserialize_match(object)
|
13
|
+
object.class == String && MATCHER.match(object)
|
14
|
+
end
|
15
|
+
|
16
|
+
def serialize(object)
|
17
|
+
"#{PREFIX}_#{object.class}_#{object.id}"
|
18
|
+
end
|
19
|
+
|
20
|
+
def deserialize(object)
|
21
|
+
match = MATCHER.match(object)
|
22
|
+
match[1].constantize.find_by_id(match[2])
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
ResqueDelayable::ACTIVE_SERIALIZERS << ResqueDelayable::Serializer::ActiveRecordSerializer
|
30
|
+
ActiveRecord::Base.send :include, ResqueDelayable
|
@@ -0,0 +1,34 @@
|
|
1
|
+
module ResqueDelayable
|
2
|
+
module Serializer
|
3
|
+
class MongoMapperSerializer
|
4
|
+
PREFIX = "MongoMapper"
|
5
|
+
MATCHER = Regexp.new("^#{PREFIX}_([^_]+)_([^_]+)_([^_]+)$")
|
6
|
+
|
7
|
+
class << self
|
8
|
+
def serialize_match(object)
|
9
|
+
object.is_a?(MongoMapper::Document)
|
10
|
+
end
|
11
|
+
|
12
|
+
def deserialize_match(object)
|
13
|
+
object.class == String && MATCHER.match(object)
|
14
|
+
end
|
15
|
+
|
16
|
+
def serialize(object)
|
17
|
+
"#{PREFIX}_#{object.class}_#{object.id.class}_#{object.id}"
|
18
|
+
end
|
19
|
+
|
20
|
+
def deserialize(object)
|
21
|
+
match = MATCHER.match(object)
|
22
|
+
|
23
|
+
id = match[3]
|
24
|
+
id = id.to_i if match[2] == "Fixnum"
|
25
|
+
|
26
|
+
match[1].constantize.find_by_id(id)
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
ResqueDelayable::ACTIVE_SERIALIZERS << ResqueDelayable::Serializer::MongoMapperSerializer
|
34
|
+
MongoMapper::Document.send :include, ResqueDelayable
|
@@ -0,0 +1,30 @@
|
|
1
|
+
module ResqueDelayable
|
2
|
+
module Serializer
|
3
|
+
class MongoidSerializer
|
4
|
+
PREFIX = "Mongoid"
|
5
|
+
MATCHER = Regexp.new("^#{PREFIX}_([^_]+)_([^_]+)$")
|
6
|
+
|
7
|
+
class << self
|
8
|
+
def serialize_match(object)
|
9
|
+
object.is_a?(Mongoid::Document)
|
10
|
+
end
|
11
|
+
|
12
|
+
def deserialize_match(object)
|
13
|
+
object.class == String && MATCHER.match(object)
|
14
|
+
end
|
15
|
+
|
16
|
+
def serialize(object)
|
17
|
+
"#{PREFIX}_#{object.class}_#{object.id}"
|
18
|
+
end
|
19
|
+
|
20
|
+
def deserialize(object)
|
21
|
+
match = MATCHER.match(object)
|
22
|
+
match[1].constantize.find_by_id(id)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
ResqueDelayable::ACTIVE_SERIALIZERS << ResqueDelayable::Serializer::MongoidSerializer
|
30
|
+
Mongoid::Document.send :include, ResqueDelayable
|
@@ -0,0 +1,76 @@
|
|
1
|
+
module ResqueDelayable
|
2
|
+
require "set"
|
3
|
+
|
4
|
+
ACTIVE_SERIALIZERS = Set.new
|
5
|
+
|
6
|
+
require "resque-delayable/delayed_method"
|
7
|
+
|
8
|
+
def self.included(base)
|
9
|
+
base.extend(ClassMethods)
|
10
|
+
super
|
11
|
+
end
|
12
|
+
|
13
|
+
def self.serialize_object(object)
|
14
|
+
if [Hash, HashWithIndifferentAccess].include?(object.class)
|
15
|
+
object.inject({}) {|doc, (k,v)| doc[k] = serialize_object(v); doc}
|
16
|
+
elsif [Set, Array].include?(object.class)
|
17
|
+
object.map {|v| serialize_object(v)}
|
18
|
+
else
|
19
|
+
ACTIVE_SERIALIZERS.each do |serializer|
|
20
|
+
return serializer.serialize(object) if serializer.serialize_match(object)
|
21
|
+
end
|
22
|
+
|
23
|
+
object
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
def self.deserialize_object(object)
|
28
|
+
if [Hash, HashWithIndifferentAccess].include?(object.class)
|
29
|
+
object.inject({}) {|doc, (k,v)| doc[k] = deserialize_object(v); doc}
|
30
|
+
elsif [Set, Array].include?(object.class)
|
31
|
+
object.map {|v| deserialize_object(v)}
|
32
|
+
else
|
33
|
+
ACTIVE_SERIALIZERS.each do |serializer|
|
34
|
+
return serializer.deserialize(object) if serializer.deserialize_match(object)
|
35
|
+
end
|
36
|
+
|
37
|
+
object
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
module ClassMethods
|
42
|
+
def rdelay(options = {})
|
43
|
+
return DelayedMethod.new(self, nil, options)
|
44
|
+
end
|
45
|
+
|
46
|
+
def perform(cmd, instance, *args)
|
47
|
+
deserialized_args = ::ResqueDelayable.deserialize_object(args)
|
48
|
+
|
49
|
+
if instance
|
50
|
+
# Instance method
|
51
|
+
deserialized_instance = ::ResqueDelayable.deserialize_object(instance)
|
52
|
+
if deserialized_instance
|
53
|
+
deserialized_instance.send(cmd, *deserialized_args)
|
54
|
+
else
|
55
|
+
puts "ResqueDelayable couldn't find instance '#{instance}' to peform method '#{cmd}' on"
|
56
|
+
end
|
57
|
+
else
|
58
|
+
# Class method
|
59
|
+
send(cmd, *deserialized_args)
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
def queue
|
64
|
+
@queue ||= self.name
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
def rdelay(options = {})
|
69
|
+
return DelayedMethod.new(self.class, self, options)
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
# Serialization plugins
|
74
|
+
require "resque-delayable/serializer/active_record_serializer" if defined? ActiveRecord::Base
|
75
|
+
require "resque-delayable/serializer/mongo_mapper_serializer" if defined? MongoMapper::Document
|
76
|
+
require "resque-delayable/serializer/mongoid_serializer" if defined? Mongoid::Document
|
metadata
ADDED
@@ -0,0 +1,132 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: resque-delayable
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 23
|
5
|
+
prerelease:
|
6
|
+
segments:
|
7
|
+
- 1
|
8
|
+
- 0
|
9
|
+
- 0
|
10
|
+
version: 1.0.0
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- James Smith
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2011-12-14 00:00:00 Z
|
19
|
+
dependencies:
|
20
|
+
- !ruby/object:Gem::Dependency
|
21
|
+
prerelease: false
|
22
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
23
|
+
none: false
|
24
|
+
requirements:
|
25
|
+
- - ">="
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
hash: 3
|
28
|
+
segments:
|
29
|
+
- 0
|
30
|
+
version: "0"
|
31
|
+
version_requirements: *id001
|
32
|
+
name: shoulda
|
33
|
+
type: :development
|
34
|
+
- !ruby/object:Gem::Dependency
|
35
|
+
prerelease: false
|
36
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
37
|
+
none: false
|
38
|
+
requirements:
|
39
|
+
- - ~>
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
hash: 23
|
42
|
+
segments:
|
43
|
+
- 1
|
44
|
+
- 0
|
45
|
+
- 0
|
46
|
+
version: 1.0.0
|
47
|
+
version_requirements: *id002
|
48
|
+
name: bundler
|
49
|
+
type: :development
|
50
|
+
- !ruby/object:Gem::Dependency
|
51
|
+
prerelease: false
|
52
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
53
|
+
none: false
|
54
|
+
requirements:
|
55
|
+
- - ~>
|
56
|
+
- !ruby/object:Gem::Version
|
57
|
+
hash: 7
|
58
|
+
segments:
|
59
|
+
- 1
|
60
|
+
- 6
|
61
|
+
- 4
|
62
|
+
version: 1.6.4
|
63
|
+
version_requirements: *id003
|
64
|
+
name: jeweler
|
65
|
+
type: :development
|
66
|
+
- !ruby/object:Gem::Dependency
|
67
|
+
prerelease: false
|
68
|
+
requirement: &id004 !ruby/object:Gem::Requirement
|
69
|
+
none: false
|
70
|
+
requirements:
|
71
|
+
- - ">="
|
72
|
+
- !ruby/object:Gem::Version
|
73
|
+
hash: 3
|
74
|
+
segments:
|
75
|
+
- 0
|
76
|
+
version: "0"
|
77
|
+
version_requirements: *id004
|
78
|
+
name: rcov
|
79
|
+
type: :development
|
80
|
+
description: Run class methods or methods on activerecord, mongomapper objects later through resque
|
81
|
+
email: james@loopj.com
|
82
|
+
executables: []
|
83
|
+
|
84
|
+
extensions: []
|
85
|
+
|
86
|
+
extra_rdoc_files:
|
87
|
+
- LICENSE.txt
|
88
|
+
- README.md
|
89
|
+
files:
|
90
|
+
- lib/resque-delayable.rb
|
91
|
+
- lib/resque-delayable/delayed_method.rb
|
92
|
+
- lib/resque-delayable/serializer/active_record_serializer.rb
|
93
|
+
- lib/resque-delayable/serializer/mongo_mapper_serializer.rb
|
94
|
+
- lib/resque-delayable/serializer/mongoid_serializer.rb
|
95
|
+
- lib/resque-delayable/version.rb
|
96
|
+
- LICENSE.txt
|
97
|
+
- README.md
|
98
|
+
homepage: http://github.com/loopj/resque-delayable
|
99
|
+
licenses:
|
100
|
+
- MIT
|
101
|
+
post_install_message:
|
102
|
+
rdoc_options: []
|
103
|
+
|
104
|
+
require_paths:
|
105
|
+
- lib
|
106
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
107
|
+
none: false
|
108
|
+
requirements:
|
109
|
+
- - ">="
|
110
|
+
- !ruby/object:Gem::Version
|
111
|
+
hash: 3
|
112
|
+
segments:
|
113
|
+
- 0
|
114
|
+
version: "0"
|
115
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
116
|
+
none: false
|
117
|
+
requirements:
|
118
|
+
- - ">="
|
119
|
+
- !ruby/object:Gem::Version
|
120
|
+
hash: 3
|
121
|
+
segments:
|
122
|
+
- 0
|
123
|
+
version: "0"
|
124
|
+
requirements: []
|
125
|
+
|
126
|
+
rubyforge_project:
|
127
|
+
rubygems_version: 1.8.10
|
128
|
+
signing_key:
|
129
|
+
specification_version: 3
|
130
|
+
summary: Run class methods or methods on activerecord, mongomapper objects later through resque
|
131
|
+
test_files: []
|
132
|
+
|