delayed-method 0.1.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/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2012 Phuong Nguyen
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,56 @@
1
+ delayed-method
2
+ ---------------
3
+
4
+ Requires the resque gem.
5
+
6
+ Allows you to move a long run method to background to be processed later by resque
7
+
8
+ I have to come up with this because all other resque based delayed execution gem out there that I have tried is complex, buggy and doesn't work.
9
+
10
+ This delayed-method is designed to be deadly simple, doesn't hack deep into resque itself (so it is more likely to continue to work, even with further internal change of resque)
11
+
12
+ Infact, it just rely on Resque.enqueue at the deepest level. It doesn't deal with serialization, so it won't have any problem with serialization
13
+
14
+ Installation
15
+ ============
16
+
17
+ $ gem install delayed-method
18
+
19
+
20
+ Usage
21
+ ============
22
+
23
+ Given you have this long call inside your web request:
24
+
25
+ Executor.long_call(arg1, arg2)
26
+
27
+ then you can easily move it to the delayed queue in background with this:
28
+
29
+ DelayedMethod.enqueue(Executor, :long_call, arg1, arg2)
30
+
31
+ Or if you use active record model:
32
+
33
+ model.long_call(arg1, arg2)
34
+
35
+ then you can easily move it to the delayed queue in background with this:
36
+
37
+ DelayedMethod.enqueue(model, :long_call, arg1, arg2)
38
+
39
+
40
+ That's it. This is the only 2 cases that DelayedMethod will work.
41
+
42
+
43
+ Warning
44
+ ===========
45
+
46
+ Please notice that Class method call and ActiveRecord instance call are the only 2 cases supported.
47
+ Even in that case, only simple arguments (string, number) are supported.
48
+ Using object, structure, or symbol as argument will yield unexpected result. (Symbol will be converted to String).
49
+
50
+ delayed-method is only that simple and will always be that simple.
51
+ I don't want to promise a bundle of complex serialization or shit that will eventually turn into something sophisticated, easily fall apart when either resque or rails updated.
52
+
53
+ Author
54
+ =====
55
+
56
+ Phuong Nguyen:: phuongnd08@gmail.com
data/Rakefile ADDED
@@ -0,0 +1,7 @@
1
+ task :default => :spec
2
+ task :test => :spec
3
+
4
+ desc "Run specs"
5
+ task :spec do
6
+ exec "rspec spec/"
7
+ end
@@ -0,0 +1,29 @@
1
+ require 'resque'
2
+ require 'active_support'
3
+
4
+ class DelayedMethod
5
+ @queue = :delayed
6
+
7
+ class << self
8
+ include ActiveSupport::Inflector
9
+
10
+ def perform(klass_name, instance_id, method, *args)
11
+ if instance_id
12
+ constantize(klass_name).find(instance_id).send(method, *args)
13
+ else
14
+ constantize(klass_name).send(method, *args)
15
+ end
16
+ end
17
+
18
+ def enqueue(object, method, *args)
19
+ raise ArgumentError.new("object does not respond to #{method}") unless object.respond_to?(method)
20
+ if object.is_a? Class
21
+ Resque.enqueue(DelayedMethod, object.name, nil, method, *args)
22
+ elsif object.is_a? ActiveRecord::Base
23
+ Resque.enqueue(DelayedMethod, object.class.name, object.id, method, *args)
24
+ else
25
+ raise ArgumentError.new("Only class and ActiveRecord resource are supported")
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,49 @@
1
+ require File.dirname(__FILE__) + '/spec_helper'
2
+ require 'active_record'
3
+
4
+ describe DelayedMethod do
5
+ describe "enqueue and perform" do
6
+ context "object does not respond method" do
7
+ it "raises exception" do
8
+ expect {
9
+ DelayedMethod.enqueue(Object, :non_existent_method)
10
+ }.to raise_error
11
+ end
12
+ end
13
+
14
+ context "object responds to method" do
15
+ class TestClass
16
+ def self.execute(name, value)
17
+
18
+ end
19
+ end
20
+
21
+ context "for class object" do
22
+ it "queues a job to cause execution to happen" do
23
+ TestClass.should_receive(:execute).with("sample", "another_sample")
24
+ DelayedMethod.enqueue(TestClass, :execute, "sample", "another_sample")
25
+ klass, args = Resque.reserve(:delayed)
26
+ klass.perform(*args)
27
+ end
28
+ end
29
+
30
+ def mock_armodel
31
+ mock(ActiveRecord::Base, { :id => 1, :execute => true }).tap do |armodel|
32
+ armodel.should_receive(:is_a?).with(Class).and_return(false)
33
+ armodel.should_receive(:is_a?).with(ActiveRecord::Base).and_return(true)
34
+ armodel.should_receive(:class).and_return ActiveRecord::Base
35
+ ActiveRecord::Base.stub(:find).with(1).and_return armodel
36
+ end
37
+ end
38
+ context "for active record instance" do
39
+ it "queues a job to cause execution to happen" do
40
+ armodel = mock_armodel
41
+ armodel.should_receive(:execute).with("sample", "another_sample")
42
+ DelayedMethod.enqueue(armodel, :execute, "sample", "another_sample")
43
+ klass, args = Resque.reserve(:delayed)
44
+ klass.perform(*args)
45
+ end
46
+ end
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,5 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ require 'rspec'
4
+ require 'delayed-method'
5
+ require 'debugger'
metadata ADDED
@@ -0,0 +1,132 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: delayed-method
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Phuong Nguyen
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-07-14 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: resque
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: activesupport
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: rspec
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ~>
52
+ - !ruby/object:Gem::Version
53
+ version: '2.0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: '2.0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: debugger
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ - !ruby/object:Gem::Dependency
79
+ name: activerecord
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ type: :development
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ description: Allow you to quickly move a long call to background which then executed
95
+ by resque
96
+ email: phuongnd08@gmail
97
+ executables: []
98
+ extensions: []
99
+ extra_rdoc_files: []
100
+ files:
101
+ - README.md
102
+ - Rakefile
103
+ - LICENSE
104
+ - lib/delayed-method.rb
105
+ - spec/delayed_method_spec.rb
106
+ - spec/spec_helper.rb
107
+ homepage: http://github.com/phuongnd08/delayed-method
108
+ licenses: []
109
+ post_install_message:
110
+ rdoc_options: []
111
+ require_paths:
112
+ - lib
113
+ required_ruby_version: !ruby/object:Gem::Requirement
114
+ none: false
115
+ requirements:
116
+ - - ! '>='
117
+ - !ruby/object:Gem::Version
118
+ version: '0'
119
+ required_rubygems_version: !ruby/object:Gem::Requirement
120
+ none: false
121
+ requirements:
122
+ - - ! '>='
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ requirements: []
126
+ rubyforge_project:
127
+ rubygems_version: 1.8.24
128
+ signing_key:
129
+ specification_version: 3
130
+ summary: Allow you to quickly move a long call to background which then executed by
131
+ resque
132
+ test_files: []