active_pubsub 0.0.7 → 0.0.8
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +10 -0
- data/bin/subscriber +39 -8
- data/lib/active_pubsub/config.rb +1 -0
- data/lib/active_pubsub/connection.rb +0 -1
- data/lib/active_pubsub/logging.rb +41 -0
- data/lib/active_pubsub/publishable.rb +6 -0
- data/lib/active_pubsub/publisher.rb +2 -0
- data/lib/active_pubsub/railtie.rb +1 -1
- data/lib/active_pubsub/subscriber.rb +10 -4
- data/lib/active_pubsub/version.rb +1 -1
- data/lib/active_pubsub.rb +13 -6
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 732df7c0baced02169548ca0e1b68fe64672bf98
|
4
|
+
data.tar.gz: dc0e9439ab43e368ebef496cb24a0686e1d6d7e1
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1fa9d7aabee001f5a1423d590277d0a6de796d3220f1ffa8c884699343e02689a4fc17fb2b8ef7389446fe7f244aa4566dc306fd22cde720c537b5a94dcdb2e5
|
7
|
+
data.tar.gz: f5068dc1ce77133ca6bbb2574d942be00a1c683c7b967da06465db2e1ecc44a1cedc4d2c78d1797c5762a9e1651ec8196e679ef3e865798c8d094657c1963fb6
|
data/README.md
CHANGED
@@ -5,6 +5,16 @@ Service oriented observers for active record, via RabbitMQ and Bunny gem. Publis
|
|
5
5
|
Best examples can be found here:
|
6
6
|
https://github.com/jasonayre/active_pubsub_examples
|
7
7
|
|
8
|
+
### Quick important development/spring bug note ###
|
9
|
+
If you are having issues with either publisher or subscribers hanging in development,
|
10
|
+
kill spring. Kill it twice actually. Make sure its dead. Then restart with env var
|
11
|
+
|
12
|
+
```
|
13
|
+
DISABLE_SPRING=1 bx subscriber start
|
14
|
+
```
|
15
|
+
|
16
|
+
And when running server or console make sure to DISABLE_SPRING=1 as well. (dont know how to treat the problem yet just diagnose the symptom which seems to be spring)
|
17
|
+
|
8
18
|
## Publisher Example
|
9
19
|
``` ruby
|
10
20
|
class Post < ::ActiveRecord::Base
|
data/bin/subscriber
CHANGED
@@ -5,22 +5,53 @@ require 'active_pubsub'
|
|
5
5
|
require './config/environment.rb'
|
6
6
|
|
7
7
|
class Subscriber < ::Thor
|
8
|
+
option :address, :type => :string, :default => ENV['RABBITMQ_URL'], :aliases => %w(-a), :desc => "RabbitMQ url. Default is #{ENV['RABBITMQ_URL']}"
|
9
|
+
option :log, :type => :string, :default => ::STDOUT, :aliases => %w(-l), :desc => 'Log file or device. Default is STDOUT.'
|
10
|
+
option :level, :type => :numeric, :default => ::Logger::INFO, :aliases => %w(-v), :desc => 'Log level to use, 0-5 (see http://www.ruby-doc.org/stdlib/libdoc/logger/rdoc/)'
|
11
|
+
|
8
12
|
desc "start", "Start ActivePubsub Subscriber"
|
9
13
|
def start
|
10
|
-
|
14
|
+
merge_config_options!(options)
|
15
|
+
configure_logger
|
16
|
+
configure_traps
|
17
|
+
start_subscribers
|
18
|
+
end
|
19
|
+
|
20
|
+
no_tasks do
|
21
|
+
def configure_logger
|
22
|
+
log_level = options.debug? ? ::Logger::DEBUG : options.level
|
23
|
+
::ActivePubsub::Logging.initialize_logger(options.log, log_level)
|
24
|
+
|
25
|
+
::ActivePubsub.config.logger
|
26
|
+
end
|
11
27
|
|
12
|
-
|
28
|
+
# Configure signal traps.
|
29
|
+
def configure_traps
|
30
|
+
exit_signals = [:INT, :TERM]
|
31
|
+
exit_signals << :QUIT unless defined?(JRUBY_VERSION)
|
13
32
|
|
14
|
-
|
33
|
+
exit_signals.each do |signal|
|
34
|
+
trap(signal) do
|
35
|
+
puts "Stopping Subscribers"
|
36
|
+
exit(0)
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
15
40
|
|
16
|
-
|
41
|
+
def merge_config_options!(options)
|
42
|
+
::ActivePubsub.config.merge!(options)
|
43
|
+
end
|
44
|
+
|
45
|
+
def start_subscribers
|
46
|
+
::ActivePubsub.logger.info "Starting Subscribers"
|
47
|
+
|
48
|
+
::ActivePubsub.load_subscribers
|
49
|
+
|
50
|
+
::ActivePubsub.start_subscribers
|
51
|
+
end
|
17
52
|
end
|
18
53
|
end
|
19
54
|
|
20
|
-
#todo: write event loop that handles shutdown gracefully
|
21
|
-
#however, it seems that Bunny knows when to wake subscribers up
|
22
|
-
#so sleeping seems to be enough for now
|
23
|
-
|
24
55
|
::Subscriber.start
|
25
56
|
|
26
57
|
sleep
|
data/lib/active_pubsub/config.rb
CHANGED
@@ -0,0 +1,41 @@
|
|
1
|
+
require 'logger'
|
2
|
+
|
3
|
+
module ActivePubsub
|
4
|
+
module Logging
|
5
|
+
def self.initialize_logger(log_target=$stdout, log_level=::Logger::INFO)
|
6
|
+
@counter ||= 0
|
7
|
+
@counter = @counter + 1
|
8
|
+
@logger = ::Logger.new(log_target)
|
9
|
+
@logger.level = log_level
|
10
|
+
@logger
|
11
|
+
end
|
12
|
+
|
13
|
+
def self.logger
|
14
|
+
defined?(@logger) ? @logger : initialize_logger
|
15
|
+
end
|
16
|
+
|
17
|
+
def self.logger=(new_logger)
|
18
|
+
@logger = new_logger
|
19
|
+
end
|
20
|
+
|
21
|
+
def logger
|
22
|
+
::ActivePubsub::Logging.logger
|
23
|
+
end
|
24
|
+
|
25
|
+
def log_exception(ex)
|
26
|
+
logger.error { ex.message }
|
27
|
+
logger.error { ex.backtrace[0..5].join("\n") }
|
28
|
+
logger.debug { ex.backtrace.join("\n") }
|
29
|
+
end
|
30
|
+
|
31
|
+
def log_signature
|
32
|
+
@_log_signature ||= "[#{self.class == Class ? self.name : self.class.name}]"
|
33
|
+
end
|
34
|
+
|
35
|
+
def sign_message(message)
|
36
|
+
"#{log_signature} #{message}"
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
# Inspired by [protobuf](https://github.com/localshred/protobuf)
|
@@ -31,18 +31,24 @@ module ActivePubsub
|
|
31
31
|
record_updated_event = ::ActivePubsub::Event.new(self.class.exchange_key, "updated", serialized_resource)
|
32
32
|
|
33
33
|
::ActivePubsub.publish_event(record_updated_event)
|
34
|
+
|
35
|
+
::ActivePubsub.logger.info(record_updated_event)
|
34
36
|
end
|
35
37
|
|
36
38
|
def publish_created_event
|
37
39
|
record_created_event = ::ActivePubsub::Event.new(self.class.exchange_key, "created", serialized_resource)
|
38
40
|
|
39
41
|
::ActivePubsub.publish_event(record_created_event)
|
42
|
+
|
43
|
+
::ActivePubsub.logger.info(record_created_event)
|
40
44
|
end
|
41
45
|
|
42
46
|
def publish_destroyed_event
|
43
47
|
record_destroyed_event = ::ActivePubsub::Event.new(self.class.exchange_key, "destroyed", serialized_resource)
|
44
48
|
|
45
49
|
::ActivePubsub.publish_event(record_destroyed_event)
|
50
|
+
|
51
|
+
::ActivePubsub.logger.info(record_destroyed_event)
|
46
52
|
end
|
47
53
|
|
48
54
|
def serialized_resource
|
@@ -52,6 +52,8 @@ module ActivePubsub
|
|
52
52
|
|
53
53
|
def publish_event(event)
|
54
54
|
::ActiveRecord::Base.connection_pool.with_connection do
|
55
|
+
::ActivePubsub.logger.info("Publishing event: #{event.id} to #{event.routing_key}")
|
56
|
+
|
55
57
|
exchanges[event.exchange].publish(serialize_event(event), :routing_key => event.routing_key)
|
56
58
|
end
|
57
59
|
end
|
@@ -5,7 +5,7 @@ module ActivePubsub
|
|
5
5
|
# we only need publisher started if service has a publishable model
|
6
6
|
::ActiveSupport.on_load(:active_record) do
|
7
7
|
if(::ActivePubsub::Publisher.publishable_model_count > 0) && !::ActivePubsub::Publisher.started?
|
8
|
-
|
8
|
+
::ActivePubsub.logger.info("Starting Publisher")
|
9
9
|
::ActivePubsub::Publisher.start
|
10
10
|
end
|
11
11
|
end
|
@@ -18,6 +18,11 @@ module ActivePubsub
|
|
18
18
|
self.local_service_namespace = service_namespace
|
19
19
|
end
|
20
20
|
|
21
|
+
def self.clear_connections!
|
22
|
+
channel.close
|
23
|
+
connection.close
|
24
|
+
end
|
25
|
+
|
21
26
|
def self.channel
|
22
27
|
connection.channel
|
23
28
|
end
|
@@ -46,6 +51,8 @@ module ActivePubsub
|
|
46
51
|
|
47
52
|
subscriber_instance = new(deserialized_record)
|
48
53
|
subscriber_instance.instance_exec(deserialized_record, &block)
|
54
|
+
|
55
|
+
::ActivePubsub.logger.info "#{delivery_info[:routing_key]} #{name} consumed #{deserialized_event}"
|
49
56
|
end
|
50
57
|
end
|
51
58
|
|
@@ -53,11 +60,11 @@ module ActivePubsub
|
|
53
60
|
end
|
54
61
|
|
55
62
|
def self.deserialize_event(event)
|
56
|
-
|
63
|
+
::Marshal.load(event)
|
57
64
|
end
|
58
65
|
|
59
66
|
def self.deserialize_record(record)
|
60
|
-
|
67
|
+
::Marshal.load(record)
|
61
68
|
end
|
62
69
|
|
63
70
|
def self.observes(target_exchange)
|
@@ -80,7 +87,7 @@ module ActivePubsub
|
|
80
87
|
"\n"
|
81
88
|
end
|
82
89
|
|
83
|
-
|
90
|
+
::ActivePubsub.logger.info(message)
|
84
91
|
end
|
85
92
|
|
86
93
|
def self.started?
|
@@ -93,6 +100,5 @@ module ActivePubsub
|
|
93
100
|
def initialize(record)
|
94
101
|
@record = record
|
95
102
|
end
|
96
|
-
|
97
103
|
end
|
98
104
|
end
|
data/lib/active_pubsub.rb
CHANGED
@@ -7,6 +7,8 @@ require "celluloid"
|
|
7
7
|
require "active_support/all"
|
8
8
|
require "active_attr"
|
9
9
|
require "pry"
|
10
|
+
require "active_pubsub/config"
|
11
|
+
require "active_pubsub/logging"
|
10
12
|
|
11
13
|
module ActivePubsub
|
12
14
|
class << self
|
@@ -16,9 +18,9 @@ module ActivePubsub
|
|
16
18
|
delegate :publish_event, :to => :publisher
|
17
19
|
end
|
18
20
|
|
19
|
-
|
20
|
-
self.configuration ||= ::ActivePubsub::Config.new
|
21
|
+
@configuration ||= ::ActivePubsub::Config.new
|
21
22
|
|
23
|
+
def self.configure
|
22
24
|
yield(configuration)
|
23
25
|
|
24
26
|
::ActiveSupport.run_load_hooks(:active_pubsub, self)
|
@@ -28,6 +30,14 @@ module ActivePubsub
|
|
28
30
|
::Dir.glob(::Rails.root.join('app', 'subscribers', "*.rb")).each{ |file| load file }
|
29
31
|
end
|
30
32
|
|
33
|
+
def self.logger
|
34
|
+
configuration.logger
|
35
|
+
end
|
36
|
+
|
37
|
+
def self.logger?
|
38
|
+
configuration.logger.present?
|
39
|
+
end
|
40
|
+
|
31
41
|
def self.publisher
|
32
42
|
::Celluloid::Actor[:rabbit_publisher]
|
33
43
|
end
|
@@ -37,11 +47,10 @@ module ActivePubsub
|
|
37
47
|
end
|
38
48
|
|
39
49
|
def self.start_subscribers
|
40
|
-
|
41
50
|
::ActivePubsub::Subscriber.subclasses.each do |subscriber|
|
42
51
|
next if subscriber.started?
|
43
52
|
|
44
|
-
|
53
|
+
::ActivePubsub.logger.info("Starting #{subscriber.name}")
|
45
54
|
|
46
55
|
subscriber.bind_subscriptions!
|
47
56
|
subscriber.print_subscriptions!
|
@@ -51,11 +60,9 @@ module ActivePubsub
|
|
51
60
|
def self.symbolize_routing_key(routing_key)
|
52
61
|
:"#{routing_key.split('.').join('_')}"
|
53
62
|
end
|
54
|
-
|
55
63
|
end
|
56
64
|
|
57
65
|
require "active_pubsub/connection"
|
58
|
-
require "active_pubsub/config"
|
59
66
|
require "active_pubsub/event"
|
60
67
|
require "active_pubsub/publisher"
|
61
68
|
require "active_pubsub/publishable"
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: active_pubsub
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.8
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jason Ayre
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-10-
|
11
|
+
date: 2014-10-27 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: active_attr
|
@@ -314,6 +314,7 @@ files:
|
|
314
314
|
- lib/active_pubsub/config.rb
|
315
315
|
- lib/active_pubsub/connection.rb
|
316
316
|
- lib/active_pubsub/event.rb
|
317
|
+
- lib/active_pubsub/logging.rb
|
317
318
|
- lib/active_pubsub/publish_with_serializer.rb
|
318
319
|
- lib/active_pubsub/publishable.rb
|
319
320
|
- lib/active_pubsub/publisher.rb
|