Commit a32fc5f3 authored by Nick Thomas's avatar Nick Thomas

Only use the experimental elasticsearch indexer if it is present

parent c13ff72c
......@@ -7,12 +7,15 @@
= f.check_box :elasticsearch_indexing
Elasticsearch indexing
- missing = !Gitlab::Elastic::Indexer.experimental_indexer_present?
.form-group
.col-sm-offset-2.col-sm-10
.checkbox
= f.label :elasticsearch_experimental_indexer do
= f.check_box :elasticsearch_experimental_indexer
Use experimental repository indexer
= f.check_box :elasticsearch_experimental_indexer, disabled: missing
Use <a href="https://gitlab.com/gitlab-org/gitlab-elasticsearch-indexer">experimental repository indexer</a>
- if missing
(not installed)
.form-group
.col-sm-offset-2.col-sm-10
......
......@@ -10,6 +10,10 @@ module Gitlab
Error = Class.new(StandardError)
def self.experimental_indexer_present?
Gitlab::Utils.which(EXPERIMENTAL_INDEXER).present?
end
attr_reader :project
def initialize(project)
......@@ -46,7 +50,7 @@ module Gitlab
end
def path_to_indexer
if current_application_settings.elasticsearch_experimental_indexer?
if current_application_settings.elasticsearch_experimental_indexer? && self.class.experimental_indexer_present?
EXPERIMENTAL_INDEXER
else
Rails.root.join('bin', 'elastic_repo_indexer').to_s
......
......@@ -27,5 +27,22 @@ module Gitlab
rescue ArgumentError
size
end
# See: http://stackoverflow.com/questions/2108727/which-in-ruby-checking-if-program-exists-in-path-from-ruby
# Cross-platform way of finding an executable in the $PATH.
#
# which('ruby') #=> /usr/bin/ruby
def which(cmd, env = ENV)
exts = env['PATHEXT'] ? env['PATHEXT'].split(';') : ['']
env['PATH'].split(File::PATH_SEPARATOR).each do |path|
exts.each do |ext|
exe = File.join(path, "#{cmd}#{ext}")
return exe if File.executable?(exe) && !File.directory?(exe)
end
end
nil
end
end
end
......@@ -86,12 +86,20 @@ describe Gitlab::Elastic::Indexer do
end
end
context 'experimental indexer present' do
context 'experimental indexer enabled' do
before do
stub_application_setting(elasticsearch_experimental_indexer: true)
end
it 'uses the experimental indexer' do
it 'uses the normal indexer when not present' do
expect(described_class).to receive(:experimental_indexer_present?).and_return(false)
expect_popen.with([Rails.root.join('bin/elastic_repo_indexer').to_s, anything, anything], anything, anything).and_return(popen_success)
indexer.run
end
it 'uses the experimental indexer when present' do
expect(described_class).to receive(:experimental_indexer_present?).and_return(true)
expect_popen.with(['gitlab-elasticsearch-indexer', anything, anything], anything, anything).and_return(popen_success)
indexer.run
......
require 'spec_helper'
describe Gitlab::Utils, lib: true do
delegate :to_boolean, to: :described_class
delegate :to_boolean, :which, to: :described_class
describe '.to_boolean' do
it 'accepts booleans' do
......@@ -30,4 +32,12 @@ describe Gitlab::Utils, lib: true do
expect(to_boolean(nil)).to be_nil
end
end
describe '.which' do
it 'finds the full path to an executable binary' do
expect(File).to receive(:executable?).with('/bin/sh').and_return(true)
expect(which('sh', 'PATH' => '/bin')).to eq('/bin/sh')
end
end
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment