Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace custom lifecycle with AS::Notifications #138

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/protobuf.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
3 changes: 1 addition & 2 deletions lib/protobuf/cli.rb
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -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
Expand Down
31 changes: 20 additions & 11 deletions lib/protobuf/lifecycle.rb
Original file line number Diff line number Diff line change
@@ -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 )
Expand Down
3 changes: 1 addition & 2 deletions lib/protobuf/rpc/service_directory.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
require 'thread'
require 'timeout'

require 'protobuf/lifecycle'
require 'protobuf/rpc/dynamic_discovery.pb'

module Protobuf
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions spec/lib/protobuf/cli_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
17 changes: 3 additions & 14 deletions spec/lib/protobuf/lifecycle_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 3 additions & 4 deletions spec/lib/protobuf/rpc/service_directory_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down