1
0
mirror of https://github.com/OpenNebula/one.git synced 2025-01-10 01:17:40 +03:00

Built-in FINALIZE action for the ActionManager. No thread is now started

for the ActionManager. Updated the examples



git-svn-id: http://svn.opennebula.org/one/trunk@334 3034c82b-c49b-4eb3-8279-a7acafdc01c0
This commit is contained in:
Rubén S. Montero 2009-01-30 18:45:13 +00:00
parent a8e3c441fa
commit 0357b87471
2 changed files with 37 additions and 43 deletions

View File

@ -64,14 +64,15 @@ class ActionManager
# +threaded+ if true actions will be executed by default in a different
# thread
def initialize(concurrency=10, threaded=true)
@finalize = false
@actions = Hash.new
@threaded = threaded
@listener = nil
@concurrency = concurrency
@action_queue = Array.new
@running_actions = 0
@listener_thread = nil
@threads_mutex = Mutex.new
@threads_cond = ConditionVariable.new
end
@ -95,7 +96,16 @@ class ActionManager
# +aname+ name of the action
# +aargs+ arguments to call the action
def trigger_action(aname,*aargs)
return if @finalize
@threads_mutex.synchronize {
if aname == "FINALIZE"
@finalize = true
@threads_cond.signal if @running_actions == 0
return
end
if !@actions.has_key?(aname)
return
@ -113,23 +123,19 @@ class ActionManager
}
end
def stop_listener
@listener_thread.kill!
end
def start_listener
@listener_thread = Thread.new {
while true
@threads_mutex.synchronize {
while ((@concurrency - @running_actions)==0) ||
@action_queue.size==0
@threads_cond.wait(@threads_mutex)
end
while true
@threads_mutex.synchronize {
while ((@concurrency - @running_actions)==0) ||
@action_queue.size==0
@threads_cond.wait(@threads_mutex)
return if ( @finalize && @running_actions == 0)
end
run_action
}
end
}
end
end
private
@ -153,7 +159,6 @@ private
action[:method].call(*action[:args])
@running_actions -= 1
@threads_cond.signal
end
end
end
@ -171,9 +176,6 @@ if __FILE__ == $0
@am.register_action("SLEEP",method("sleep_action"))
# @am.register_action("SLEEP",Proc.new{|s,i| p s ; sleep(s)})
@am.register_action("NOP",method("nop_action"))
@am.register_action("FINALIZE",method("finalize_action"),false)
@am.start_listener
end
def sleep_action(secs, id)
@ -185,27 +187,24 @@ if __FILE__ == $0
def nop_action
p " - Just an action"
end
def finalize_action
p "Exiting..."
@am.stop_listener
p "Done!"
end
end
s = Sample.new
100.times {|n|
100.times {|m|
s.am.trigger_action("SLEEP",rand(3)+1,m+n)
Thread.new {
sleep 1
100.times {|n|
s.am.trigger_action("SLEEP",rand(3)+1,n)
s.am.trigger_action("NOP")
}
s.am.trigger_action("FINALIZE")
s.am.trigger_action("SLEEP",rand(3)+1,999)
s.am.trigger_action("SLEEP",rand(3)+1,333)
}
sleep 10
s.am.trigger_action("FINALIZE")
sleep 3600
s.am.start_listener
end

View File

@ -33,7 +33,6 @@ class OpenNebulaDriver
@send_mutex=Mutex.new
@am = ActionManager.new(concurrency,threaded)
@am.register_action("FINALIZE",method("stop_driver"),false)
end
def send_message(*args)
@ -53,17 +52,15 @@ class OpenNebulaDriver
end
def start_driver
loop_thread = Thread.new { loop }
@am.start_listener
loop
loop_thread.kill!
end
private
def stop_driver
@am.stop_listener
exit(0)
end
def loop
while true
exit(-1) if STDIN.eof?
@ -106,6 +103,4 @@ if __FILE__ == $0
sd = SampleDriver.new
sd.start_driver
gets
end