summaryrefslogtreecommitdiff
path: root/chef/lib/chef/index_queue/indexable.rb
blob: 0a06a7aa358b01a245eb88d46d0b99d4ec57019e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#
# Author:: Daniel DeLeo (<dan@kallistec.com>)
# Author:: Seth Falcon (<seth@opscode.com>)
# Copyright:: Copyright (c) 2009 Daniel DeLeo
# Copyright:: Copyright (c) 2010 Opscode, Inc.
# License:: Apache License, Version 2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# 
#     http://www.apache.org/licenses/LICENSE-2.0
# 
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
require 'chef/json_compat'

class Chef
  module IndexQueue
    module Indexable
      
      module ClassMethods
        
        def index_object_type(explicit_type_name=nil)
          @index_object_type = explicit_type_name.to_s if explicit_type_name
          @index_object_type
        end
        
        # Resets all metadata used for indexing to nil. Used for testing
        def reset_index_metadata!
          @index_object_type = nil
        end
        
      end

      def self.included(including_class)
        including_class.send(:extend, ClassMethods)
      end
      
      attr_accessor :index_id
      
      def index_object_type
        self.class.index_object_type || Mixin::ConvertToClassName.snake_case_basename(self.class.name)
      end
      
      def with_indexer_metadata(indexer_metadata={})
        # changing input param symbol keys to strings, as the keys in hash that goes to solr are expected to be strings [cb]
        # Ruby 1.9 hates you, cb [dan]
        with_metadata = {}
        indexer_metadata.each_key do |key|
          with_metadata[key.to_s] = indexer_metadata[key]
        end

        with_metadata["type"]     ||= self.index_object_type
        with_metadata["id"]       ||= self.index_id
        with_metadata["database"] ||= Chef::Config[:couchdb_database]
        with_metadata["item"]     ||= self.to_hash
        with_metadata["enqueued_at"] ||= Time.now.utc.to_i

        raise ArgumentError, "Type, Id, or Database missing in index operation: #{with_metadata.inspect}" if (with_metadata["id"].nil? or with_metadata["type"].nil?)
        with_metadata        
      end

      def add_to_index(metadata={})
       Chef::Log.debug("Pushing item to index queue for addition: #{self.with_indexer_metadata(metadata)}")
       object_with_metadata = with_indexer_metadata(metadata)
       obj_id = object_with_metadata["id"]
       obj = {:action => :add, :payload => self.with_indexer_metadata(metadata)}

       publish_object(obj_id, obj)
      end

      def delete_from_index(metadata={})
        Chef::Log.debug("Pushing item to index queue for deletion: #{self.with_indexer_metadata(metadata)}")
        object_with_metadata = with_indexer_metadata(metadata)
        obj_id = object_with_metadata["id"]
        obj = {:action => :delete, :payload => self.with_indexer_metadata(metadata)}

        publish_object(obj_id, obj)
      end

      private

      # Uses the publisher to update the object's queue. If
      # Chef::Config[:persistent_queue] is true, the update is wrapped
      # in a transaction.
      def publish_object(object_id, object)
        publisher = AmqpClient.instance
        retries = 0
        begin
          publisher.amqp_client.tx_select if Chef::Config[:persistent_queue]
          publisher.queue_for_object(object_id) do |queue|
            queue.publish(Chef::JSONCompat.to_json(object), :persistent => Chef::Config[:persistent_queue])
          end
          publisher.amqp_client.tx_commit if Chef::Config[:persistent_queue]
        rescue Bunny::ServerDownError, Bunny::ConnectionError, Errno::ECONNRESET
          publisher.disconnected!
          if (retries += 1) < 2
            Chef::Log.info("Attempting to reconnect to the AMQP broker")
            retry
          else
            Chef::Log.fatal("Could not re-connect to the AMQP broker, giving up")
            raise
          end
        rescue
          publisher.amqp_client.tx_rollback if Chef::Config[:persistent_queue]
          raise
        end

        true
      end

    end
  end
end