rabbithutch 0.1.2 → 0.1.4

Sign up to get free protection for your applications and to get access to all the features.
data/lib/configurator.rb CHANGED
@@ -1,38 +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 = ARGV[0] || (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
-
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 CHANGED
@@ -1,32 +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
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
32
  end
@@ -1,20 +1,20 @@
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 item
15
- rescue Exception => e
16
- puts "Error occurred Message Handler trying to write messages to Log #{e.inspect}"
17
- end
18
- end
19
- end
20
- end
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 item
15
+ rescue Exception => e
16
+ puts "Error occurred Message Handler trying to write messages to Log #{e.inspect}"
17
+ end
18
+ end
19
+ end
20
+ end
@@ -1,35 +1,35 @@
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
- @logger.info(item)
29
- rescue Exception => e
30
- puts "Error occurred Message Handler trying to write messages to Log #{e.inspect}"
31
- #@log.error("Error occurred Message Handler trying to write messages to MONGODB #{e.inspect}")
32
- end
33
- end
34
- end
35
- end
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
+ @logger.info(item)
29
+ rescue Exception => e
30
+ puts "Error occurred Message Handler trying to write messages to Log #{e.inspect}"
31
+ #@log.error("Error occurred Message Handler trying to write messages to MONGODB #{e.inspect}")
32
+ end
33
+ end
34
+ end
35
+ end
@@ -1,34 +1,34 @@
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
- db = @connection.db(@database)
27
- coll = db.collection(item[:exchange])
28
- coll.insert(item)
29
- rescue Exception => e
30
- puts "Error occurred Message Handler trying to write messages to MONGODB #{e.inspect}"
31
- end
32
- end
33
- end
34
- end
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
+ db = @connection.db(@database)
27
+ coll = db.collection(item[:exchange])
28
+ coll.insert(item)
29
+ rescue Exception => e
30
+ puts "Error occurred Message Handler trying to write messages to MONGODB #{e.inspect}"
31
+ end
32
+ end
33
+ end
34
+ end
data/lib/logger.rb CHANGED
@@ -1,16 +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
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
data/lib/rabbithutch.rb CHANGED
@@ -1,78 +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
-
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
+