Skip to content

Commit

Permalink
:wait_for_status chewy config option
Browse files Browse the repository at this point in the history
  • Loading branch information
pyromaniac committed Mar 5, 2014
1 parent a435d41 commit b621d55
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# master

* `Chewy.configuration[:wait_for_status]` option. Can be set to `red`, `yellow` or `green`. If set - chewy will wait for cluster status before creating, deleting index and import. Useful for specs.

# Version 0.3.0

* Added `Chewy.configuration[:index]` config to setup common indexes options.
Expand Down
4 changes: 4 additions & 0 deletions lib/chewy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ def self.derive_type name
end
end

def self.wait_for_status
client.cluster.health wait_for_status: Chewy.configuration[:wait_for_status] if Chewy.configuration[:wait_for_status].present?
end

def self.config
Chewy::Config.instance
end
Expand Down
5 changes: 5 additions & 0 deletions lib/chewy/index/actions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ def create *args
def create! *args
options = args.extract_options!.reverse_merge!(alias: true)
name = build_index_name(suffix: args.first)

Chewy.wait_for_status

result = client.indices.create(index: name, body: index_params)
result &&= client.indices.put_alias(index: name, name: index_name) if options[:alias] && name != index_name
result
Expand Down Expand Up @@ -84,6 +87,8 @@ def delete suffix = nil
# UsersIndex.delete '01-2014' # deletes `users_01-2014` index
#
def delete! suffix = nil
Chewy.wait_for_status

client.indices.delete index: build_index_name(suffix: suffix)
end

Expand Down
3 changes: 3 additions & 0 deletions lib/chewy/type/import.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ def import! *args
# Adds `:suffix` option to bulk import to index with specified suffix.
def bulk options = {}
suffix = options.delete(:suffix)

Chewy.wait_for_status

result = client.bulk options.merge(index: index.build_index_name(suffix: suffix), type: type_name)

extract_errors result
Expand Down
9 changes: 8 additions & 1 deletion spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,14 @@
end
end

Chewy.configuration = {host: 'localhost:9250', index: {number_of_shards: 1, number_of_replicas: 0}}
Chewy.configuration = {
host: 'localhost:9250',
wait_for_status: 'green',
index: {
number_of_shards: 1,
number_of_replicas: 0
}
}

RSpec.configure do |config|
config.mock_with :rspec
Expand Down

0 comments on commit b621d55

Please sign in to comment.