Skip to content

Commit e801d6c

Browse files
committed
Added a shutdown hook for ActorContext and SimpleActorRef.
1 parent 58c21be commit e801d6c

File tree

3 files changed

+18
-45
lines changed

3 files changed

+18
-45
lines changed

demos/global_thread_pool-demo.rb

Lines changed: 0 additions & 45 deletions
This file was deleted.

lib/concurrent/simple_actor_ref.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ def initialize(actor, opts = {})
1919
@reset_on_error = opts.fetch(:reset_on_error, true)
2020
@exception_class = opts.fetch(:rescue_exception, false) ? Exception : StandardError
2121
@observers = CopyOnNotifyObserverSet.new
22+
23+
@actor.define_singleton_method(:shutdown, &method(:set_stop_event))
2224
end
2325

2426
def running?
@@ -70,6 +72,10 @@ def join(timeout = nil)
7072

7173
Message = Struct.new(:payload, :ivar, :callback)
7274

75+
def set_stop_event
76+
@stop_event.set
77+
end
78+
7379
def supervise
7480
if @thread.nil?
7581
@actor.on_start
@@ -109,7 +115,10 @@ def run_message_loop
109115

110116
observers.notify_observers(now, message.payload, result, ex)
111117
end
118+
119+
break if @stop_event.set?
112120
end
121+
@actor.on_shutdown
113122
end
114123
end
115124
end

spec/concurrent/actor_ref_shared.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ def receive(*msg)
1616
raise StandardError
1717
when :bullet
1818
raise Exception
19+
when :stop
20+
shutdown
1921
when :terminate
2022
Thread.current.kill
2123
when :sleep
@@ -48,6 +50,13 @@ def receive(*msg)
4850
sleep(0.1)
4951
subject.should be_shutdown
5052
end
53+
54+
it 'defines a shutdown method on the actor(s)' do
55+
subject << :foo
56+
subject << :stop
57+
sleep(0.1)
58+
subject.should be_shutdown
59+
end
5160
end
5261

5362
context '#post' do

0 commit comments

Comments
 (0)