chewy_kiqqer 0.2.1 → 0.2.2
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/.travis.yml +2 -0
- data/README.md +13 -0
- data/chewy_kiqqer.gemspec +1 -1
- data/lib/chewy_kiqqer/log_subscriber.rb +5 -1
- data/lib/chewy_kiqqer/version.rb +1 -1
- data/lib/chewy_kiqqer/worker.rb +18 -12
- data/spec/log_subscriber_spec.rb +8 -0
- data/spec/worker_spec.rb +0 -7
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 69fa520fad10a08f620c667e91f1b133ac7e8adf
|
4
|
+
data.tar.gz: 6d8ab912f3eb918570395081cc4e89720e286b7e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7459fef7cf824e5d833636876c4e09c6ad3c44ebb26e2c99e9bf7e96e775eff5845076f2ba55d4574cd75cb6b235190504e93b6547612fa204c1bc6bebbf56f1
|
7
|
+
data.tar.gz: fdf22831d0609f93fa8fc23b1d69ded783baf684af20507b708867784ae9003800db5352bc58a77e08cf7a1254a9dcc3ee17474cd914b8cc1c2b78cc1bcef0ae
|
data/.travis.yml
CHANGED
data/README.md
CHANGED
@@ -51,6 +51,19 @@ which will be indexed. The default is to use the current record.
|
|
51
51
|
|
52
52
|
## Update handling
|
53
53
|
|
54
|
+
The kiqqer does *not* use Chewy's `atomic` update, since that functionality is deeply linked with Chewy's syncronous update mechanism.
|
55
|
+
|
56
|
+
Instead, ChewyKiqqer will bind itself to the `#after_commit` callback, which means that it will only trigger a new job after a complete database transaction. This behaviour also ensures that only one job is enqueued per transaction.
|
57
|
+
|
58
|
+
However, if you have multiple database transactions, the kiqqer will still queue multiple jobs. The same is true when you enqueue jobs manually.
|
59
|
+
|
60
|
+
ChewyKiqqer uses locking via redis to ensure that all updates for one database record are run sequentially. This prevents race conditions which could lead to outdated data being written to the index otherwise.
|
61
|
+
|
62
|
+
## Logging
|
63
|
+
|
64
|
+
Logging is disabled by default, but you can set `ChewyKiqqer.logger` if you need log output (e.g. `ChewyKiqqer.logger = Rails.logger`). ChewyKiqqer uses ActiveSupport notifications, which you can also subscribe to.
|
65
|
+
See `log_subscriber.rb` for more info.
|
66
|
+
|
54
67
|
## Contributing
|
55
68
|
|
56
69
|
1. Fork it
|
data/chewy_kiqqer.gemspec
CHANGED
@@ -20,7 +20,7 @@ Gem::Specification.new do |spec|
|
|
20
20
|
|
21
21
|
spec.add_dependency 'sidekiq'
|
22
22
|
spec.add_dependency 'chewy'
|
23
|
-
spec.add_dependency '
|
23
|
+
spec.add_dependency 'mlanett-redis-lock'
|
24
24
|
spec.add_dependency 'activesupport', '>= 3.2.0'
|
25
25
|
|
26
26
|
spec.add_development_dependency "bundler", "~> 1.3"
|
@@ -3,7 +3,11 @@ module ChewyKiqqer
|
|
3
3
|
class LogSubscriber < ActiveSupport::LogSubscriber
|
4
4
|
|
5
5
|
def perform(event)
|
6
|
-
info "ChewyKiqqer
|
6
|
+
info "ChewyKiqqer job ran on #{event.payload[:index_name]} with ids #{event.payload[:ids]} - duration #{event.duration}"
|
7
|
+
end
|
8
|
+
|
9
|
+
def index(event)
|
10
|
+
debug "ChewyKiqqer index updated on #{event.payload[:index_name]} with ids #{event.payload[:ids]} - duration #{event.duration}"
|
7
11
|
end
|
8
12
|
|
9
13
|
def queue_jobs(event)
|
data/lib/chewy_kiqqer/version.rb
CHANGED
data/lib/chewy_kiqqer/worker.rb
CHANGED
@@ -1,27 +1,33 @@
|
|
1
1
|
require 'sidekiq'
|
2
|
-
require '
|
2
|
+
require 'redis-lock'
|
3
3
|
|
4
4
|
module ChewyKiqqer
|
5
5
|
class Worker
|
6
6
|
|
7
|
-
class LockError < StandardError ; end
|
8
|
-
|
9
7
|
include Sidekiq::Worker
|
10
|
-
include Sidekiq::Lock::Worker
|
11
|
-
|
12
|
-
sidekiq_options lock: {
|
13
|
-
timeout: 3000,
|
14
|
-
name: ->(index_name, ids) { "lock:chewy_kiqqer:#{index_name}-#{[ids].flatten.sort.join('-')}" }
|
15
|
-
}
|
16
8
|
|
17
9
|
|
18
10
|
def perform(index_name, ids)
|
19
|
-
lock.acquire! or raise LockError
|
20
11
|
ActiveSupport::Notifications.instrument('perform.chewy_kiqqer', index_name: index_name, ids: ids) do
|
12
|
+
with_lock(index_name, ids)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
private
|
17
|
+
|
18
|
+
def with_lock(index_name, ids)
|
19
|
+
Sidekiq.redis do |redis|
|
20
|
+
lock_name = "#{index_name}-#{ids.join('-')}"
|
21
|
+
redis.lock(lock_name, life: 60, acquire: 5) { index(index_name, ids) }
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
def index(index_name, ids)
|
26
|
+
ActiveSupport::Notifications.instrument('index.chewy_kiqqer', index_name: index_name, ids: ids) do
|
21
27
|
Chewy.derive_type(index_name).import ids
|
22
28
|
end
|
23
|
-
ensure
|
24
|
-
lock.release!
|
25
29
|
end
|
30
|
+
|
31
|
+
|
26
32
|
end
|
27
33
|
end
|
data/spec/log_subscriber_spec.rb
CHANGED
@@ -19,6 +19,14 @@ describe ChewyKiqqer::LogSubscriber do
|
|
19
19
|
}.to_not raise_error
|
20
20
|
end
|
21
21
|
|
22
|
+
it 'logs an index event' do
|
23
|
+
allow(subscriber).to receive(:logger).and_return(logger)
|
24
|
+
expect(logger).to receive(:debug)
|
25
|
+
expect {
|
26
|
+
subscriber.index(double(payload: { index_name: 'foo', ids: [1,2] }, duration: 0.2))
|
27
|
+
}.to_not raise_error
|
28
|
+
end
|
29
|
+
|
22
30
|
it 'log a queue_jobs event' do
|
23
31
|
allow(subscriber).to receive(:logger).and_return(logger)
|
24
32
|
expect(logger).to receive(:debug)
|
data/spec/worker_spec.rb
CHANGED
@@ -15,11 +15,4 @@ describe ChewyKiqqer::Worker do
|
|
15
15
|
worker.perform('foo#bar', [17])
|
16
16
|
end
|
17
17
|
|
18
|
-
it 'raises a lock error if the lock cannot be acquired' do
|
19
|
-
allow(lock).to receive(:acquire!).and_return(false)
|
20
|
-
expect {
|
21
|
-
worker.perform('foo#bar', [17])
|
22
|
-
}.to raise_error(ChewyKiqqer::Worker::LockError)
|
23
|
-
end
|
24
|
-
|
25
18
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: chewy_kiqqer
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Daniel Hahn
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-07-
|
11
|
+
date: 2014-07-30 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: sidekiq
|
@@ -39,7 +39,7 @@ dependencies:
|
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: '0'
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
|
-
name:
|
42
|
+
name: mlanett-redis-lock
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
44
44
|
requirements:
|
45
45
|
- - '>='
|