Parent

Included Modules

Chef::Expander::Solrizer

Attributes

action[R]
chef_object[R]
database[R]
enqueued_at[R]
indexer_payload[R]
obj_id[R]
obj_type[R]

Public Class Methods

clear_http_requests() click to toggle source
# File lib/chef/expander/solrizer.rb, line 46
def self.clear_http_requests
  @active_http_requests.clear
end
http_request_completed(instance) click to toggle source
# File lib/chef/expander/solrizer.rb, line 38
def self.http_request_completed(instance)
  @active_http_requests.delete(instance)
end
http_request_started(instance) click to toggle source
# File lib/chef/expander/solrizer.rb, line 34
def self.http_request_started(instance)
  @active_http_requests << instance
end
http_requests_active?() click to toggle source
# File lib/chef/expander/solrizer.rb, line 42
def self.http_requests_active?
  !@active_http_requests.empty?
end
new(object_command_json, &on_completion_block) click to toggle source
# File lib/chef/expander/solrizer.rb, line 85
def initialize(object_command_json, &on_completion_block)
  @start_time = Time.now.to_f
  @on_completion_block = on_completion_block
  if parsed_message    = parse(object_command_json)
    @action           = parsed_message["action"]
    @indexer_payload  = parsed_message["payload"]

    extract_object_fields if @indexer_payload
  else
    @action = SKIP
  end
end

Public Instance Methods

add() click to toggle source
# File lib/chef/expander/solrizer.rb, line 128
def add
  post_to_solr(pointyize_add) do
    ["indexed #{indexed_object}",
     "transit,xml,solr-post |",
     [transit_time, @xml_time, @solr_post_time].join(","),
     "|"
    ].join(" ")
  end
rescue Exception => e
  log.error { "#{e.class.name}: #{e.message}\n#{e.backtrace.join("\n")}"}
end
completed() click to toggle source
# File lib/chef/expander/solrizer.rb, line 243
def completed
  @solr_post_time = Time.now.to_f - @start_time
  self.class.http_request_completed(self)
  @on_completion_block.call
end
delete() click to toggle source
# File lib/chef/expander/solrizer.rb, line 140
def delete
  post_to_solr(pointyize_delete) { "deleted #{indexed_object} transit-time[#{transit_time}s]"}
rescue Exception => e
  log.error { "#{e.class.name}: #{e.message}\n#{e.backtrace.join("\n")}"}
end
eql?(other) click to toggle source
# File lib/chef/expander/solrizer.rb, line 265
def eql?(other)
  other.hash == hash
end
extract_object_fields() click to toggle source
# File lib/chef/expander/solrizer.rb, line 98
def extract_object_fields
  @chef_object = @indexer_payload[ITEM]
  @database    = @indexer_payload[DATABASE]
  @obj_id      = @indexer_payload[ID]
  @obj_type    = @indexer_payload[TYPE]
  @enqueued_at = @indexer_payload[ENQUEUED_AT]
  @data_bag = @obj_type == DATA_BAG_ITEM ? @chef_object[DATA_BAG] : nil
end
flattened_object() click to toggle source
# File lib/chef/expander/solrizer.rb, line 146
def flattened_object
  flattened_object = Flattener.new(@chef_object).flattened_item
 
  flattened_object[X_CHEF_id_CHEF_X]        = [@obj_id]
  flattened_object[X_CHEF_database_CHEF_X]  = [@database]
  flattened_object[X_CHEF_type_CHEF_X]      = [@obj_type]

  log.debug {"adding flattened object to Solr: #{flattened_object.inspect}"}

  flattened_object
end
hash() click to toggle source
# File lib/chef/expander/solrizer.rb, line 269
def hash
  "#{action}#{indexed_object}#@enqueued_at#{self.class.name}".hash
end
http_request_started() click to toggle source
# File lib/chef/expander/solrizer.rb, line 261
def http_request_started
  self.class.http_request_started(self)
end
indexed_object() click to toggle source
# File lib/chef/expander/solrizer.rb, line 257
def indexed_object
  "#{@obj_type}[#{@obj_id}] database[#{@database}]"
end
parse(serialized_object) click to toggle source
# File lib/chef/expander/solrizer.rb, line 107
def parse(serialized_object)
  Yajl::Parser.parse(serialized_object)
rescue Yajl::ParseError
  log.error { "cannot index object because it is invalid JSON: #{serialized_object}" }
end
pointyize_add() click to toggle source

Takes a flattened hash where the values are arrays and converts it into a dignified XML document suitable for POST to Solr. The general structure of the output document is like this:

<?xml version="1.0" encoding="UTF-8"?>
<add>
  <doc>
    <field name="content">
        key__=__value
        key__=__another_value
        other_key__=__yet another value
    </field>
  </doc>
</add>

The document as generated has minimal newlines and formatting, however.

# File lib/chef/expander/solrizer.rb, line 189
def pointyize_add
  xml = ""
  xml << START_XML << ADD_DOC
  xml << (FLD_CHEF_ID_FMT % @obj_id)
  xml << (FLD_CHEF_DB_FMT % @database)
  xml << (FLD_CHEF_TY_FMT % @obj_type)
  xml << START_CONTENT
  content = ""
  flattened_object.each do |field, values|
    values.each do |v|
      content << (KEYVAL_FMT % [field, v])
    end
  end
  xml << content.fast_xs
  xml << CLOSE_FIELD      # ends content
  xml << (FLD_DATA_BAG % @data_bag.fast_xs) if @data_bag
  xml << END_ADD_DOC
  @xml_time = Time.now.to_f - @start_time
  xml
end
pointyize_delete() click to toggle source

Takes a succinct document id, like 2342, and turns it into something even more compact, like

"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<delete><id>2342</id></delete>\n"
# File lib/chef/expander/solrizer.rb, line 213
def pointyize_delete
  xml = ""
  xml << START_XML
  xml << DELETE_DOC
  xml << ID_OPEN
  xml << @obj_id.to_s
  xml << ID_CLOSE
  xml << END_DELETE
  xml
end
post_to_solr(document, &logger_block) click to toggle source
# File lib/chef/expander/solrizer.rb, line 224
def post_to_solr(document, &logger_block)
  log.debug("POSTing document to SOLR:\n#{document}")
  http_req = EventMachine::HttpRequest.new(solr_url).post(:body => document, :timeout => 1200, :head => CONTENT_TYPE_XML)
  http_request_started

  http_req.callback do
    completed
    if http_req.response_header.status == 200
      log.info(&logger_block)
    else
      log.error { "Failed to post to solr: #{indexed_object}" }
    end
  end
  http_req.errback do
    completed
    log.error { "Failed to post to solr (connection error): #{indexed_object}" }
  end
end
run() click to toggle source
# File lib/chef/expander/solrizer.rb, line 113
def run
  case @action
  when ADD
    add
  when DELETE
    delete
  when SKIP
    completed
    log.info { "not indexing this item because of malformed JSON"}
  else
    completed
    log.error { "cannot index object becuase it has an invalid action #{@action}" }
  end
end
solr_url() click to toggle source
# File lib/chef/expander/solrizer.rb, line 253
def solr_url
  "#{Expander.config.solr_url}/update"
end
transit_time() click to toggle source
# File lib/chef/expander/solrizer.rb, line 249
def transit_time
  Time.now.utc.to_i - @enqueued_at
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.