dafiti-rabbit-hutch 0.1.8 → 0.1.9

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 4988e066462827b4bec2923fe80efa7019b497e769658a61d85b7c0876ece728
4
- data.tar.gz: d19fd550bf7f0f5b4cea3b029fc9ab98ed79cedb99a7fc1db60db7a344818e08
3
+ metadata.gz: 2785455395752f9d4b464ea0e22afb47c5d68d93f58467dadd6e372ab7d7107c
4
+ data.tar.gz: 1b4ea9a77762aaa8a36791336f30f3808da91da8f8945d1b8f347f6a0e0fb00d
5
5
  SHA512:
6
- metadata.gz: 2a67d149dd23f58060b4c395bb5fabca1c93959f5925600dfe8ee8c91eed0016e20b22bbd7c54b0a912a88658c9402c5fe607a3b5f78a429c88fc8dd367d4c42
7
- data.tar.gz: 3dfd2f2afab7013771fd5109e64dfd01df4b1fd25fd5d2feb1356a12e55a0ee89c1991a0e2b939e128320b2274729346528ba118dbb72da456b5502aaa2420f6
6
+ metadata.gz: 580df330bb83f5ea847357a32d519ac8c136bf3f1373ce269dee9727486af1ce99f12365daae04e6c08706c3b68d9c7482491c6eadad7cb7d5fc3bdaf4d9dd2d
7
+ data.tar.gz: 42f59b65f800822e67eb09d06306c97c2dcf80555bb0bc50d0b8e33b86f888bc257bf3155568b38955cfb15c30363c0273f05d923c16f292f9ab5a201df5f047
@@ -0,0 +1,38 @@
1
+ require 'yaml'
2
+
3
+ module RabbitHutch
4
+ class Configurator
5
+
6
+ attr_accessor :config
7
+
8
+ def initialize options
9
+
10
+ file = options['config'] || (File.dirname(__FILE__) + '/../config.yaml')
11
+
12
+ puts "Using config from #{file}"
13
+
14
+ unless File.exists? file
15
+ raise "Configuration file [#{file}] doesn't exist"
16
+ end
17
+ @config = YAML::load(File.open(file))
18
+ end
19
+
20
+ def application
21
+ @config['application']
22
+ end
23
+
24
+ def log_config
25
+ @config['log4r_config']
26
+ end
27
+
28
+ def consumers
29
+ @config['consumers_config']["consumers"]
30
+ end
31
+
32
+ def rabbitmq_hosts
33
+ @config['rabbitmq']['hosts']
34
+ end
35
+
36
+ end
37
+ end
38
+
data/lib/consumer.rb ADDED
@@ -0,0 +1,32 @@
1
+ require "rubygems"
2
+ require_relative "configurator"
3
+ require_relative "consumers/mongo_consumer"
4
+
5
+ module RabbitHutch
6
+ # Controls the consumers that are to be kicked off based on the config file
7
+ class Consumer
8
+ def initialize(consumers)
9
+ @consumers = consumers
10
+ end
11
+
12
+ # Raised on receipt of a message from RabbitMq and uses the appropriate appender
13
+ def handle_message(metadata, payload)
14
+ # loop through appenders and fire each as required
15
+ @consumers.each do |consumer|
16
+ action = metadata.routing_key.split('.', 2).first
17
+ if(action == "publish")
18
+ exchange = metadata.attributes[:headers]["exchange_name"]
19
+ queue = metadata.routing_key.split('.', 2).last
20
+ item = {:date => Time.now,
21
+ :exchange => exchange,
22
+ :queue => queue,
23
+ :routing_keys => metadata.attributes[:headers]["routing_keys"].inspect,
24
+ :attributes => metadata.attributes.inspect,
25
+ :payload => payload.inspect
26
+ }
27
+ consumer.log_event(item)
28
+ end
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,21 @@
1
+ # encoding: utf-8
2
+ require 'rubygems'
3
+ require "mongo"
4
+
5
+ module RabbitHutch
6
+ class ConsoleConsumer
7
+
8
+ def initialize()
9
+ puts "\tInitializing Console Consumer"
10
+ end
11
+
12
+ def log_event(item)
13
+ begin
14
+ puts "console"
15
+ puts item
16
+ rescue Exception => e
17
+ puts "Error occurred Message Handler trying to write messages to Log #{e.inspect}"
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,36 @@
1
+ # encoding: utf-8
2
+ require 'rubygems'
3
+ require 'log4r'
4
+ #require 'log4r/outputter/syslogoutputter'
5
+
6
+ module RabbitHutch
7
+ class Log4rConsumer
8
+
9
+ def initialize(rabbitmq_host, config)
10
+ puts "\tInitializing Log4r Consumer"
11
+ @rabbitmq_host = rabbitmq_host
12
+ @config = config
13
+ @log_name = rabbitmq_host["displayname"]
14
+ @config.consumers.each do |consumer|
15
+ if consumer["name"] == 'log4r_consumer'
16
+ @log_prefix = consumer['log_prefix']
17
+ @log_location = consumer['log_location']
18
+ end
19
+ end
20
+
21
+
22
+ @logger = Log4r::Logger.new("#{@log_name}#_log")
23
+ @logger.outputters << Log4r::FileOutputter.new("#{@log_name}_filelog", :filename => "#{@log_location}/#{@log_prefix}#{@log_name}.log")
24
+ end
25
+
26
+ def log_event(item)
27
+ begin
28
+ puts "log_consumer"
29
+ @logger.info(item)
30
+ rescue Exception => e
31
+ puts "Error occurred Message Handler trying to write messages to Log #{e.inspect}"
32
+ #@log.error("Error occurred Message Handler trying to write messages to MONGODB #{e.inspect}")
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,35 @@
1
+ # encoding: utf-8
2
+ require 'rubygems'
3
+ require "mongo"
4
+
5
+ module RabbitHutch
6
+ class MongoConsumer
7
+
8
+ def initialize(rabbitmq_host, config)
9
+ puts "\tInitializing MongoDb Consumer"
10
+ @config = config
11
+ @rabbitmq_host = rabbitmq_host
12
+
13
+ @config.consumers.each do |consumer|
14
+ if consumer["name"] == 'mongo_consumer'
15
+ @host = consumer['hostname']
16
+ @port = consumer["port"]
17
+ @database_prefix = consumer['database_prefix']
18
+ @database = "#{@database_prefix}#{rabbitmq_host["displayname"]}"
19
+ end
20
+ end
21
+ @connection = Mongo::Connection.new(@host, @port)
22
+ end
23
+
24
+ def log_event(item)
25
+ begin
26
+ puts "mongo"
27
+ db = @connection.db(@database)
28
+ coll = db.collection(item[:exchange])
29
+ coll.insert(item)
30
+ rescue Exception => e
31
+ puts "Error occurred Message Handler trying to write messages to MONGODB #{e.inspect}"
32
+ end
33
+ end
34
+ end
35
+ end
data/lib/logger.rb ADDED
@@ -0,0 +1,16 @@
1
+ require 'log4r'
2
+ require 'log4r/yamlconfigurator'
3
+ require 'log4r/outputter/syslogoutputter'
4
+
5
+ class Logger
6
+ @@log = nil
7
+ def self.init(config)
8
+ if !@@log.nil?
9
+ return @@log
10
+ end
11
+
12
+ configurator = Log4r::YamlConfigurator
13
+ configurator.decode_yaml config.log_config
14
+ @@log = Log4r::Logger['main']
15
+ end
16
+ end
@@ -0,0 +1,78 @@
1
+ require 'rubygems'
2
+ require "amqp"
3
+ require_relative "configurator"
4
+ require_relative "consumers/mongo_consumer"
5
+ require_relative "consumers/log4r_consumer"
6
+ require_relative "consumers/console_consumer"
7
+ require_relative "worker"
8
+
9
+ @config = RabbitHutch::Configurator.new({})
10
+
11
+ puts "\tEnvironment Settings"
12
+ @config.rabbitmq_hosts.each do |rabbitmq_host|
13
+ puts "\tDisplay Name: #{rabbitmq_host["displayname"]}, host: #{rabbitmq_host["hostname"]}, username: #{rabbitmq_host["username"]}, enabled #{rabbitmq_host["enabled"]}"
14
+ end
15
+
16
+ # Initialize all enabled consumners
17
+ # NOTE: this method will be replaced with a routine to reflect through all valid consumers and initialze them implicitly
18
+ def initialize_consumers(rabbitmq_host)
19
+ puts "Initializing Consumers for #{rabbitmq_host["displayname"]}"
20
+ consumers = []
21
+ @config.consumers.each do |consumer|
22
+ # if consumer["enabled"] == true
23
+ case consumer["name"]
24
+ when "console_consumer"
25
+ consumers << RabbitHutch::ConsoleConsumer.new()
26
+ when "mongo_consumer"
27
+ consumers << RabbitHutch::MongoConsumer.new(rabbitmq_host, @config)
28
+ when "log4r_consumer"
29
+ consumers << RabbitHutch::Log4rConsumer.new(rabbitmq_host, @config)
30
+ end
31
+ end
32
+ #end
33
+ puts "\tdone"
34
+ consumers
35
+ end
36
+
37
+ # Start the worker process to listen to a RabbitMq Node
38
+ def start_worker(rabbitmq_host)
39
+ displayname = rabbitmq_host["displayname"]
40
+ hostname = rabbitmq_host["hostname"]
41
+ username = rabbitmq_host["username"]
42
+ password = rabbitmq_host["password"]
43
+
44
+ consumers = initialize_consumers(rabbitmq_host)
45
+
46
+ puts "Listening to RabbitMq #{displayname}, host: #{hostname}, username #{username}"
47
+ AMQP.connect(:host => hostname, :user => username, :password => password) do |connection|
48
+ channel = AMQP::Channel.new(connection)
49
+ worker = RabbitHutch::Worker.new(channel, @config, consumers)
50
+ worker.start
51
+ end
52
+ end
53
+
54
+ # Entry Point to the application, Begins queue listener and initializes all consmers
55
+ def start
56
+ begin
57
+ EventMachine.run do
58
+
59
+ @config.rabbitmq_hosts.each do |rabbitmq_host|
60
+ if rabbitmq_host["enabled"] == true
61
+ start_worker(rabbitmq_host)
62
+ end
63
+ end
64
+ Signal.trap("INT"){EventMachine.stop}
65
+ Signal.trap("QUIT"){EventMachine.stop}
66
+ Signal.trap("TERM"){EventMachine.stop}
67
+ Signal.trap("TSTP"){EventMachine.stop}
68
+ end
69
+ rescue Exception=>e
70
+ puts "#{e.message} #{e.backtrace}"
71
+ end
72
+ end
73
+
74
+ # Kick off the App
75
+ start
76
+
77
+
78
+
@@ -0,0 +1,48 @@
1
+ require 'thor'
2
+ require 'mustache'
3
+ require_relative 'configurator.rb'
4
+
5
+ module RabbitHutch
6
+ #This class controls the Command Line Interface
7
+ class CLI < Thor
8
+
9
+ StandardTemplate =<<-TEMPLATE
10
+ {{#config}}
11
+
12
+ {{#application}}
13
+ Application
14
+ +------------------------+------------------------+
15
+ |Exchange |Queue |
16
+ +------------------------+------------------------+
17
+ |{{exchangename}} | {{queuename}} |
18
+ +------------------------+------------------------+
19
+ {{/application}}
20
+
21
+ {{#rabbitmq}}
22
+ RabbitMq Servers
23
+ +------------------------+------------------------+
24
+ |Display Name |Host |
25
+ +------------------------+------------------------+
26
+ {{#hosts}}
27
+ |{{displayname}} | {{hostname}} |
28
+ +------------------------+------------------------+
29
+ {{/hosts}}
30
+ {{/rabbitmq}}
31
+
32
+ {{/config}}
33
+ TEMPLATE
34
+
35
+ desc "list", "List all config settings"
36
+ #method_option :errors, :aliases => "-e", :type => :boolean, :default => false, :desc => "true = display files that could not be processed, false = do not display skipped files"
37
+ #method_option :hashtype, :aliases => "-h", :default => "cmd5", :desc => "Choose the hash algorithm to use - md5 or sha"
38
+ method_option :config, :aliases => "-c", :default => nil, :desc => "parse the config file from a specified location "
39
+ #method_option :recursive, :aliases => "-r", :type => :boolean, :default => "false", :desc => "true = recurse through sub directories, false = only do top directory"
40
+ def list()
41
+ @config = RabbitHutch::Configurator.new(options)
42
+ puts Mustache.render(StandardTemplate, :config => @config);
43
+ end
44
+
45
+ end
46
+
47
+ CLI.start()
48
+ end
@@ -0,0 +1,18 @@
1
+ require 'rubygems'
2
+ require 'daemons'
3
+
4
+ # The Service controller.
5
+ def start_service
6
+ begin
7
+ puts "-------------------------"
8
+ puts "Starting RabbitHutch #1"
9
+ Daemons.run(File.dirname(__FILE__) + '/rabbithutch.rb')
10
+ rescue SystemExit=>e
11
+ puts e.inspect
12
+ rescue Exception=>e
13
+ puts e.inspect
14
+ end
15
+ end
16
+
17
+ start_service()
18
+
data/lib/worker.rb ADDED
@@ -0,0 +1,33 @@
1
+ require "rubygems"
2
+ require "amqp"
3
+ require_relative "configurator"
4
+ require_relative "consumer"
5
+
6
+ module RabbitHutch
7
+ @exchange_name = "amq.rabbitmq.trace"
8
+
9
+ class Worker
10
+
11
+ def initialize(channel, config, consumers)
12
+ @channel = channel
13
+ @channel.on_error(&method(:handle_channel_exception))
14
+ @consumer = Consumer.new(consumers)
15
+ @exchange_name = config.application['exchangename']
16
+ @queue_name = config.application['queuename']
17
+ end
18
+
19
+ # begin listening for all topics in publish.#
20
+ def start
21
+ @exchange = @channel.topic(@exchange_name, :durable => true, :auto_delete => false, :internal => true)
22
+ @queue = @channel.queue(@queue_name, :durable => true, :auto_delete => false)
23
+ @queue.bind(@exchange, :routing_key => 'publish.#')
24
+ @queue.subscribe(&@consumer.method(:handle_message))
25
+ end
26
+
27
+ def handle_channel_exception(channel, channel_close)
28
+ puts "Oops... a channel-level exception: code = #{channel_close.reply_code}, message = #{channel_close.reply_text}"
29
+ end
30
+
31
+ end
32
+
33
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dafiti-rabbit-hutch
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.8
4
+ version: 0.1.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - Luiz Bartolomeu
@@ -249,6 +249,16 @@ files:
249
249
  - bin/rabbithutch
250
250
  - bin/rabbithutchweb
251
251
  - config.yaml
252
+ - lib/configurator.rb
253
+ - lib/consumer.rb
254
+ - lib/consumers/console_consumer.rb
255
+ - lib/consumers/log4r_consumer.rb
256
+ - lib/consumers/mongo_consumer.rb
257
+ - lib/logger.rb
258
+ - lib/rabbithutch.rb
259
+ - lib/rabbithutchmgr.rb
260
+ - lib/rabbithutchservice.rb
261
+ - lib/worker.rb
252
262
  - web/public/assets/css/bootstrap-responsive.css
253
263
  - web/public/assets/css/bootstrap-responsive.min.css
254
264
  - web/public/assets/css/bootstrap.css