diff --git a/lib/protobuf.rb b/lib/protobuf.rb index a0c53441..8d6081df 100644 --- a/lib/protobuf.rb +++ b/lib/protobuf.rb @@ -4,6 +4,7 @@ require 'stringio' require 'active_support/core_ext/object/blank' require 'active_support/version' +require 'active_support/notifications' if ActiveSupport::VERSION::MAJOR > 2 require 'active_support/core_ext/object/try' diff --git a/lib/protobuf/cli.rb b/lib/protobuf/cli.rb index b973e434..3af746d8 100644 --- a/lib/protobuf/cli.rb +++ b/lib/protobuf/cli.rb @@ -1,6 +1,5 @@ require 'thor' require 'protobuf/version' -require 'protobuf/lifecycle' require 'protobuf/logger' require 'protobuf/rpc/servers/evented_runner' require 'protobuf/rpc/servers/socket_runner' @@ -244,7 +243,7 @@ def start_server "pid #{::Process.pid} -- #{@runner_mode} RPC Server listening at #{options.host}:#{options.port}" } - ::Protobuf::Lifecycle.trigger( "after_server_bind" ) + ::ActiveSupport::Notifications.instrument("after_server_bind") end end end diff --git a/lib/protobuf/lifecycle.rb b/lib/protobuf/lifecycle.rb index e0082307..5f5afd3c 100644 --- a/lib/protobuf/lifecycle.rb +++ b/lib/protobuf/lifecycle.rb @@ -1,26 +1,35 @@ module Protobuf class Lifecycle + include ::Protobuf::Logger::LogMethods def self.register( event_name, &blk ) raise "Lifecycle register must have a block" unless block_given? + event_name = normalized_event_name( event_name ) - lifecycle_events[ event_name ] ||= [] - lifecycle_events[ event_name ] << blk + if ::Protobuf.print_deprecation_warnings? + $stderr.puts <<-ERROR + [DEPRECATED] ::Protobuf::Lifecycle has been deprecated and will be removed in a future version. + Use ::ActiveSupport::Notifications.subscribe('#{event_name}') + ERROR + end + + ::ActiveSupport::Notifications.subscribe(event_name) do |name, start, finish, id, args| + blk.call(*args) + end end def self.trigger( event_name, *args ) + if ::Protobuf.print_deprecation_warnings? + $stderr.puts <<-ERROR + [DEPRECATED] ::Protobuf::Lifecycle has been deprecated and will be removed in a future version. + Use ::ActiveSupport::Notifications.instrument(...) + ERROR + end + event_name = normalized_event_name( event_name ) - if lifecycle_events.has_key?( event_name ) - lifecycle_events[ event_name ].each do |block| - if ! args.empty? && block.arity != 0 - block.call(*args) - else - block.call - end - end - end + ::ActiveSupport::Notifications.instrument(event_name, args) end def self.normalized_event_name( event_name ) diff --git a/lib/protobuf/rpc/service_directory.rb b/lib/protobuf/rpc/service_directory.rb index 0bd75a88..bc7874d4 100644 --- a/lib/protobuf/rpc/service_directory.rb +++ b/lib/protobuf/rpc/service_directory.rb @@ -5,7 +5,6 @@ require 'thread' require 'timeout' -require 'protobuf/lifecycle' require 'protobuf/rpc/dynamic_discovery.pb' module Protobuf @@ -239,7 +238,7 @@ def run end def trigger(action, listing) - ::Protobuf::Lifecycle.trigger("directory.listing.#{action}", listing) + ::ActiveSupport::Notifications.instrument("directory.listing.#{action}", :listing => listing) end end end diff --git a/spec/lib/protobuf/cli_spec.rb b/spec/lib/protobuf/cli_spec.rb index e58f4bf0..3ed68e01 100644 --- a/spec/lib/protobuf/cli_spec.rb +++ b/spec/lib/protobuf/cli_spec.rb @@ -9,13 +9,13 @@ let(:sock_runner) { runner = double("SocketRunner", :register_signals => nil) - runner.stub(:run) { ::Protobuf::Lifecycle.trigger( "after_server_bind" ) } + runner.stub(:run) { ::ActiveSupport::Notifications.publish( "after_server_bind" ) } runner } let(:zmq_runner) { runner = double "ZmqRunner", register_signals: nil - runner.stub(:run) { ::Protobuf::Lifecycle.trigger( "after_server_bind" ) } + runner.stub(:run) { ::ActiveSupport::Notifications.publish( "after_server_bind" ) } runner } diff --git a/spec/lib/protobuf/lifecycle_spec.rb b/spec/lib/protobuf/lifecycle_spec.rb index e889ce82..f40a5fb2 100644 --- a/spec/lib/protobuf/lifecycle_spec.rb +++ b/spec/lib/protobuf/lifecycle_spec.rb @@ -5,23 +5,12 @@ subject { described_class } before(:each) do - described_class.lifecycle_events = {} + ::ActiveSupport::Notifications.notifier = ::ActiveSupport::Notifications::Fanout.new end it "registers a string as the event_name" do - expect { - subject.register("something") do - true - end - }.to change { subject.lifecycle_events.size }.by(1) - end - - it "registers a symbol as the event_name" do - expect { - subject.register("something") do - true - end - }.to change { subject.lifecycle_events.size }.by(1) + ::ActiveSupport::Notifications.should_receive(:subscribe).with("something") + subject.register("something") { true } end it "only registers blocks for event callbacks" do diff --git a/spec/lib/protobuf/rpc/service_directory_spec.rb b/spec/lib/protobuf/rpc/service_directory_spec.rb index 9cc5d8b8..c7bae230 100644 --- a/spec/lib/protobuf/rpc/service_directory_spec.rb +++ b/spec/lib/protobuf/rpc/service_directory_spec.rb @@ -56,10 +56,9 @@ end def expect_event_trigger(event) - ::Protobuf::Lifecycle - .should_receive(:trigger) - .with(event, - an_instance_of(::Protobuf::Rpc::ServiceDirectory::Listing)) + ::ActiveSupport::Notifications + .should_receive(:instrument) + .with(event, hash_including(:listing => an_instance_of(::Protobuf::Rpc::ServiceDirectory::Listing))) .once end