Skip to content
This repository has been archived by the owner on Mar 21, 2018. It is now read-only.

Commit

Permalink
refactor(push): Split push logic in several methods
Browse files Browse the repository at this point in the history
  • Loading branch information
pixelastic committed Jul 21, 2015
1 parent 73f1d90 commit bd96080
Showing 1 changed file with 21 additions and 15 deletions.
36 changes: 21 additions & 15 deletions lib/push.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ def init_options(args = [], options = {}, config = {})
@options = options
@config = config
@is_verbose = @config['verbose']
@is_dry_run = @config['dry_run']

self
end
Expand Down Expand Up @@ -120,32 +121,37 @@ def configure_index(index)
index.set_settings(settings)
end

def push(items)
AlgoliaSearchCredentialChecker.new(@config).assert_valid

is_dry_run = @config['dry_run']
Jekyll.logger.info '=== DRY RUN ===' if is_dry_run

# Create a temporary index
index_name = @config['algolia']['index_name']
index_name_tmp = "#{index_name}_tmp"
index_tmp = Algolia::Index.new(index_name_tmp)
configure_index(index_tmp) unless is_dry_run
# Create an index to push our data
def create_index(index_name)
index = Algolia::Index.new(index_name)
configure_index(index) unless @is_dry_run
index
end

# Push to temporary index
# Push records to the index
def batch_add_items(items, index)
items.each_slice(1000) do |batch|
Jekyll.logger.info "Indexing #{batch.size} items"
begin
index_tmp.add_objects!(batch) unless is_dry_run
index.add_objects!(batch) unless @is_dry_run
rescue StandardError => error
Jekyll.logger.error 'Algolia Error: HTTP Error'
Jekyll.logger.warn error.message
exit 1
end
end
end

def push(items)
AlgoliaSearchCredentialChecker.new(@config).assert_valid

# Move temporary index to real one
Algolia.move_index(index_name_tmp, index_name) unless is_dry_run
Jekyll.logger.info '=== DRY RUN ===' if @is_dry_run

# Add items to a temp index, then rename it
index_name = @config['algolia']['index_name']
index_name_tmp = "#{index_name}_tmp"
batch_add_items(items, create_index(index_name_tmp))
Algolia.move_index(index_name_tmp, index_name) unless @is_dry_run

Jekyll.logger.info "Indexing of #{items.size} items " \
"in #{index_name} done."
Expand Down

0 comments on commit bd96080

Please sign in to comment.