Skip to content

Commit

Permalink
added auto terminate
Browse files Browse the repository at this point in the history
  • Loading branch information
kapso committed Aug 18, 2012
1 parent 027b75a commit 83a4b54
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 14 deletions.
38 changes: 28 additions & 10 deletions app/models/actor_manager.rb
@@ -1,11 +1,13 @@
class ActorManager
def initialize(actor = nil)
raise 'Not a valid actor object' if actor.present? && !actor.is_a?(Celluloid)
raise 'Not a valid actor object' if actor.present? && !actor.is_a?(Celluloid)

@init_actor = actor
@futures = []
@actors = []
@response = []
@next_response_ctr = 0
@auto_terminate = true
end

def submit(method, *params)
Expand Down Expand Up @@ -38,9 +40,9 @@ def response
end

def next_response
if @futures.blank?
raise 'No actors/requests submitted, cannot process response'
elsif @next_response_ctr >= @futures.size
logger.warn '[ActorManager] No actors/requests submitted' if @futures.blank?

if @next_response_ctr >= @futures.size
nil
else
value = process_request(@next_response_ctr)
Expand All @@ -53,12 +55,18 @@ def next_response
def terminate!
actor_arr = @actors.uniq

actor_arr.each do |actor|
begin
actor.terminate if actor.alive?
rescue => e
logger.warn "Error: terminating actor #{actor.inspect}: #{e.message}"
if actor_arr.present?
actor_arr.each do |actor|
begin
actor.terminate if actor.alive?
rescue => e
logger.warn "[ActorManager] Error: terminating actor #{actor.inspect}: #{e.message}"
end
end

@actors.clear
@futures.clear
@next_response_ctr = 0
end
end

Expand All @@ -83,14 +91,24 @@ def any_nil_response?
response.include? nil
end

def set_auto_terminate(auto = true)
@auto_terminate = auto
end

def auto_terminate?
@auto_terminate
end

private
def logger
Rails.logger
end

def process_request(index)
if @actors[index].alive?
@futures[index].value
value = @futures[index].value
terminate! if auto_terminate? && index == (@futures.size - 1)
value
else
raise "Actor is dead possibly due to some earlier exception, cannot process response: #{@actors[index].inspect}"
end
Expand Down
6 changes: 2 additions & 4 deletions app/models/car.rb
Expand Up @@ -50,10 +50,8 @@ def self.test_drive(thread_count = 5)
# resp << r
# end

puts "\nTasks complete: #{am.tasks_complete?}"

# Required to terminate actors & GC
am.terminate!
# Terminate actors & GC. Required if set_auto_terminate(false)
# am.terminate!

resp
end
Expand Down

0 comments on commit 83a4b54

Please sign in to comment.