chewy_kiqqer 0.1.4 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: cb3f4ba0613a2a7aaf26d0420b24b04b71e4e7d8
4
- data.tar.gz: eb448452aea67e4fd1cdc10abd2994730b9313e0
3
+ metadata.gz: 18f4132a1ead16bf52fdad2c82b2e2ad915847f8
4
+ data.tar.gz: d7c229850a348c5218eb79bbf7c60bb1a3da2671
5
5
  SHA512:
6
- metadata.gz: cc1e952e7d5563ba29a3c66062dd3b60b530b97e6a0bd0823b21f480ab21e2dc1028d0890c3940dbb198e234ef5e492d05b394c369564a99170599a39c3495e5
7
- data.tar.gz: 58781a4e0677d625b432a733b46cd741f6ba9fd647d6d72350c0aca3824563d62c2e8a3249f8eec2acfdc34fa85d0b660266f43e86ca2513609d6e5e75ab3207
6
+ metadata.gz: 5b49b9869ccd87ae8c504d79fec9968465787a69ca00f2a17b38d26f3622de6e3d8be9c66853fe8775c605e07de1021bf592ddab5b4baaab507dd3ab7c1b5db5
7
+ data.tar.gz: d7cd065b06dfb7060cfea6eea902f37078f5dcb2db8644007d052cbc68359331cab904c9c5f8f92147d4058c6750291b756d92eeb423c4b74289d316185dc94c
data/.travis.yml ADDED
@@ -0,0 +1,7 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.0.0
4
+ - 2.1.2
5
+ addons:
6
+ code_climate:
7
+ repo_token: dadcc13fd018e8813c5052a604bec42110d8f4c0931414639973cb94e5e150cb
data/README.md CHANGED
@@ -1,5 +1,9 @@
1
1
  # ChewyKiqqer
2
2
 
3
+ [![Build Status](https://travis-ci.org/averell23/chewy_kiqqer.svg?branch=master)](https://travis-ci.org/averell23/chewy_kiqqer)
4
+ [![Code Climate](https://codeclimate.com/github/averell23/chewy_kiqqer.png)](https://codeclimate.com/github/averell23/chewy_kiqqer)
5
+ [![Test Coverage](https://codeclimate.com/github/averell23/chewy_kiqqer/coverage.png)](https://codeclimate.com/github/averell23/chewy_kiqqer)
6
+
3
7
  This is an alternative udpate/callback mechanism for [Chewy](https://github.com/toptal/chewy). It queues the updates as [Sidekiq](https://github.com/mperham/sidekiq) jobs.
4
8
 
5
9
  You can pass backrefs like with the standard chewy mechanism, but the job itself will always receive an array of ids.
@@ -45,6 +49,8 @@ which will be indexed. The default is to use the current record.
45
49
  # Pass a proc. It will be called with the current record to get the backref
46
50
  ... backref: -> (rec) { |rec| rec.do_something }
47
51
 
52
+ ## Update handling
53
+
48
54
  ## Contributing
49
55
 
50
56
  1. Fork it
data/Rakefile CHANGED
@@ -1,6 +1,8 @@
1
1
  require "bundler/gem_tasks"
2
2
  require 'rspec/core/rake_task'
3
3
 
4
+ task default: :spec
5
+
4
6
  RSpec::Core::RakeTask.new(:spec)
5
7
 
6
8
  Bundler.require
data/chewy_kiqqer.gemspec CHANGED
@@ -20,9 +20,12 @@ Gem::Specification.new do |spec|
20
20
 
21
21
  spec.add_dependency 'sidekiq'
22
22
  spec.add_dependency 'chewy'
23
+ spec.add_dependency 'sidekiq-lock'
24
+ spec.add_dependency 'activesupport', '>= 3.2.0'
23
25
 
24
26
  spec.add_development_dependency "bundler", "~> 1.3"
25
27
  spec.add_development_dependency "rake"
26
28
  spec.add_development_dependency "kaminari"
27
29
  spec.add_development_dependency "rspec"
30
+ spec.add_development_dependency "codeclimate-test-reporter"
28
31
  end
data/lib/chewy_kiqqer.rb CHANGED
@@ -1,4 +1,12 @@
1
+ require 'active_support/notifications'
2
+ require 'active_support/log_subscriber'
3
+
1
4
  require "chewy_kiqqer/version"
2
5
  require "chewy_kiqqer/mixin"
3
6
  require "chewy_kiqqer/worker"
4
7
  require "chewy_kiqqer/index"
8
+ require "chewy_kiqqer/logger"
9
+ require "chewy_kiqqer/log_subscriber"
10
+
11
+
12
+ ChewyKiqqer::LogSubscriber.attach_to 'chewy_kiqqer'
@@ -0,0 +1,19 @@
1
+ module ChewyKiqqer
2
+
3
+ class LogSubscriber < ActiveSupport::LogSubscriber
4
+
5
+ def perform(event)
6
+ info "ChewyKiqqer performed on #{event.payload[:index_name]} with ids #{event.payload[:ids]} - duration #{event.duration}"
7
+ end
8
+
9
+ def queue_jobs(event)
10
+ debug "ChewyKiqqer queued jobs from #{event.payload[:class]} #{event.payload[:id]}"
11
+ end
12
+
13
+ def logger
14
+ ChewyKiqqer.logger
15
+ end
16
+
17
+ end
18
+
19
+ end
@@ -0,0 +1,13 @@
1
+ require 'logger'
2
+
3
+ module ChewyKiqqer
4
+
5
+ def self.logger
6
+ @logger
7
+ end
8
+
9
+ def self.logger=(value)
10
+ @logger = value
11
+ end
12
+
13
+ end
@@ -35,8 +35,10 @@ module ChewyKiqqer
35
35
  end
36
36
 
37
37
  def queue_chewy_jobs
38
- self.class.indexers or return
39
- self.class.indexers.each { |idx| idx.enqueue(self) }
38
+ ActiveSupport::Notifications.instrument('queue_jobs.chewy_kiqqer', class: self.class.name, id: self.id) do
39
+ self.class.indexers or return
40
+ self.class.indexers.each { |idx| idx.enqueue(self) }
41
+ end
40
42
  end
41
43
  end
42
44
  end
@@ -1,3 +1,3 @@
1
1
  module ChewyKiqqer
2
- VERSION = "0.1.4"
2
+ VERSION = "0.2.0"
3
3
  end
@@ -1,11 +1,27 @@
1
1
  require 'sidekiq'
2
+ require 'sidekiq-lock'
2
3
 
3
4
  module ChewyKiqqer
4
5
  class Worker
6
+
7
+ class LockError < StandardError ; end
8
+
5
9
  include Sidekiq::Worker
10
+ include Sidekiq::Lock::Worker
11
+
12
+ sidekiq_options lock: {
13
+ timeout: 3000,
14
+ name: ->(index_name, ids) { "lock:chewy_kiqqer:#{ids}-#{[ids].flatten.sort.join('-')}" }
15
+ }
16
+
6
17
 
7
18
  def perform(index_name, ids)
8
- Chewy.derive_type(index_name).import ids
19
+ lock.acquire! or raise LockError
20
+ ActiveSupport::Notifications.instrument('perform.chewy_kiqqer', index_name: index_name, ids: ids) do
21
+ Chewy.derive_type(index_name).import ids
22
+ end
23
+ ensure
24
+ lock.release!
9
25
  end
10
26
  end
11
27
  end
data/spec/index_spec.rb CHANGED
@@ -1,4 +1,4 @@
1
- require 'chewy_kiqqer'
1
+ require 'spec_helper'
2
2
 
3
3
  describe ChewyKiqqer::Index do
4
4
 
@@ -7,30 +7,30 @@ describe ChewyKiqqer::Index do
7
7
  it 'defaults to the corresponding object' do
8
8
  object = Object.new
9
9
  idx = ChewyKiqqer::Index.new
10
- idx.backref(object).should eq object
10
+ expect(idx.backref(object)).to eq object
11
11
  end
12
12
 
13
13
  it 'can run a random method' do
14
14
  object = double(foobar: :backref)
15
15
  idx = ChewyKiqqer::Index.new(backref: :foobar)
16
- idx.backref(object).should eq :backref
16
+ expect(idx.backref(object)).to eq :backref
17
17
  end
18
18
 
19
19
  it 'can use a proc' do
20
20
  object = double(foobar: :my_backref)
21
21
  idx = ChewyKiqqer::Index.new(backref: -> (r) { r.foobar })
22
- idx.backref(object).should eq :my_backref
22
+ expect(idx.backref(object)).to eq :my_backref
23
23
  end
24
24
 
25
25
  context 'turning backrefs into ids' do
26
26
  let(:idx) { ChewyKiqqer::Index.new }
27
27
 
28
28
  it 'uses the ids of the objects' do
29
- idx.backref_ids([double(id: 3), double(id: 6)]).should eq [3, 6]
29
+ expect(idx.backref_ids([double(id: 3), double(id: 6)])).to eq [3, 6]
30
30
  end
31
31
 
32
32
  it 'turns everything else into ints' do
33
- idx.backref_ids([3, '6']).should eq [3, 6]
33
+ expect(idx.backref_ids([3, '6'])).to eq [3, 6]
34
34
  end
35
35
 
36
36
  end
@@ -41,7 +41,7 @@ describe ChewyKiqqer::Index do
41
41
  idx = ChewyKiqqer::Index.new index: 'foo#bar',
42
42
  queue: 'hello'
43
43
 
44
- Sidekiq::Client.should_receive(:push)
44
+ expect(Sidekiq::Client).to receive(:push)
45
45
  .with('queue' => 'hello',
46
46
  'class' => ChewyKiqqer::Worker,
47
47
  'args' => ['foo#bar', [24]])
@@ -53,7 +53,7 @@ describe ChewyKiqqer::Index do
53
53
  ChewyKiqqer.default_queue = :my_default
54
54
  idx = ChewyKiqqer::Index.new index: 'foo#bar'
55
55
 
56
- Sidekiq::Client.should_receive(:push)
56
+ expect(Sidekiq::Client).to receive(:push)
57
57
  .with('queue' => :my_default,
58
58
  'class' => ChewyKiqqer::Worker,
59
59
  'args' => ['foo#bar', [24]])
@@ -0,0 +1,30 @@
1
+ require 'spec_helper'
2
+
3
+ describe ChewyKiqqer::LogSubscriber do
4
+
5
+ let(:subscriber) { ChewyKiqqer::LogSubscriber.new }
6
+ let(:logger) { Logger.new(nil) }
7
+
8
+ it 'logs a perform with the default logger' do
9
+ expect {
10
+ subscriber.perform(double(payload: { index_name: 'foo', ids: [1,2] }, duration: 0.2))
11
+ }.to_not raise_error
12
+ end
13
+
14
+ it 'logs a perform event' do
15
+ allow(subscriber).to receive(:logger).and_return(logger)
16
+ expect(logger).to receive(:info)
17
+ expect {
18
+ subscriber.perform(double(payload: { index_name: 'foo', ids: [1,2] }, duration: 0.2))
19
+ }.to_not raise_error
20
+ end
21
+
22
+ it 'log a queue_jobs event' do
23
+ allow(subscriber).to receive(:logger).and_return(logger)
24
+ expect(logger).to receive(:debug)
25
+ expect {
26
+ subscriber.queue_jobs(double(payload: { class: 'Huha', id: 123 }))
27
+ }.to_not raise_error
28
+ end
29
+
30
+ end
@@ -0,0 +1,14 @@
1
+ require 'spec_helper'
2
+
3
+ describe '#logger' do
4
+
5
+
6
+ after(:each) { ChewyKiqqer.logger = nil }
7
+
8
+ it 'can be set from the outside' do
9
+ ChewyKiqqer.logger = :nolog
10
+ expect(ChewyKiqqer.logger).to eq(:nolog)
11
+ end
12
+
13
+
14
+ end
data/spec/mixin_spec.rb CHANGED
@@ -1,10 +1,12 @@
1
- require 'chewy_kiqqer'
1
+ require 'spec_helper'
2
2
 
3
3
  class Gummi
4
4
  include ChewyKiqqer::Mixin
5
5
 
6
6
  def self.after_commit(*args) ; end
7
7
 
8
+ def id ; 17 ; end
9
+
8
10
  end
9
11
 
10
12
  describe ChewyKiqqer::Mixin do
@@ -14,22 +16,22 @@ describe ChewyKiqqer::Mixin do
14
16
  context '#update_index' do
15
17
 
16
18
  it 'installs the hooks if nothing was installed yet' do
17
- Gummi.should_receive(:install_chewy_hooks)
19
+ expect(Gummi).to receive(:install_chewy_hooks)
18
20
  Gummi.async_update_index(index: 'xxx')
19
21
  end
20
22
 
21
23
  it 'does not install the hooks twice' do
22
- Gummi.should_receive(:install_chewy_hooks).once
24
+ expect(Gummi).to receive(:install_chewy_hooks).once
23
25
  Gummi.async_update_index(index: 'xxx')
24
26
  Gummi.async_update_index(index: 'xxx')
25
27
  end
26
28
 
27
29
  it 'installs the indexer' do
28
- ChewyKiqqer::Index.should_receive(:new)
30
+ expect(ChewyKiqqer::Index).to receive(:new)
29
31
  .with(index: 'some#thing', queue: :medium, backref: :foobar)
30
32
  .and_return(:idx)
31
33
  Gummi.async_update_index(index: 'some#thing', queue: :medium, backref: :foobar)
32
- Gummi.indexers.should eq [:idx]
34
+ expect(Gummi.indexers).to eq [:idx]
33
35
  end
34
36
 
35
37
  end
@@ -37,7 +39,7 @@ describe ChewyKiqqer::Mixin do
37
39
  context '#install hooks' do
38
40
 
39
41
  it 'installs the hooks' do
40
- Gummi.should_receive(:after_commit).with(:queue_chewy_jobs)
42
+ expect(Gummi).to receive(:after_commit).with(:queue_chewy_jobs)
41
43
  Gummi.install_chewy_hooks
42
44
  end
43
45
 
@@ -49,8 +51,8 @@ describe ChewyKiqqer::Mixin do
49
51
 
50
52
  it 'queues the job' do
51
53
  idx = double
52
- idx.should_receive(:enqueue).with(record)
53
- Gummi.stub(indexers: [idx])
54
+ expect(idx).to receive(:enqueue).with(record)
55
+ allow(Gummi).to receive(:indexers).and_return([idx])
54
56
  record.queue_chewy_jobs
55
57
  end
56
58
 
@@ -0,0 +1,5 @@
1
+ require "codeclimate-test-reporter"
2
+ CodeClimate::TestReporter.start
3
+
4
+ require 'chewy_kiqqer'
5
+ require 'chewy'
data/spec/worker_spec.rb CHANGED
@@ -1,16 +1,25 @@
1
- require 'chewy_kiqqer'
2
- require 'chewy'
1
+ require 'spec_helper'
3
2
 
4
3
  describe ChewyKiqqer::Worker do
5
4
 
6
5
  let(:worker) { ChewyKiqqer::Worker.new }
6
+ let(:lock) { double(acquire!: true, release!: true) }
7
+
8
+ before(:each) { allow(worker).to receive(:lock).and_return(lock) }
7
9
 
8
10
  it 'calls the indexing with chewy' do
9
11
  index = double
10
- Chewy.should_receive(:derive_type).with('foo#bar').and_return(index)
11
- index.should_receive(:import).with([17])
12
+ expect(Chewy).to receive(:derive_type).with('foo#bar').and_return(index)
13
+ expect(index).to receive(:import).with([17])
12
14
 
13
15
  worker.perform('foo#bar', [17])
14
16
  end
15
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
+
16
25
  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.1.4
4
+ version: 0.2.0
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-06-03 00:00:00.000000000 Z
11
+ date: 2014-07-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: sidekiq
@@ -38,6 +38,34 @@ dependencies:
38
38
  - - '>='
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: sidekiq-lock
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: activesupport
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '>='
60
+ - !ruby/object:Gem::Version
61
+ version: 3.2.0
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '>='
67
+ - !ruby/object:Gem::Version
68
+ version: 3.2.0
41
69
  - !ruby/object:Gem::Dependency
42
70
  name: bundler
43
71
  requirement: !ruby/object:Gem::Requirement
@@ -94,6 +122,20 @@ dependencies:
94
122
  - - '>='
95
123
  - !ruby/object:Gem::Version
96
124
  version: '0'
125
+ - !ruby/object:Gem::Dependency
126
+ name: codeclimate-test-reporter
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - '>='
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - '>='
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
97
139
  description: Sidekiq integration for chewy and sidekiq
98
140
  email:
99
141
  - dha@betterplace.org
@@ -102,6 +144,7 @@ extensions: []
102
144
  extra_rdoc_files: []
103
145
  files:
104
146
  - .gitignore
147
+ - .travis.yml
105
148
  - Gemfile
106
149
  - LICENSE.txt
107
150
  - README.md
@@ -109,11 +152,16 @@ files:
109
152
  - chewy_kiqqer.gemspec
110
153
  - lib/chewy_kiqqer.rb
111
154
  - lib/chewy_kiqqer/index.rb
155
+ - lib/chewy_kiqqer/log_subscriber.rb
156
+ - lib/chewy_kiqqer/logger.rb
112
157
  - lib/chewy_kiqqer/mixin.rb
113
158
  - lib/chewy_kiqqer/version.rb
114
159
  - lib/chewy_kiqqer/worker.rb
115
160
  - spec/index_spec.rb
161
+ - spec/log_subscriber_spec.rb
162
+ - spec/logger_spec.rb
116
163
  - spec/mixin_spec.rb
164
+ - spec/spec_helper.rb
117
165
  - spec/worker_spec.rb
118
166
  homepage: ''
119
167
  licenses:
@@ -135,12 +183,15 @@ required_rubygems_version: !ruby/object:Gem::Requirement
135
183
  version: '0'
136
184
  requirements: []
137
185
  rubyforge_project:
138
- rubygems_version: 2.0.3
186
+ rubygems_version: 2.0.14
139
187
  signing_key:
140
188
  specification_version: 4
141
189
  summary: Small helper gem that allows you to automatically run all chewy index updates
142
190
  in sidekiq
143
191
  test_files:
144
192
  - spec/index_spec.rb
193
+ - spec/log_subscriber_spec.rb
194
+ - spec/logger_spec.rb
145
195
  - spec/mixin_spec.rb
196
+ - spec/spec_helper.rb
146
197
  - spec/worker_spec.rb