Skip to content

[MODEL] Use logger to log index not found message #868

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 4, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 6 additions & 10 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,22 +25,18 @@ matrix:
jdk: oraclejdk8
env: RAILS_VERSIONS=3.0

- rvm: 2.3
- rvm: 2.3.8
jdk: oraclejdk8
env: RAILS_VERSIONS=5.0

- rvm: 2.4
- rvm: 2.6.1
jdk: oraclejdk8
env: RAILS_VERSIONS=5.0
env: RAILS_VERSIONS=4.0,5.0

- rvm: jruby-9.1
- rvm: jruby-9.2.5.0
jdk: oraclejdk8
env: RAILS_VERSIONS=5.0

- rvm: 2.5
jdk: oraclejdk8
env: RAILS_VERSIONS=4.0,5.0

env:
global:
- ELASTICSEARCH_VERSION=6.4.0
Expand All @@ -53,8 +49,8 @@ before_install:
- shasum -a 512 -c elasticsearch-${ELASTICSEARCH_VERSION}.deb.sha512
- sudo dpkg -i --force-confnew elasticsearch-${ELASTICSEARCH_VERSION}.deb
- sudo service elasticsearch start
- gem update --system -q
- gem update bundler -q
- gem update --system
- gem update bundler
- gem --version
- bundle version

Expand Down
6 changes: 4 additions & 2 deletions elasticsearch-model/lib/elasticsearch/model/indexing.rb
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,8 @@ def delete_index!(options={})
self.client.indices.delete index: target_index
rescue Exception => e
if e.class.to_s =~ /NotFound/ && options[:force]
STDERR.puts "[!!!] Index does not exist (#{e.class})"
client.transport.logger.debug("[!!!] Index does not exist (#{e.class})") if client.transport.logger
nil
else
raise e
end
Expand All @@ -295,7 +296,8 @@ def refresh_index!(options={})
self.client.indices.refresh index: target_index
rescue Exception => e
if e.class.to_s =~ /NotFound/ && options[:force]
STDERR.puts "[!!!] Index does not exist (#{e.class})"
client.transport.logger.debug("[!!!] Index does not exist (#{e.class})") if client.transport.logger
nil
else
raise e
end
Expand Down
50 changes: 46 additions & 4 deletions elasticsearch-model/spec/elasticsearch/model/indexing_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -612,7 +612,7 @@ class ::DummyIndexingModelForRecreate
context 'when the index is not found' do

let(:client) do
double('client', indices: indices)
double('client', indices: indices, transport: double('transport', { logger: nil }))
end

let(:indices) do
Expand All @@ -622,14 +622,34 @@ class ::DummyIndexingModelForRecreate
end

before do
expect(DummyIndexingModelForRecreate).to receive(:client).and_return(client)
expect(DummyIndexingModelForRecreate).to receive(:client).at_most(3).times.and_return(client)
end

context 'when the force option is true' do

it 'deletes the index without raising an exception' do
expect(DummyIndexingModelForRecreate.delete_index!(force: true)).to be_nil
end

context 'when the client has a logger' do

let(:logger) do
Logger.new(STDOUT).tap { |l| l.level = Logger::DEBUG }
end

let(:client) do
double('client', indices: indices, transport: double('transport', { logger: logger }))
end

it 'deletes the index without raising an exception' do
expect(DummyIndexingModelForRecreate.delete_index!(force: true)).to be_nil
end

it 'logs the message that the index is not found' do
expect(logger).to receive(:debug)
expect(DummyIndexingModelForRecreate.delete_index!(force: true)).to be_nil
end
end
end

context 'when the force option is not provided' do
Expand Down Expand Up @@ -799,6 +819,8 @@ class ::DummyIndexingModelForCreate
expect(DummyIndexingModelForCreate.create_index!(index: 'custom-foo'))
end
end

context 'when the logging level is debug'
end

describe '#refresh_index!' do
Expand All @@ -824,15 +846,15 @@ class ::DummyIndexingModelForRefresh
end

let(:client) do
double('client', indices: indices)
double('client', indices: indices, transport: double('transport', { logger: nil }))
end

let(:indices) do
double('indices')
end

before do
expect(DummyIndexingModelForRefresh).to receive(:client).and_return(client)
expect(DummyIndexingModelForRefresh).to receive(:client).at_most(3).times.and_return(client)
end

context 'when the force option is true' do
Expand All @@ -846,6 +868,26 @@ class ::DummyIndexingModelForRefresh
it 'does not raise an exception' do
expect(DummyIndexingModelForRefresh.refresh_index!(force: true)).to be_nil
end

context 'when the client has a logger' do

let(:logger) do
Logger.new(STDOUT).tap { |l| l.level = Logger::DEBUG }
end

let(:client) do
double('client', indices: indices, transport: double('transport', { logger: logger }))
end

it 'does not raise an exception' do
expect(DummyIndexingModelForRefresh.refresh_index!(force: true)).to be_nil
end

it 'logs the message that the index is not found' do
expect(logger).to receive(:debug)
expect(DummyIndexingModelForRefresh.refresh_index!(force: true)).to be_nil
end
end
end

context 'when the operation raises another type of exception' do
Expand Down