protobuf 2.8.0.beta8 → 2.8.0.beta9

Sign up to get free protection for your applications and to get access to all the features.
data/lib/protobuf/cli.rb CHANGED
@@ -1,5 +1,6 @@
1
1
  require 'thor'
2
2
  require 'protobuf/version'
3
+ require 'protobuf/lifecycle'
3
4
  require 'protobuf/logger'
4
5
  require 'protobuf/rpc/servers/evented_runner'
5
6
  require 'protobuf/rpc/servers/socket_runner'
@@ -87,7 +88,7 @@ module Protobuf
87
88
  def configure_logger
88
89
  debug_say('Configuring logger')
89
90
  ::Protobuf::Logger.configure({ :file => options.log || STDOUT,
90
- :level => options.debug? ? ::Logger::DEBUG : options.level })
91
+ :level => options.debug? ? ::Logger::DEBUG : options.level })
91
92
 
92
93
  # Debug output the server options to the log file.
93
94
  ::Protobuf::Logger.debug { 'Debugging options:' }
@@ -242,9 +243,10 @@ module Protobuf
242
243
  ::Protobuf::Logger.info {
243
244
  "pid #{::Process.pid} -- #{@runner_mode} RPC Server listening at #{options.host}:#{options.port}"
244
245
  }
246
+
247
+ ::Protobuf::Lifecycle.trigger( "after_server_bind" )
245
248
  end
246
249
  end
247
250
  end
248
251
  end
249
252
  end
250
-
@@ -0,0 +1,34 @@
1
+ module Protobuf
2
+ class Lifecycle
3
+
4
+ def self.register( event_name, &blk )
5
+ raise "Lifecycle register must have a block" unless block_given?
6
+ event_name = normalized_event_name( event_name )
7
+
8
+ lifecycle_events[ event_name ] ||= []
9
+ lifecycle_events[ event_name ] << blk
10
+ end
11
+
12
+ def self.trigger( event_name )
13
+ event_name = normalized_event_name( event_name )
14
+
15
+ if lifecycle_events.has_key?( event_name )
16
+ lifecycle_events[ event_name ].each do |block|
17
+ block.call
18
+ end
19
+ end
20
+ end
21
+
22
+ def self.normalized_event_name( event_name )
23
+ return "#{event_name}".downcase
24
+ end
25
+
26
+ class << self
27
+ attr_accessor :lifecycle_events
28
+
29
+ alias_method :on, :register
30
+ end
31
+
32
+ @lifecycle_events ||= {}
33
+ end
34
+ end
@@ -167,8 +167,10 @@ module Protobuf
167
167
 
168
168
  start_broker unless brokerless?
169
169
  start_missing_workers
170
+
171
+ yield if block_given? # runs on startup
170
172
  wait_for_shutdown_signal
171
- broadcast_flatline if broadcast_beacons?
173
+ broadcast_flatline if broadcast_beacons?
172
174
  Thread.pass until reap_dead_workers.empty?
173
175
  @broker.join unless brokerless?
174
176
  ensure
@@ -20,8 +20,9 @@ module Protobuf
20
20
  def run
21
21
  @server = ::Protobuf::Rpc::Zmq::Server.new(@options)
22
22
  register_signals
23
- yield if block_given?
24
- @server.run
23
+ @server.run do
24
+ yield if block_given?
25
+ end
25
26
  end
26
27
 
27
28
  def running?
@@ -1,4 +1,4 @@
1
1
  module Protobuf
2
- VERSION = '2.8.0.beta8'
2
+ VERSION = '2.8.0.beta9'
3
3
  PROTOC_VERSION = '2.4.1'
4
4
  end
@@ -7,9 +7,23 @@ describe ::Protobuf::CLI do
7
7
  File.expand_path('../../../support/test_app_file.rb', __FILE__)
8
8
  end
9
9
 
10
- let(:sock_runner) { double "SocketRunner", run: nil, register_signals: nil }
11
- let(:zmq_runner) { double "ZmqRunner", run: nil, register_signals: nil }
12
- let(:evented_runner) { double "EventedRunner", run: nil, register_signals: nil }
10
+ let(:sock_runner) {
11
+ runner = double("SocketRunner", :register_signals => nil)
12
+ runner.stub(:run) { ::Protobuf::Lifecycle.trigger( "after_server_bind" ) }
13
+ runner
14
+ }
15
+
16
+ let(:zmq_runner) {
17
+ runner = double "ZmqRunner", register_signals: nil
18
+ runner.stub(:run) { ::Protobuf::Lifecycle.trigger( "after_server_bind" ) }
19
+ runner
20
+ }
21
+
22
+ let(:evented_runner) {
23
+ runner = double "EventedRunner", register_signals: nil
24
+ runner.stub(:run) { ::Protobuf::Lifecycle.trigger( "after_server_bind" ) }
25
+ runner
26
+ }
13
27
 
14
28
  before(:each) do
15
29
  ::Protobuf::Rpc::SocketRunner.stub(:new) { sock_runner }
@@ -0,0 +1,70 @@
1
+ require 'spec_helper'
2
+ require 'protobuf/lifecycle'
3
+
4
+ describe ::Protobuf::Lifecycle do
5
+ subject { described_class }
6
+
7
+ before(:each) do
8
+ described_class.lifecycle_events = {}
9
+ end
10
+
11
+ it "registers a string as the event_name" do
12
+ expect {
13
+ subject.register("something") do
14
+ true
15
+ end
16
+ }.to change { subject.lifecycle_events.size }.by(1)
17
+ end
18
+
19
+ it "registers a symbol as the event_name" do
20
+ expect {
21
+ subject.register("something") do
22
+ true
23
+ end
24
+ }.to change { subject.lifecycle_events.size }.by(1)
25
+ end
26
+
27
+ it "only registers blocks for event callbacks" do
28
+ expect {
29
+ subject.register("something")
30
+ }.to raise_error( /block/ )
31
+ end
32
+
33
+ it "calls the registered block when triggered" do
34
+ this = nil
35
+ subject.register("this") do
36
+ this = "not nil"
37
+ end
38
+
39
+ subject.trigger("this")
40
+ this.should_not be_nil
41
+ this.should eq("not nil")
42
+ end
43
+
44
+ it "calls multiple registered blocks when triggered" do
45
+ this = nil
46
+ that = nil
47
+
48
+ subject.register("this") do
49
+ this = "not nil"
50
+ end
51
+
52
+ subject.register("this") do
53
+ that = "not nil"
54
+ end
55
+
56
+ subject.trigger("this")
57
+ this.should_not be_nil
58
+ this.should eq("not nil")
59
+ that.should_not be_nil
60
+ that.should eq("not nil")
61
+ end
62
+
63
+ context "normalized event names" do
64
+ specify { subject.normalized_event_name(:derp).should eq("derp") }
65
+ specify { subject.normalized_event_name(:Derp).should eq("derp") }
66
+ specify { subject.normalized_event_name("DERP").should eq("derp") }
67
+ specify { subject.normalized_event_name("derp").should eq("derp") }
68
+ end
69
+
70
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: protobuf
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.8.0.beta8
4
+ version: 2.8.0.beta9
5
5
  prerelease: 6
6
6
  platform: ruby
7
7
  authors:
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2013-07-15 00:00:00.000000000 Z
13
+ date: 2013-07-19 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: activesupport
@@ -367,6 +367,7 @@ files:
367
367
  - lib/protobuf/field/uint32_field.rb
368
368
  - lib/protobuf/field/uint64_field.rb
369
369
  - lib/protobuf/field/varint_field.rb
370
+ - lib/protobuf/lifecycle.rb
370
371
  - lib/protobuf/logger.rb
371
372
  - lib/protobuf/message.rb
372
373
  - lib/protobuf/message/decoder.rb
@@ -417,6 +418,7 @@ files:
417
418
  - spec/lib/protobuf/enum_spec.rb
418
419
  - spec/lib/protobuf/enum_value_spec.rb
419
420
  - spec/lib/protobuf/field/int32_field_spec.rb
421
+ - spec/lib/protobuf/lifecycle_spec.rb
420
422
  - spec/lib/protobuf/logger_spec.rb
421
423
  - spec/lib/protobuf/message_spec.rb
422
424
  - spec/lib/protobuf/rpc/client_spec.rb
@@ -507,7 +509,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
507
509
  version: '0'
508
510
  segments:
509
511
  - 0
510
- hash: 1303972375277947888
512
+ hash: 3723502328517629432
511
513
  required_rubygems_version: !ruby/object:Gem::Requirement
512
514
  none: false
513
515
  requirements: