Class: Monitor::Worker

Inherits:
Object
  • Object
show all
Defined in:
lib/chef_monitor/worker.rb

Instance Method Summary collapse

Instance Method Details

#runObject



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
# File 'lib/chef_monitor/worker.rb', line 21

def run
  conn = Bunny.new(:hostname => MQSERVER)
  conn.start

  ch = conn.create_channel
  q  = ch.queue(MQQUEUE, :durable => true)
  ch.prefetch(1)

  begin
    q.subscribe(:ack => true, :block => true) do |delivery_info, properties, body|
      if properties[:content_type] =~ /.*json/
        data = JSON.parse(body).to_hash
        Monitor::Log.new("Receiving : #{data}", "DEBUG") 
        data['object'] = nil unless ALLOWED_OBJECTS.include?(data['object'])

        unless data['object'].nil? || !File.directory?((File.join(DOWNLOAD_PATH, data['org'])))
          Chef::Config[:chef_server_url] = CHEF_URL + "/organizations/#{data['org']}"

          obj = Monitor::Item.new(data)
          if data['action'] == "DELETE"
            obj.delete(DOWNLOAD_PATH)
          else
            items = Monitor::ItemList.new(data)              
            items.each do |item|
              if data['action'] == "PUT"
                item.download(DOWNLOAD_PATH)
              end
           
              if data['action'] == "POST"
                item.delete(DOWNLOAD_PATH)
                item.download(DOWNLOAD_PATH)
              end
            end
          end
          obj.commit(DOWNLOAD_PATH)
        else
          Monitor::Log.new("Ignoring  : #{data}", "DEBUG")
        end
      else
        Monitor::Log.new("Unknown   : #{body}", "ERROR")
      end
    ch.ack(delivery_info.delivery_tag)
    end
  rescue Interrupt => _
    conn.close
  end
end