sidekiq-activerecord 0.0.4 → 0.0.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +1 -1
- data/lib/sidekiq/active_record/task_worker.rb +24 -11
- data/lib/sidekiq/active_record/version.rb +1 -1
- data/spec/examples/alias_user_task_worker.rb +27 -0
- data/spec/examples/helper_classes.rb +11 -0
- data/spec/examples/no_alias_user_task_worker.rb +27 -0
- data/spec/sidekiq/active_record/example/alias_user_task_worker_spec.rb +86 -0
- data/spec/sidekiq/active_record/example/no_alias_user_task_worker_spec.rb +86 -0
- data/spec/sidekiq/active_record/task_worker_spec.rb +1 -52
- data/spec/spec_helper.rb +3 -1
- data/spec/support/models.rb +4 -0
- metadata +12 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9139afdf7efd7b2b766b0f0c79cbffe98cf0a3df
|
4
|
+
data.tar.gz: 579edad53f837e6254d84205f0a0c58985f95fb0
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3dd406e37261203cb64935227bef8bc3448917a7aef5f1035f0a4baea91fe41fbb38b4483155a37eebc732c5504c44ba98b4a94add2b15b9fb068fce1917cbc9
|
7
|
+
data.tar.gz: f6ddc5772b85d0f80dbb9931fb1087dc8403480eb1bfd0b17df11002bed8ffd06006198cb54ef29fcabec8b93fe4a4bb8370669262df788751add0fa0c5442d3
|
data/README.md
CHANGED
@@ -57,13 +57,13 @@ module Sidekiq
|
|
57
57
|
# UserMailerTaskWorker.perform_async(user.id, :new_email)
|
58
58
|
#
|
59
59
|
def perform(identifier, *args)
|
60
|
-
@task_model = fetch_model
|
61
|
-
return not_found_model
|
60
|
+
@task_model = call_alias_method(:fetch_model, identifier, *args)
|
61
|
+
return call_alias_method(:not_found_model, identifier, *args) unless @task_model.present?
|
62
62
|
|
63
|
-
if should_perform_on_model?
|
64
|
-
perform_on_model
|
63
|
+
if call_alias_method(:should_perform_on_model?)
|
64
|
+
call_alias_method(:perform_on_model, *args)
|
65
65
|
else
|
66
|
-
did_not_perform_on_model
|
66
|
+
call_alias_method(:did_not_perform_on_model)
|
67
67
|
end
|
68
68
|
end
|
69
69
|
|
@@ -91,6 +91,15 @@ module Sidekiq
|
|
91
91
|
self.class.model_class.find_by(self.class.identifier_key => identifier)
|
92
92
|
end
|
93
93
|
|
94
|
+
# Try calling the alias method, and fallback to default name if not defined
|
95
|
+
def call_alias_method(method_name, *args)
|
96
|
+
alias_name = self.class.method_aliases_mapping[method_name]
|
97
|
+
if respond_to?(alias_name.to_sym)
|
98
|
+
send(alias_name, *args)
|
99
|
+
else
|
100
|
+
send(method_name, *args)
|
101
|
+
end
|
102
|
+
end
|
94
103
|
|
95
104
|
class << self
|
96
105
|
|
@@ -121,6 +130,9 @@ module Sidekiq
|
|
121
130
|
@sidekiq_task_options_hash = get_sidekiq_task_options.merge((opts).symbolize_keys!)
|
122
131
|
end
|
123
132
|
|
133
|
+
def method_aliases_mapping
|
134
|
+
@_method_aliases_mapping
|
135
|
+
end
|
124
136
|
|
125
137
|
private
|
126
138
|
|
@@ -145,18 +157,19 @@ module Sidekiq
|
|
145
157
|
if model_klass_name.is_a?(Class)
|
146
158
|
model_klass_name = model_klass_name.name.underscore
|
147
159
|
end
|
148
|
-
|
160
|
+
setup_method_aliases_mapping(model_klass_name)
|
161
|
+
alias_method model_klass_name.to_sym, :task_model
|
162
|
+
end
|
163
|
+
|
164
|
+
def setup_method_aliases_mapping(model_klass_name)
|
165
|
+
@_method_aliases_mapping = {
|
149
166
|
:task_model => model_klass_name,
|
150
167
|
:fetch_model => "fetch_#{model_klass_name}",
|
151
168
|
:not_found_model => "not_found_#{model_klass_name}",
|
152
169
|
:should_perform_on_model? => "should_perform_on_#{model_klass_name}?",
|
153
170
|
:did_not_perform_on_model => "did_not_perform_on_#{model_klass_name}",
|
154
171
|
:perform_on_model => "perform_on_#{model_klass_name}"
|
155
|
-
}
|
156
|
-
self.class_exec do
|
157
|
-
alias_method new_name.to_sym, old_name
|
158
|
-
end
|
159
|
-
end
|
172
|
+
}
|
160
173
|
end
|
161
174
|
|
162
175
|
def get_sidekiq_task_options
|
@@ -0,0 +1,27 @@
|
|
1
|
+
module Examples
|
2
|
+
class AliasUserTaskWorker < Sidekiq::ActiveRecord::TaskWorker
|
3
|
+
|
4
|
+
sidekiq_task_model :user # or User class
|
5
|
+
sidekiq_task_options :identifier_key => :email
|
6
|
+
|
7
|
+
def perform_on_user(new_email = nil)
|
8
|
+
UserMailer.update_email(user, new_email)
|
9
|
+
end
|
10
|
+
|
11
|
+
# optional
|
12
|
+
def not_found_user(email)
|
13
|
+
Log.error "User not found for email:#{email}"
|
14
|
+
end
|
15
|
+
|
16
|
+
# optional
|
17
|
+
def should_perform_on_user?
|
18
|
+
user.active?
|
19
|
+
end
|
20
|
+
|
21
|
+
# optional
|
22
|
+
def did_not_perform_on_user
|
23
|
+
Log.error "User #{user.id} is invalid"
|
24
|
+
end
|
25
|
+
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
module Examples
|
2
|
+
class NoAliasUserTaskWorker < Sidekiq::ActiveRecord::TaskWorker
|
3
|
+
|
4
|
+
sidekiq_task_model :user # or User class
|
5
|
+
sidekiq_task_options :identifier_key => :email
|
6
|
+
|
7
|
+
def perform_on_model(new_email = nil)
|
8
|
+
UserMailer.update_email(task_model, new_email)
|
9
|
+
end
|
10
|
+
|
11
|
+
# optional
|
12
|
+
def not_found_model(email)
|
13
|
+
Log.error "User not found for email:#{email}"
|
14
|
+
end
|
15
|
+
|
16
|
+
# optional
|
17
|
+
def should_perform_on_model?
|
18
|
+
user.active?
|
19
|
+
end
|
20
|
+
|
21
|
+
# optional
|
22
|
+
def did_not_perform_on_model
|
23
|
+
Log.error "User #{task_model.id} is invalid"
|
24
|
+
end
|
25
|
+
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,86 @@
|
|
1
|
+
module Examples
|
2
|
+
describe AliasUserTaskWorker do
|
3
|
+
|
4
|
+
let(:logger) { Log }
|
5
|
+
let(:service_class) { UserMailer }
|
6
|
+
let(:other_param) { 'new@email.com' }
|
7
|
+
|
8
|
+
let(:task_worker_class) { AliasUserTaskWorker }
|
9
|
+
subject(:task_worker) { AliasUserTaskWorker.new }
|
10
|
+
|
11
|
+
def run_worker
|
12
|
+
task_worker.perform(user.email, other_param)
|
13
|
+
end
|
14
|
+
|
15
|
+
before do
|
16
|
+
allow(service_class).to receive(:update_email)
|
17
|
+
allow(logger).to receive(:error)
|
18
|
+
end
|
19
|
+
|
20
|
+
context 'when the user is not found' do
|
21
|
+
|
22
|
+
let(:unfound_email) { 'unfound@email.com' }
|
23
|
+
|
24
|
+
it 'calls not_found_user' do
|
25
|
+
expect(task_worker).to receive(:not_found_user).with(unfound_email).and_call_original
|
26
|
+
task_worker.perform(unfound_email)
|
27
|
+
end
|
28
|
+
|
29
|
+
it 'calls the logger with the not found email' do
|
30
|
+
task_worker.perform(unfound_email)
|
31
|
+
expect(logger).to have_received(:error).with("User not found for email:#{unfound_email}")
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
context 'when the user is found' do
|
36
|
+
|
37
|
+
let(:user) { create(:user) }
|
38
|
+
|
39
|
+
it 'calls should_perform_on_user?' do
|
40
|
+
expect(task_worker).to receive(:should_perform_on_user?).and_call_original
|
41
|
+
run_worker
|
42
|
+
end
|
43
|
+
|
44
|
+
it 'check if the user is active' do
|
45
|
+
allow(task_worker).to receive(:user).and_return(user)
|
46
|
+
expect(user).to receive(:active?).and_call_original
|
47
|
+
run_worker
|
48
|
+
end
|
49
|
+
|
50
|
+
context 'when the user is active' do
|
51
|
+
|
52
|
+
let(:user) { create(:user, :active) }
|
53
|
+
|
54
|
+
it 'calls perform_on_user' do
|
55
|
+
expect(task_worker).to receive(:perform_on_user).and_call_original
|
56
|
+
run_worker
|
57
|
+
end
|
58
|
+
|
59
|
+
it 'calls the service class with the user' do
|
60
|
+
run_worker
|
61
|
+
expect(service_class).to have_received(:update_email).with(user, other_param)
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
context 'when the user is inactive' do
|
66
|
+
|
67
|
+
let(:user) { create(:user, :banned) }
|
68
|
+
|
69
|
+
it 'ignores the user' do
|
70
|
+
run_worker
|
71
|
+
expect(service_class).to_not have_received(:update_email)
|
72
|
+
end
|
73
|
+
|
74
|
+
it 'calls did_not_perform_on_user' do
|
75
|
+
expect(task_worker).to receive(:did_not_perform_on_user).and_call_original
|
76
|
+
run_worker
|
77
|
+
end
|
78
|
+
|
79
|
+
it 'calls the logger with the inactive user' do
|
80
|
+
run_worker
|
81
|
+
expect(logger).to have_received(:error).with("User #{user.id} is invalid")
|
82
|
+
end
|
83
|
+
end
|
84
|
+
end
|
85
|
+
end
|
86
|
+
end
|
@@ -0,0 +1,86 @@
|
|
1
|
+
module Examples
|
2
|
+
describe NoAliasUserTaskWorker do
|
3
|
+
|
4
|
+
let(:logger) { Log }
|
5
|
+
let(:service_class) { UserMailer }
|
6
|
+
let(:other_param) { 'new@email.com' }
|
7
|
+
|
8
|
+
let(:task_worker_class) { NoAliasUserTaskWorker }
|
9
|
+
subject(:task_worker) { NoAliasUserTaskWorker.new }
|
10
|
+
|
11
|
+
def run_worker
|
12
|
+
task_worker.perform(user.email, other_param)
|
13
|
+
end
|
14
|
+
|
15
|
+
before do
|
16
|
+
allow(service_class).to receive(:update_email)
|
17
|
+
allow(logger).to receive(:error)
|
18
|
+
end
|
19
|
+
|
20
|
+
context 'when the user is not found' do
|
21
|
+
|
22
|
+
let(:unfound_email) { 'unfound@email.com' }
|
23
|
+
|
24
|
+
it 'calls not_found_model' do
|
25
|
+
expect(task_worker).to receive(:not_found_model).with(unfound_email).and_call_original
|
26
|
+
task_worker.perform(unfound_email)
|
27
|
+
end
|
28
|
+
|
29
|
+
it 'calls the logger with the not found email' do
|
30
|
+
task_worker.perform(unfound_email)
|
31
|
+
expect(logger).to have_received(:error).with("User not found for email:#{unfound_email}")
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
context 'when the user is found' do
|
36
|
+
|
37
|
+
let(:user) { create(:user) }
|
38
|
+
|
39
|
+
it 'calls should_perform_on_model?' do
|
40
|
+
expect(task_worker).to receive(:should_perform_on_model?).and_call_original
|
41
|
+
run_worker
|
42
|
+
end
|
43
|
+
|
44
|
+
it 'check if the user is active' do
|
45
|
+
allow(task_worker).to receive(:user).and_return(user)
|
46
|
+
expect(user).to receive(:active?).and_call_original
|
47
|
+
run_worker
|
48
|
+
end
|
49
|
+
|
50
|
+
context 'when the user is active' do
|
51
|
+
|
52
|
+
let(:user) { create(:user, :active) }
|
53
|
+
|
54
|
+
it 'calls perform_on_model' do
|
55
|
+
expect(task_worker).to receive(:perform_on_model).and_call_original
|
56
|
+
run_worker
|
57
|
+
end
|
58
|
+
|
59
|
+
it 'calls the service class with the user' do
|
60
|
+
run_worker
|
61
|
+
expect(service_class).to have_received(:update_email).with(user, other_param)
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
context 'when the user is inactive' do
|
66
|
+
|
67
|
+
let(:user) { create(:user, :banned) }
|
68
|
+
|
69
|
+
it 'ignores the user' do
|
70
|
+
run_worker
|
71
|
+
expect(service_class).to_not have_received(:update_email)
|
72
|
+
end
|
73
|
+
|
74
|
+
it 'calls did_not_perform_on_model' do
|
75
|
+
expect(task_worker).to receive(:did_not_perform_on_model).and_call_original
|
76
|
+
run_worker
|
77
|
+
end
|
78
|
+
|
79
|
+
it 'calls the logger with the inactive user' do
|
80
|
+
run_worker
|
81
|
+
expect(logger).to have_received(:error).with("User #{user.id} is invalid")
|
82
|
+
end
|
83
|
+
end
|
84
|
+
end
|
85
|
+
end
|
86
|
+
end
|
@@ -92,11 +92,6 @@ describe Sidekiq::ActiveRecord::TaskWorker do
|
|
92
92
|
run_worker
|
93
93
|
expect(task_worker.task_model).to eq user
|
94
94
|
end
|
95
|
-
|
96
|
-
it 'has an alias of `task_model` withe for the specified model name' do
|
97
|
-
run_worker
|
98
|
-
expect(task_worker.user).to eq user
|
99
|
-
end
|
100
95
|
end
|
101
96
|
|
102
97
|
end
|
@@ -118,11 +113,6 @@ describe Sidekiq::ActiveRecord::TaskWorker do
|
|
118
113
|
run_worker
|
119
114
|
expect(task_worker.task_model).to eq user
|
120
115
|
end
|
121
|
-
|
122
|
-
it 'has an alias of `task_model` withe for the specified model name' do
|
123
|
-
run_worker
|
124
|
-
expect(task_worker.user).to eq user
|
125
|
-
end
|
126
116
|
end
|
127
117
|
|
128
118
|
describe 'when fetching the model' do
|
@@ -155,13 +145,6 @@ describe Sidekiq::ActiveRecord::TaskWorker do
|
|
155
145
|
expect(task_worker).to_not receive(:perform_on_model)
|
156
146
|
run_worker
|
157
147
|
end
|
158
|
-
|
159
|
-
describe 'method alias' do
|
160
|
-
it 'has an alias of not_found_model hook with for the specified task model name' do
|
161
|
-
expect(task_worker.not_found_user(trash_id)).to eq trash_id
|
162
|
-
end
|
163
|
-
end
|
164
|
-
|
165
148
|
end
|
166
149
|
|
167
150
|
context 'when the mode is found' do
|
@@ -171,24 +154,6 @@ describe Sidekiq::ActiveRecord::TaskWorker do
|
|
171
154
|
expect(task_worker).to receive(:should_perform_on_model?)
|
172
155
|
run_worker
|
173
156
|
end
|
174
|
-
|
175
|
-
describe 'method alias' do
|
176
|
-
|
177
|
-
let(:mock_result) { 1234 }
|
178
|
-
|
179
|
-
before do
|
180
|
-
class UserTaskWorker
|
181
|
-
def should_perform_on_user?
|
182
|
-
1234
|
183
|
-
end
|
184
|
-
end
|
185
|
-
end
|
186
|
-
|
187
|
-
it 'has an alias of should_perform_on_model? hook with for the specified task model name' do
|
188
|
-
run_worker
|
189
|
-
expect(task_worker.should_perform_on_user?).to eq mock_result
|
190
|
-
end
|
191
|
-
end
|
192
157
|
end
|
193
158
|
|
194
159
|
context 'when the model should be performed' do
|
@@ -216,14 +181,6 @@ describe Sidekiq::ActiveRecord::TaskWorker do
|
|
216
181
|
task_worker.perform(user.id, user.email)
|
217
182
|
end
|
218
183
|
end
|
219
|
-
|
220
|
-
describe 'method alias' do
|
221
|
-
it 'has an alias of perform_on_model hook with for the specified task model name' do
|
222
|
-
task_worker.perform(user.id)
|
223
|
-
expect(task_worker.perform_on_user).to eq user
|
224
|
-
end
|
225
|
-
end
|
226
|
-
|
227
184
|
end
|
228
185
|
end
|
229
186
|
|
@@ -242,21 +199,13 @@ describe Sidekiq::ActiveRecord::TaskWorker do
|
|
242
199
|
expect(task_worker).to_not receive(:perform_on_model)
|
243
200
|
run_worker
|
244
201
|
end
|
245
|
-
|
246
|
-
describe 'method alias' do
|
247
|
-
it 'has an alias of did_not_perform_on_model hook with for the specified task model name' do
|
248
|
-
run_worker
|
249
|
-
expect(task_worker.did_not_perform_on_user).to eq user
|
250
|
-
end
|
251
|
-
end
|
252
|
-
|
253
202
|
end
|
254
203
|
|
255
204
|
end
|
256
205
|
end
|
257
206
|
end
|
258
207
|
|
259
|
-
describe 'perform_async_on'
|
208
|
+
describe 'perform_async_on' do
|
260
209
|
|
261
210
|
before do
|
262
211
|
class UserTaskWorker
|
data/spec/spec_helper.rb
CHANGED
@@ -9,7 +9,9 @@ require 'database_cleaner'
|
|
9
9
|
|
10
10
|
RSpec.configure do |config|
|
11
11
|
config.alias_example_to :expect_it
|
12
|
+
config.filter_run_including :focus => true
|
13
|
+
config.run_all_when_everything_filtered = true
|
12
14
|
end
|
13
15
|
|
14
16
|
Dir['./spec/support/**/*.rb'].sort.each { |f| require f }
|
15
|
-
|
17
|
+
Dir['./spec/examples/**/*.rb'].sort.each { |f| require f }
|
data/spec/support/models.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: sidekiq-activerecord
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Adam Farhi
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-06-
|
11
|
+
date: 2014-06-21 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: sidekiq
|
@@ -113,6 +113,11 @@ files:
|
|
113
113
|
- lib/sidekiq/active_record/version.rb
|
114
114
|
- lib/sidekiq/activerecord.rb
|
115
115
|
- sidekiq-activerecord.gemspec
|
116
|
+
- spec/examples/alias_user_task_worker.rb
|
117
|
+
- spec/examples/helper_classes.rb
|
118
|
+
- spec/examples/no_alias_user_task_worker.rb
|
119
|
+
- spec/sidekiq/active_record/example/alias_user_task_worker_spec.rb
|
120
|
+
- spec/sidekiq/active_record/example/no_alias_user_task_worker_spec.rb
|
116
121
|
- spec/sidekiq/active_record/manager_worker_spec.rb
|
117
122
|
- spec/sidekiq/active_record/task_worker_spec.rb
|
118
123
|
- spec/spec_helper.rb
|
@@ -145,6 +150,11 @@ signing_key:
|
|
145
150
|
specification_version: 4
|
146
151
|
summary: Encapsulates various interactions between Sidekiq and ActiveRecord
|
147
152
|
test_files:
|
153
|
+
- spec/examples/alias_user_task_worker.rb
|
154
|
+
- spec/examples/helper_classes.rb
|
155
|
+
- spec/examples/no_alias_user_task_worker.rb
|
156
|
+
- spec/sidekiq/active_record/example/alias_user_task_worker_spec.rb
|
157
|
+
- spec/sidekiq/active_record/example/no_alias_user_task_worker_spec.rb
|
148
158
|
- spec/sidekiq/active_record/manager_worker_spec.rb
|
149
159
|
- spec/sidekiq/active_record/task_worker_spec.rb
|
150
160
|
- spec/spec_helper.rb
|