tire_async_index 0.2.1 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Changelog.md +5 -0
- data/README.md +18 -3
- data/lib/tire/model/async_callbacks.rb +17 -20
- data/lib/tire_async_index.rb +10 -6
- data/lib/tire_async_index/version.rb +1 -1
- data/lib/tire_async_index/workers/resque.rb +15 -0
- data/lib/tire_async_index/workers/sidekiq.rb +18 -0
- data/lib/tire_async_index/workers/update_index.rb +7 -2
- data/spec/active_model_tire_async_index_spec.rb +5 -5
- data/spec/active_record_tire_async_index_spec.rb +35 -5
- data/spec/test_schema.rb +1 -1
- data/spec/update_index_spec.rb +25 -3
- metadata +4 -4
- data/lib/tire_async_index/workers/resque_update_index.rb +0 -11
- data/lib/tire_async_index/workers/sidekiq_update_index.rb +0 -8
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b862d7239b23af431da5f68fc6569e8e2ab79555
|
4
|
+
data.tar.gz: 3ac36b477df231387b642c84622041f5a17feff0
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 99db27b1eb4445aa70225b45fa0d34664775ad1d7dc2cebe3ad49101acce707b981b941b9819e812ef5ce9eea7dc2a8fb1f826d65b12cbf304a19a0f337aa32b
|
7
|
+
data.tar.gz: 70157720a282e54a349e8b9df1a304370ab2f861bea42d5c07d72de9f51779c46e3cd6cac1cc1ff966cf62f8f76fad35c3d33d39fe57515abd1a5366e09177cb
|
data/Changelog.md
CHANGED
data/README.md
CHANGED
@@ -8,10 +8,8 @@ Requirements: Ruby 1.9, 2.0, Rails => 3.0
|
|
8
8
|
|
9
9
|
## Installation
|
10
10
|
|
11
|
-
Add this line to your application's Gemfile
|
11
|
+
Add this line to your application's Gemfile:
|
12
12
|
|
13
|
-
gem 'tire'
|
14
|
-
gem 'sidekiq' #'resque'
|
15
13
|
gem 'tire_async_index'
|
16
14
|
|
17
15
|
And then execute:
|
@@ -44,6 +42,23 @@ Just add AsyncCallbacks to your model:
|
|
44
42
|
|
45
43
|
That's all.
|
46
44
|
|
45
|
+
## Custom identificator or finder
|
46
|
+
|
47
|
+
If you need more complex solution to define identificators and/or finder. You could simply add methods to override default:
|
48
|
+
|
49
|
+
class User < ActiveRecord::Base
|
50
|
+
include Tire::Model::Search
|
51
|
+
include Tire::Model::AsyncCallbacks
|
52
|
+
|
53
|
+
def self.tire_async_finder(id)
|
54
|
+
User.where(...).first
|
55
|
+
end
|
56
|
+
|
57
|
+
def async_tire_object_id
|
58
|
+
"#{id}-#{name}"
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
47
62
|
## TODO
|
48
63
|
|
49
64
|
* Add support for custom filter / custom finders
|
@@ -32,30 +32,27 @@ module Tire
|
|
32
32
|
end
|
33
33
|
|
34
34
|
def async_tire_callback(type)
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
else
|
45
|
-
case type
|
46
|
-
when :update
|
47
|
-
tire.update_index
|
48
|
-
when :delete
|
49
|
-
tire.index.remove self
|
50
|
-
end
|
35
|
+
if TireAsyncIndex.engine == :none
|
36
|
+
case type
|
37
|
+
when :update
|
38
|
+
tire.update_index
|
39
|
+
when :delete
|
40
|
+
tire.index.remove self
|
41
|
+
end
|
42
|
+
else
|
43
|
+
TireAsyncIndex.worker.run(type, self.class.name, get_async_tire_object_id)
|
51
44
|
end
|
52
45
|
end
|
53
46
|
|
54
|
-
def
|
55
|
-
if
|
56
|
-
self.
|
47
|
+
def get_async_tire_object_id
|
48
|
+
if self.respond_to?(:async_tire_object_id)
|
49
|
+
self.send(:async_tire_object_id)
|
57
50
|
else
|
58
|
-
self.id
|
51
|
+
if (method = ID_CONVERSION[self.id.class.name])
|
52
|
+
self.id.send(method)
|
53
|
+
else
|
54
|
+
self.id
|
55
|
+
end
|
59
56
|
end
|
60
57
|
end
|
61
58
|
|
data/lib/tire_async_index.rb
CHANGED
@@ -9,7 +9,6 @@ module TireAsyncIndex
|
|
9
9
|
def configure
|
10
10
|
self.configuration ||= Configuration.new
|
11
11
|
yield(configuration)
|
12
|
-
reconfig_workers
|
13
12
|
end
|
14
13
|
|
15
14
|
def queue
|
@@ -20,20 +19,25 @@ module TireAsyncIndex
|
|
20
19
|
self.configuration.engine
|
21
20
|
end
|
22
21
|
|
23
|
-
def
|
24
|
-
|
25
|
-
|
22
|
+
def worker
|
23
|
+
case configuration.engine
|
24
|
+
when :sidekiq
|
25
|
+
TireAsyncIndex::Workers::Sidekiq
|
26
|
+
when :resque
|
27
|
+
TireAsyncIndex::Workers::Resque
|
28
|
+
else
|
29
|
+
TireAsyncIndex::Workers::UpdateIndex
|
26
30
|
end
|
27
31
|
end
|
28
32
|
|
29
33
|
module Workers
|
30
34
|
autoload :UpdateIndex, 'tire_async_index/workers/update_index'
|
35
|
+
autoload :Sidekiq, 'tire_async_index/workers/sidekiq'
|
36
|
+
autoload :Resque, 'tire_async_index/workers/resque'
|
31
37
|
end
|
32
38
|
|
33
39
|
end
|
34
40
|
|
35
|
-
require 'tire_async_index/workers/sidekiq_update_index' if defined?(Sidekiq)
|
36
|
-
require 'tire_async_index/workers/resque_update_index' if defined?(Resque)
|
37
41
|
require 'tire/model/async_callbacks'
|
38
42
|
|
39
43
|
TireAsyncIndex.configure {}
|
@@ -0,0 +1,15 @@
|
|
1
|
+
module TireAsyncIndex
|
2
|
+
module Workers
|
3
|
+
class Resque < UpdateIndex
|
4
|
+
def self.queue; TireAsyncIndex.queue end
|
5
|
+
|
6
|
+
def self.run(action_type, class_name, id)
|
7
|
+
::Resque.enqueue TireAsyncIndex::Workers::Resque, action_type, class_name, id
|
8
|
+
end
|
9
|
+
|
10
|
+
def self.perform(action_type, class_name, id)
|
11
|
+
self.new.process(action_type, class_name, id)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
module TireAsyncIndex
|
2
|
+
module Workers
|
3
|
+
class Sidekiq < UpdateIndex
|
4
|
+
include ::Sidekiq::Worker
|
5
|
+
sidekiq_options queue: TireAsyncIndex.queue
|
6
|
+
|
7
|
+
def self.run(action_type, class_name, id)
|
8
|
+
TireAsyncIndex::Workers::Sidekiq
|
9
|
+
.perform_async(action_type, class_name, id)
|
10
|
+
end
|
11
|
+
|
12
|
+
def perform(action_type, class_name, id)
|
13
|
+
process(action_type, class_name, id)
|
14
|
+
end
|
15
|
+
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -11,12 +11,13 @@ module TireAsyncIndex
|
|
11
11
|
# class_name - Model class name
|
12
12
|
# id - Document id
|
13
13
|
#
|
14
|
-
|
14
|
+
|
15
|
+
def process(action_type, class_name, id)
|
15
16
|
klass = find_class_const(class_name)
|
16
17
|
|
17
18
|
case action_type.to_sym
|
18
19
|
when :update
|
19
|
-
object = klass.
|
20
|
+
object = klass.send(get_finder_method(klass), id)
|
20
21
|
|
21
22
|
if object.present? && object.respond_to?(:tire)
|
22
23
|
object.tire.update_index
|
@@ -38,6 +39,10 @@ module TireAsyncIndex
|
|
38
39
|
end
|
39
40
|
end
|
40
41
|
end
|
42
|
+
|
43
|
+
def get_finder_method(klass)
|
44
|
+
klass.respond_to?(:tire_async_finder) ? :tire_async_finder : :find
|
45
|
+
end
|
41
46
|
end
|
42
47
|
end
|
43
48
|
end
|
@@ -86,7 +86,7 @@ describe TireAsyncIndex do
|
|
86
86
|
config.background_engine :sidekiq
|
87
87
|
end
|
88
88
|
|
89
|
-
TireAsyncIndex::Workers::
|
89
|
+
TireAsyncIndex::Workers::Sidekiq.should_receive(:perform_async).with(:update, "AmUser", instance_of(Fixnum))
|
90
90
|
|
91
91
|
AmUser.new.tap { |a| a.id = 23 }.save
|
92
92
|
end
|
@@ -96,7 +96,7 @@ describe TireAsyncIndex do
|
|
96
96
|
config.background_engine :resque
|
97
97
|
end
|
98
98
|
|
99
|
-
Resque.should_receive(:enqueue).with(TireAsyncIndex::Workers::
|
99
|
+
Resque.should_receive(:enqueue).with(TireAsyncIndex::Workers::Resque, :update, "AmUser", instance_of(Fixnum))
|
100
100
|
|
101
101
|
AmUser.new.tap { |a| a.id = 23 }.save
|
102
102
|
end
|
@@ -122,7 +122,7 @@ describe TireAsyncIndex do
|
|
122
122
|
config.background_engine :sidekiq
|
123
123
|
end
|
124
124
|
|
125
|
-
TireAsyncIndex::Workers::
|
125
|
+
TireAsyncIndex::Workers::Sidekiq.should_receive(:perform_async).with(:delete, "AmUser", instance_of(Fixnum))
|
126
126
|
|
127
127
|
AmUser.new.tap { |a| a.id = 23 }.destroy
|
128
128
|
end
|
@@ -132,11 +132,11 @@ describe TireAsyncIndex do
|
|
132
132
|
config.background_engine :resque
|
133
133
|
end
|
134
134
|
|
135
|
-
Resque.should_receive(:enqueue).with(TireAsyncIndex::Workers::
|
135
|
+
Resque.should_receive(:enqueue).with(TireAsyncIndex::Workers::Resque, :delete, "AmUser", instance_of(Fixnum))
|
136
136
|
|
137
137
|
AmUser.new.tap { |a| a.id = 23 }.destroy
|
138
138
|
end
|
139
139
|
end
|
140
140
|
|
141
141
|
end
|
142
|
-
end
|
142
|
+
end
|
@@ -8,6 +8,14 @@ class ArUser < ActiveRecord::Base
|
|
8
8
|
include Tire::Model::AsyncCallbacks
|
9
9
|
end
|
10
10
|
|
11
|
+
class ArIdUser < ActiveRecord::Base
|
12
|
+
include Tire::Model::AsyncCallbacks
|
13
|
+
|
14
|
+
def async_tire_object_id
|
15
|
+
"text_id"
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
11
19
|
describe TireAsyncIndex do
|
12
20
|
|
13
21
|
before(:all) do
|
@@ -72,7 +80,7 @@ describe TireAsyncIndex do
|
|
72
80
|
config.background_engine :sidekiq
|
73
81
|
end
|
74
82
|
|
75
|
-
TireAsyncIndex::Workers::
|
83
|
+
TireAsyncIndex::Workers::Sidekiq.should_receive(:perform_async).with(:update, "ArUser", instance_of(Fixnum))
|
76
84
|
|
77
85
|
a = ArUser.new
|
78
86
|
a.save
|
@@ -83,11 +91,33 @@ describe TireAsyncIndex do
|
|
83
91
|
config.background_engine :resque
|
84
92
|
end
|
85
93
|
|
86
|
-
Resque.should_receive(:enqueue).with(TireAsyncIndex::Workers::
|
94
|
+
Resque.should_receive(:enqueue).with(TireAsyncIndex::Workers::Resque, :update, "ArUser", instance_of(Fixnum))
|
87
95
|
|
88
96
|
a = ArUser.new
|
89
97
|
a.save
|
90
98
|
end
|
99
|
+
|
100
|
+
it "should start sidekiq with custom id" do
|
101
|
+
TireAsyncIndex.configure do |config|
|
102
|
+
config.background_engine :sidekiq
|
103
|
+
end
|
104
|
+
|
105
|
+
TireAsyncIndex::Workers::Sidekiq.should_receive(:perform_async).with(:update, "ArIdUser", "text_id")
|
106
|
+
|
107
|
+
a = ArIdUser.new
|
108
|
+
a.save
|
109
|
+
end
|
110
|
+
|
111
|
+
it "should start resque with custom id" do
|
112
|
+
TireAsyncIndex.configure do |config|
|
113
|
+
config.background_engine :resque
|
114
|
+
end
|
115
|
+
|
116
|
+
Resque.should_receive(:enqueue).with(TireAsyncIndex::Workers::Resque, :update, "ArIdUser", "text_id")
|
117
|
+
|
118
|
+
a = ArIdUser.new
|
119
|
+
a.save
|
120
|
+
end
|
91
121
|
end
|
92
122
|
|
93
123
|
describe '#after_destroy' do
|
@@ -110,7 +140,7 @@ describe TireAsyncIndex do
|
|
110
140
|
config.background_engine :sidekiq
|
111
141
|
end
|
112
142
|
|
113
|
-
TireAsyncIndex::Workers::
|
143
|
+
TireAsyncIndex::Workers::Sidekiq.should_receive(:perform_async).with(:delete, "ArUser", instance_of(Fixnum))
|
114
144
|
|
115
145
|
ArUser.new.tap { |a| a.id = 23 }.destroy
|
116
146
|
end
|
@@ -120,11 +150,11 @@ describe TireAsyncIndex do
|
|
120
150
|
config.background_engine :resque
|
121
151
|
end
|
122
152
|
|
123
|
-
Resque.should_receive(:enqueue).with(TireAsyncIndex::Workers::
|
153
|
+
Resque.should_receive(:enqueue).with(TireAsyncIndex::Workers::Resque, :delete, "ArUser", instance_of(Fixnum))
|
124
154
|
|
125
155
|
ArUser.new.tap { |a| a.id = 23 }.destroy
|
126
156
|
end
|
127
157
|
end
|
128
158
|
|
129
159
|
end
|
130
|
-
end
|
160
|
+
end
|
data/spec/test_schema.rb
CHANGED
data/spec/update_index_spec.rb
CHANGED
@@ -11,16 +11,38 @@ module Level1
|
|
11
11
|
end
|
12
12
|
end
|
13
13
|
|
14
|
+
class ArUser < ActiveRecord::Base
|
15
|
+
include Tire::Model::AsyncCallbacks
|
16
|
+
end
|
17
|
+
|
18
|
+
class ArUserFinder < ActiveRecord::Base
|
19
|
+
include Tire::Model::AsyncCallbacks
|
20
|
+
|
21
|
+
def self.tire_async_finder(id)
|
22
|
+
nil
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
14
26
|
describe TireAsyncIndex::Workers::UpdateIndex do
|
15
27
|
|
16
|
-
describe '#
|
28
|
+
describe '#process' do
|
17
29
|
let(:instance) { described_class.new }
|
18
30
|
|
19
31
|
it 'resolves class constant' do
|
20
32
|
expect {
|
21
|
-
instance.
|
33
|
+
instance.process(:nothing, 'Level1::Level2::Model', 123)
|
22
34
|
}.not_to raise_error
|
23
35
|
end
|
36
|
+
|
37
|
+
it 'trigger find on simple ar model' do
|
38
|
+
ArUser.should_receive(:find).with(123).and_return(nil)
|
39
|
+
instance.process(:update, 'ArUser', 123)
|
40
|
+
end
|
41
|
+
|
42
|
+
it 'trigger finder on ar mode with custom finder' do
|
43
|
+
ArUserFinder.should_receive(:tire_async_finder).with(123).and_return(nil)
|
44
|
+
instance.process(:update, 'ArUserFinder', 123)
|
45
|
+
end
|
24
46
|
end
|
25
47
|
|
26
|
-
end
|
48
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: tire_async_index
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Sergey Efremov
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-07-
|
11
|
+
date: 2013-07-26 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: tire
|
@@ -142,8 +142,8 @@ files:
|
|
142
142
|
- lib/tire_async_index/configuration.rb
|
143
143
|
- lib/tire_async_index/exceptions.rb
|
144
144
|
- lib/tire_async_index/version.rb
|
145
|
-
- lib/tire_async_index/workers/
|
146
|
-
- lib/tire_async_index/workers/
|
145
|
+
- lib/tire_async_index/workers/resque.rb
|
146
|
+
- lib/tire_async_index/workers/sidekiq.rb
|
147
147
|
- lib/tire_async_index/workers/update_index.rb
|
148
148
|
- spec/active_model_tire_async_index_spec.rb
|
149
149
|
- spec/active_record_tire_async_index_spec.rb
|