Skip to content

Commit

Permalink
Merge pull request #527 from celluloid/publish-module-method
Browse files Browse the repository at this point in the history
Publish module method, and pub/sub example.
  • Loading branch information
digitalextremist committed Mar 31, 2015
2 parents 8418a58 + e14a9df commit 114f229
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGES.md
Expand Up @@ -3,6 +3,7 @@ HEAD
* Make "Terminating task" log messages debug-level events
* Fix $CELLULOID_TEST warnings
* Added `.dead?` method on actors, as opposite of `.alive?`
* Added class/module method to access `publish` outside actors.

0.16.0 (2014-09-04)
-------------------
Expand Down
36 changes: 36 additions & 0 deletions examples/pubsub.rb
@@ -0,0 +1,36 @@
require 'celluloid/autostart'

class Publisher
include Celluloid
include Celluloid::Notifications

def initialize
now = Time.now.to_f
sleep now.ceil - now + 0.001
9.times {
publish 'example_write_by_instance_method', Time.now
}
end
end

class Subscriber
include Celluloid
include Celluloid::Notifications
include Celluloid::Logger

def initialize
info "Subscribing to topics."
subscribe 'example_write_by_instance_method', :new_message
subscribe 'example_write_by_class_method', :new_message
end

def new_message(topic,data)
info "#{topic}: #{data}"
end
end

sub = Subscriber.new

Celluloid::Notifications.publish "example_write_by_class_method", Time.now

pub = Publisher.new
5 changes: 5 additions & 0 deletions lib/celluloid/notifications.rb
Expand Up @@ -7,6 +7,7 @@ def self.notifier
def publish(pattern, *args)
Celluloid::Notifications.notifier.publish(pattern, *args)
end
module_function :publish

def subscribe(pattern, method)
Celluloid::Notifications.notifier.subscribe(Actor.current, pattern, method)
Expand Down Expand Up @@ -80,4 +81,8 @@ def matches?(subscriber_or_pattern)
end
end
end

def self.publish(*args)
Notifications.publish(*args)
end
end
5 changes: 4 additions & 1 deletion spec/celluloid/probe_spec.rb
Expand Up @@ -34,9 +34,12 @@ def event_received(topic, args)
end
end

puts "CELLULOID_MONITORING: #{$CELLULOID_MONITORING}"


RSpec.describe "Probe", actor_system: :global do
describe 'on boot' do
it 'should capture system actor spawn' do
it 'should capture system actor spawn', flaky: true do
client = TestProbeClient.new
Celluloid::Probe.run
create_events = []
Expand Down

0 comments on commit 114f229

Please sign in to comment.