Skip to content

Commit

Permalink
Allow PoolManager to incorrectly resume a task from within a task
Browse files Browse the repository at this point in the history
  • Loading branch information
halorgium committed Sep 5, 2013
1 parent be46cab commit 10e29c1
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 2 deletions.
1 change: 1 addition & 0 deletions lib/celluloid/pool_manager.rb
Expand Up @@ -91,6 +91,7 @@ def idle_size

# Provision a new worker
def __provision_worker__
Task.current.guard_warnings = true
while @idle.empty?
# Wait for responses from one of the busy workers
response = exclusive { receive { |msg| msg.is_a?(Response) } }
Expand Down
13 changes: 11 additions & 2 deletions lib/celluloid/tasks.rb
Expand Up @@ -25,7 +25,7 @@ def self.suspend(status)
end

attr_reader :type, :meta, :status
attr_accessor :chain_id
attr_accessor :chain_id, :guard_warnings

# Create a new task
def initialize(type, meta)
Expand All @@ -35,6 +35,7 @@ def initialize(type, meta)

@exclusive = false
@dangerous_suspend = @meta ? @meta.delete(:dangerous_suspend) : false
@guard_warnings = false

actor = Thread.current[:celluloid_actor]
@chain_id = CallChain.current_id
Expand Down Expand Up @@ -93,7 +94,7 @@ def suspend(status)

# Resume a suspended task, giving it a value to return if needed
def resume(value = nil)
raise "Cannot resume a task from inside of a task" if Thread.current[:celluloid_task]
guard "Cannot resume a task from inside of a task" if Thread.current[:celluloid_task]
deliver(value)
nil
end
Expand Down Expand Up @@ -141,6 +142,14 @@ def running?; @status != :dead; end
def inspect
"#<#{self.class}:0x#{object_id.to_s(16)} @type=#{@type.inspect}, @meta=#{@meta.inspect}, @status=#{@status.inspect}>"
end

def guard(message)
if @guard_warnings
Logger.warn message if $CELLULOID_DEBUG
else
raise message
end
end
end
end

Expand Down
7 changes: 7 additions & 0 deletions spec/celluloid/pool_spec.rb
Expand Up @@ -49,4 +49,11 @@ def crash
it "terminates" do
expect { subject.terminate }.to_not raise_exception
end

it "handles many requests" do
futures = 10.times.map do
subject.future.process
end
futures.map(&:value)
end
end

0 comments on commit 10e29c1

Please sign in to comment.