delayed-method 0.2.4 → 0.3.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.
- checksums.yaml +4 -4
- data/README.md +36 -15
- data/lib/delayed-method.rb +3 -3
- data/spec/delayed_method_inheritance_spec.rb +78 -0
- metadata +4 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6e36ff8184489e5c0415e3ee4ce57985c10470fe
|
4
|
+
data.tar.gz: e210411cac14668a7aa3e5bd3ccd30f7d358da5b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: da3ce8966c10d57988f8fddf82851ca665885375d3dd7b96ed45252f46a93b9e5e7fc3ae91498f020bfa2cd68a3fe8687acb2674d461e088b07c591fd77258f2
|
7
|
+
data.tar.gz: 7d7fea6d7148b4f586aa58908391d14a71d9edff03bdf912322a7d0db6f0b0d99c77c423dfc869d23ea2322685e431b792971b30498d64400dd3b90771627c31
|
data/README.md
CHANGED
@@ -3,13 +3,13 @@ delayed-method
|
|
3
3
|
|
4
4
|
Requires the resque gem.
|
5
5
|
|
6
|
-
Allows you to move a long
|
6
|
+
Allows you to move a long running method to background to be processed later by resque
|
7
7
|
|
8
|
-
I have to come up with this because all other resque based delayed execution
|
8
|
+
I have to come up with this because all other resque based delayed execution gems out there that I have tried are complex, buggy and don't work.
|
9
9
|
|
10
|
-
|
10
|
+
delayed-method is designed to be dead simple, and doesn't hack deep into resque itself (so it is more likely to continue to work, even with further internal change of resque)
|
11
11
|
|
12
|
-
|
12
|
+
In fact, it just relies on Resque.enqueue at the deepest level. It doesn't deal with serialization, so it won't have any problem with serialization
|
13
13
|
|
14
14
|
Installation
|
15
15
|
============
|
@@ -22,37 +22,58 @@ Usage
|
|
22
22
|
|
23
23
|
Given you have this long call inside your web request:
|
24
24
|
|
25
|
-
Executor.long_call(arg1, arg2)
|
25
|
+
Executor.long_call(arg1, arg2)
|
26
26
|
|
27
27
|
then you can easily move it to the delayed queue in background with this:
|
28
28
|
|
29
|
-
DelayedMethod.enqueue(Executor, :long_call, arg1, arg2)
|
29
|
+
DelayedMethod.enqueue(Executor, :long_call, arg1, arg2)
|
30
30
|
|
31
31
|
Or if you use active record model:
|
32
32
|
|
33
|
-
model.long_call(arg1, arg2)
|
33
|
+
model.long_call(arg1, arg2)
|
34
34
|
|
35
35
|
then you can easily move it to the delayed queue in background with this:
|
36
36
|
|
37
|
-
DelayedMethod.enqueue(model, :long_call, arg1, arg2)
|
37
|
+
DelayedMethod.enqueue(model, :long_call, arg1, arg2)
|
38
38
|
|
39
|
-
That's it. This is the only
|
40
|
-
|
39
|
+
That's it. This is the only two cases where DelayedMethod will work: Caller
|
40
|
+
needs to be either a class or a persisted ActiveRecord model
|
41
41
|
|
42
42
|
If you use resque-scheduler, you can also do this:
|
43
43
|
|
44
|
-
|
44
|
+
DelayedMethod.enqueue_at(1.day.from_now, Executor, :long_call, arg1, arg2)
|
45
45
|
|
46
46
|
or
|
47
47
|
|
48
|
-
|
48
|
+
DelayedMethod.enqueue_at(1.day.from_now, model, :long_call, arg1, arg2)
|
49
|
+
|
50
|
+
Customize your worker
|
51
|
+
=====================
|
52
|
+
|
53
|
+
If you want to customize DelayedMethod to enforce things like having
|
54
|
+
ActiveRecord reading from master instead of slave, you can do this:
|
55
|
+
|
56
|
+
```
|
57
|
+
class MyCustomDelayedMethod < DelayedMethod
|
58
|
+
@queue = :delayed
|
59
|
+
|
60
|
+
def perform(klass_name, instance_id, method, *args)
|
61
|
+
wrap_method do
|
62
|
+
super
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
MyCustomDelayedMethod.enqueue(object, :method)
|
68
|
+
```
|
69
|
+
|
49
70
|
|
50
71
|
Warning
|
51
72
|
===========
|
52
73
|
|
53
|
-
Please notice that Class method call and ActiveRecord instance call are the only
|
54
|
-
Even in
|
55
|
-
Using object, structure, or symbol as argument will yield unexpected
|
74
|
+
Please notice that Class method call and ActiveRecord instance call are the only two cases supported.
|
75
|
+
Even in those cases, only simple arguments (string, number) are supported.
|
76
|
+
Using object, structure, or symbol as argument will yield unexpected results. (Symbol will be converted to String).
|
56
77
|
|
57
78
|
Author
|
58
79
|
=====
|
data/lib/delayed-method.rb
CHANGED
@@ -16,13 +16,13 @@ class DelayedMethod
|
|
16
16
|
|
17
17
|
def enqueue(object, method, *args)
|
18
18
|
ensure_proper_call(object, method) do |klass, id|
|
19
|
-
Resque.enqueue(
|
19
|
+
Resque.enqueue(self, klass, id, method, *args)
|
20
20
|
end
|
21
21
|
end
|
22
22
|
|
23
23
|
def enqueue_to(queue, object, method, *args)
|
24
24
|
ensure_proper_call(object, method) do |klass, id|
|
25
|
-
Resque.enqueue_to(queue,
|
25
|
+
Resque.enqueue_to(queue, self, klass, id, method, *args)
|
26
26
|
end
|
27
27
|
end
|
28
28
|
|
@@ -30,7 +30,7 @@ class DelayedMethod
|
|
30
30
|
def enqueue_at(time, object, method, *args)
|
31
31
|
ensure_proper_call(object, method) do |klass, id|
|
32
32
|
if Resque.respond_to?(:enqueue_at)
|
33
|
-
Resque.enqueue_at(time,
|
33
|
+
Resque.enqueue_at(time, self, klass, id, method, *args)
|
34
34
|
else
|
35
35
|
raise "resque-scheduler need to be included for this to work"
|
36
36
|
end
|
@@ -0,0 +1,78 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/spec_helper'
|
2
|
+
require 'active_record'
|
3
|
+
|
4
|
+
describe "DelayedMethod inheritance" do
|
5
|
+
describe "enqueue and perform" do
|
6
|
+
class CustomDelayedMethod < DelayedMethod
|
7
|
+
@queue = :delayed
|
8
|
+
|
9
|
+
def self.perform(*args)
|
10
|
+
before_hook
|
11
|
+
super
|
12
|
+
after_hook
|
13
|
+
end
|
14
|
+
|
15
|
+
def self.before_hook
|
16
|
+
end
|
17
|
+
|
18
|
+
def self.after_hook
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
class TestClass2
|
23
|
+
def self.execute(name, value)
|
24
|
+
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
context "for class object" do
|
29
|
+
it "queues a job to cause execution to happen" do
|
30
|
+
TestClass2.should_receive(:execute).with("sample", "another_sample")
|
31
|
+
CustomDelayedMethod.enqueue(TestClass2, :execute, "sample", "another_sample")
|
32
|
+
klass, args = Resque.reserve(:delayed)
|
33
|
+
klass.perform(*args)
|
34
|
+
end
|
35
|
+
|
36
|
+
it "queues CustomDelayedMethod as worker" do
|
37
|
+
CustomDelayedMethod.enqueue(TestClass2, :execute, "sample", "another_sample")
|
38
|
+
klass, args = Resque.reserve(:delayed)
|
39
|
+
klass.payload_class.should eq(CustomDelayedMethod)
|
40
|
+
end
|
41
|
+
|
42
|
+
it "executes correctly with extra hooks" do
|
43
|
+
CustomDelayedMethod.should_receive(:before_hook)
|
44
|
+
CustomDelayedMethod.should_receive(:after_hook)
|
45
|
+
CustomDelayedMethod.enqueue(TestClass2, :execute, "sample", "another_sample")
|
46
|
+
klass, args = Resque.reserve(:delayed)
|
47
|
+
klass.perform(*args)
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
context "for active record instance" do
|
52
|
+
it "queues a job to cause execution to happen" do
|
53
|
+
armodel = mock_armodel
|
54
|
+
armodel.should_receive(:execute).with("sample", "another_sample")
|
55
|
+
CustomDelayedMethod.enqueue(armodel, :execute, "sample", "another_sample")
|
56
|
+
klass, args = Resque.reserve(:delayed)
|
57
|
+
klass.perform(*args)
|
58
|
+
end
|
59
|
+
|
60
|
+
it "queues CustomDelayedMethod as worker" do
|
61
|
+
armodel = mock_armodel
|
62
|
+
CustomDelayedMethod.enqueue(armodel, :execute, "sample", "another_sample")
|
63
|
+
klass, args = Resque.reserve(:delayed)
|
64
|
+
klass.payload_class.should eq(CustomDelayedMethod)
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
def mock_armodel
|
71
|
+
mock(ActiveRecord::Base, { :id => 1, :execute => true }).tap do |armodel|
|
72
|
+
armodel.should_receive(:is_a?).with(Class).and_return(false)
|
73
|
+
armodel.should_receive(:is_a?).with(ActiveRecord::Base).and_return(true)
|
74
|
+
armodel.should_receive(:class).and_return ActiveRecord::Base
|
75
|
+
armodel.stub(:persisted?).and_return true
|
76
|
+
ActiveRecord::Base.stub(:find).with(1).and_return armodel
|
77
|
+
end
|
78
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: delayed-method
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Phuong Nguyen
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2019-11-16 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: resque
|
@@ -91,6 +91,7 @@ files:
|
|
91
91
|
- README.md
|
92
92
|
- Rakefile
|
93
93
|
- lib/delayed-method.rb
|
94
|
+
- spec/delayed_method_inheritance_spec.rb
|
94
95
|
- spec/delayed_method_spec.rb
|
95
96
|
- spec/spec_helper.rb
|
96
97
|
homepage: http://github.com/phuongnd08/delayed-method
|
@@ -112,7 +113,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
112
113
|
version: '0'
|
113
114
|
requirements: []
|
114
115
|
rubyforge_project:
|
115
|
-
rubygems_version: 2.
|
116
|
+
rubygems_version: 2.6.14.3
|
116
117
|
signing_key:
|
117
118
|
specification_version: 4
|
118
119
|
summary: Allow you to quickly move a long call to background which then executed by
|