Browse files

moved warmer_interface.rb

  • Loading branch information...
1 parent d9ac862 commit d305758263e96f426df3c1148fdba43a0f2f8a04 Missy committed Apr 30, 2013
Showing with 64 additions and 0 deletions.
  1. +64 −0 squirrel/warmer_interface.rb
View
64 squirrel/warmer_interface.rb
@@ -0,0 +1,64 @@
+require 'multi_json'
+require 'httparty'
+
+class WarmerInterface
+ def initialize(options = {})
+ puts options.inspect
+ @host = options[:host]
+ @port = options[:port]
+ @query = MultiJson.dump(options[:query])
+ @warmer_name = options[:warmer_name]
+ @index = options[:index]
+ @action = options[:action]
+ @warmer_state = nil
+ end
+
+ def add_warmer
+ url = "http://#{@host}:#{@port}/#{@index}/_warmer/#{@warmer_name}"
+ puts url
+ puts @query
+ response = HTTParty.put(url, {:body => @query})
+ #puts "`curl -s -XPUT '#{@host}:#{@port}/#{@index}/_warmer/#{@warmer_name}' -d '#{@query}'`"
+ #puts "\n"
+ puts response
+ #output = %x(curl -s -XPUT '#{@host}:#{@port}/#{@index}/_warmer/#{@warmer_name}' -d '#{@query}')
+ #puts output
+ end
+
+ def remove_warmer
+ puts "removing warmer #{@warmer_name}"
+ `curl -s -XDELETE #{@host}:#{@port}/#{@index}/_warmer/#{@warmer_name}`
+ end
+
+ def enable_warmer
+ puts "closing #{@index}"
+ `curl -s -XPOST '#{@host}:#{@port}/#{@index}/_close'`
+ puts "enabling warmer"
+ `curl -s -XPUT '#{@host}:#{@port}/#{@index}/_settings?pretty=true' -d '{"index.warmer.enabled":"true"}'`
+ puts "opening #{@index}"
+ `curl -s -XPOST '#{@host}:#{@port}/#{@index}/_open'`
+ end
+
+ def disable_warmer
+ puts "closing #{@index}"
+ `curl -s -XPOST '#{@host}:#{@port}/#{@index}/_close'`
+ puts "disabling warmer"
+ `curl -s -XPUT '#{@host}:#{@port}/#{@index}/_settings?pretty=true' -d '{"index.warmer.enabled":"false"}'`
+ puts "opening #{@index}"
+ `curl -s -XPOST '#{@host}:#{@port}/#{@index}/_open'`
+ end
+
+ def determine_interaction
+ unless @index.nil? || @host.nil? || @port.nil?
+ case command = @action.to_sym
+ when :add_warmer then add_warmer
+ when :remove_warmer then remove_warmer
+ when :enable_warmer then enable_warmer
+ when :disable_warmer then disable_warmer
+ else abort "#{command} is not a recognized action for determine_interaction from warmers_interface"
+ end
+ else
+ puts "index, host and port are required to interact with the warmers"
+ end
+ end
+end

0 comments on commit d305758

Please sign in to comment.