2012-06-29 05:18:28 +00:00
|
|
|
# -*- coding: binary -*-
|
2009-12-28 00:21:21 +00:00
|
|
|
module Msf
|
|
|
|
|
|
|
|
###
|
|
|
|
#
|
|
|
|
# This class provides a task manager
|
|
|
|
#
|
|
|
|
###
|
|
|
|
|
|
|
|
class TaskManager
|
|
|
|
|
|
|
|
class Task
|
|
|
|
attr_accessor :timeout
|
|
|
|
attr_accessor :created
|
|
|
|
attr_accessor :completed
|
|
|
|
attr_accessor :status
|
|
|
|
attr_accessor :proc
|
|
|
|
attr_accessor :source
|
|
|
|
attr_accessor :exception
|
|
|
|
|
|
|
|
#
|
|
|
|
# Create a new task
|
|
|
|
#
|
|
|
|
def initialize(proc,timeout=nil)
|
|
|
|
self.proc = proc
|
|
|
|
self.status = :new
|
|
|
|
self.created = Time.now
|
|
|
|
self.timeout = timeout
|
|
|
|
self.source = caller
|
|
|
|
end
|
|
|
|
|
|
|
|
#
|
|
|
|
# Task duration in seconds (float)
|
|
|
|
#
|
|
|
|
def duration
|
|
|
|
etime = self.completed || Time.now
|
|
|
|
etime.to_f - self.created.to_f
|
|
|
|
end
|
|
|
|
|
2009-12-28 20:15:33 +00:00
|
|
|
def wait
|
|
|
|
while self.status == :new
|
2010-05-20 20:42:17 +00:00
|
|
|
::IO.select(nil,nil,nil,0.10)
|
2009-12-28 20:15:33 +00:00
|
|
|
end
|
2010-01-09 02:11:46 +00:00
|
|
|
return self.status
|
2009-12-28 20:15:33 +00:00
|
|
|
end
|
|
|
|
|
2009-12-28 19:37:14 +00:00
|
|
|
#
|
|
|
|
# Run the associated proc
|
|
|
|
#
|
|
|
|
def run(*args)
|
2009-12-28 19:44:14 +00:00
|
|
|
self.proc.call(*args) if self.proc
|
2009-12-28 19:37:14 +00:00
|
|
|
end
|
|
|
|
|
2009-12-28 00:21:21 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
attr_accessor :processing
|
|
|
|
attr_accessor :queue
|
|
|
|
attr_accessor :thread
|
|
|
|
attr_accessor :framework
|
|
|
|
|
|
|
|
#
|
|
|
|
# Create a new TaskManager
|
|
|
|
#
|
|
|
|
def initialize(framework)
|
|
|
|
self.framework = framework
|
|
|
|
self.flush
|
|
|
|
end
|
|
|
|
|
|
|
|
#
|
|
|
|
# Add a new task via proc
|
|
|
|
#
|
|
|
|
def queue_proc(proc)
|
2009-12-28 20:15:33 +00:00
|
|
|
task = Task.new(proc)
|
|
|
|
queue_task(task)
|
|
|
|
return task
|
2009-12-28 00:21:21 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
#
|
2010-10-02 18:48:29 +00:00
|
|
|
# Add a new task to the queue unless we are called
|
2011-11-20 01:32:06 +00:00
|
|
|
# by the queue thread itself.
|
2009-12-28 00:21:21 +00:00
|
|
|
#
|
|
|
|
def queue_task(task)
|
2010-10-02 18:48:29 +00:00
|
|
|
if Thread.current[:task_manager]
|
|
|
|
process_task(task)
|
|
|
|
else
|
|
|
|
self.queue.push(task)
|
|
|
|
end
|
2009-12-28 00:21:21 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
#
|
|
|
|
# Flush the queue
|
|
|
|
#
|
|
|
|
def flush
|
|
|
|
self.queue = []
|
|
|
|
end
|
|
|
|
|
|
|
|
#
|
|
|
|
# Stop processing events
|
|
|
|
#
|
|
|
|
def stop
|
|
|
|
return if not self.thread
|
|
|
|
self.processing = false
|
|
|
|
self.thread.join
|
|
|
|
self.thread = nil
|
|
|
|
end
|
|
|
|
|
|
|
|
#
|
|
|
|
# Forcefully kill the processing thread
|
|
|
|
#
|
|
|
|
def kill
|
|
|
|
return if not self.thread
|
|
|
|
self.processing = false
|
|
|
|
self.thread.kill
|
|
|
|
self.thread = nil
|
|
|
|
end
|
|
|
|
|
|
|
|
#
|
|
|
|
# Start processing tasks
|
|
|
|
#
|
|
|
|
def start
|
|
|
|
return if self.thread
|
|
|
|
self.processing = true
|
2010-11-12 06:19:49 +00:00
|
|
|
self.thread = framework.threads.spawn("TaskManager", true) do
|
2010-04-15 19:25:48 +00:00
|
|
|
begin
|
|
|
|
process_tasks
|
|
|
|
rescue ::Exception => e
|
|
|
|
elog("taskmanager: process_tasks exception: #{e.class} #{e} #{e.backtrace}")
|
|
|
|
retry
|
|
|
|
end
|
2009-12-28 00:21:21 +00:00
|
|
|
end
|
2011-11-20 01:32:06 +00:00
|
|
|
|
2010-10-02 18:48:29 +00:00
|
|
|
# Mark this thread as the task manager
|
|
|
|
self.thread[:task_manager] = true
|
2011-11-20 01:32:06 +00:00
|
|
|
|
2010-10-02 18:48:29 +00:00
|
|
|
# Return the thread object to the caller
|
|
|
|
self.thread
|
2009-12-28 00:21:21 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
#
|
|
|
|
# Restart the task processor
|
|
|
|
#
|
|
|
|
def restart
|
|
|
|
stop
|
|
|
|
start
|
|
|
|
end
|
|
|
|
|
|
|
|
#
|
|
|
|
# Retrieve the number of tasks in the queue
|
|
|
|
#
|
|
|
|
def backlog
|
|
|
|
self.queue.length
|
|
|
|
end
|
|
|
|
|
|
|
|
#
|
|
|
|
# Process the actual queue
|
|
|
|
#
|
|
|
|
def process_tasks
|
2010-04-05 20:34:41 +00:00
|
|
|
spin = 50
|
|
|
|
ltask = nil
|
|
|
|
|
2009-12-28 00:21:21 +00:00
|
|
|
while(self.processing)
|
2010-04-05 20:34:41 +00:00
|
|
|
cnt = 0
|
2009-12-28 00:21:21 +00:00
|
|
|
while(task = self.queue.shift)
|
2010-04-05 20:34:41 +00:00
|
|
|
stime = Time.now.to_f
|
2009-12-28 00:21:21 +00:00
|
|
|
ret = process_task(task)
|
2010-04-05 20:34:41 +00:00
|
|
|
etime = Time.now.to_f
|
|
|
|
|
2009-12-28 00:21:21 +00:00
|
|
|
case ret
|
|
|
|
when :requeue
|
|
|
|
self.queue.push(task)
|
|
|
|
when :drop, :done
|
|
|
|
# Processed or dropped
|
|
|
|
end
|
2010-04-05 20:34:41 +00:00
|
|
|
cnt += 1
|
|
|
|
|
|
|
|
ltask = task
|
2009-12-28 00:21:21 +00:00
|
|
|
end
|
2010-04-05 20:34:41 +00:00
|
|
|
|
|
|
|
spin = (cnt == 0) ? (spin + 1) : 0
|
|
|
|
|
|
|
|
if spin > 10
|
2010-05-20 20:42:17 +00:00
|
|
|
::IO.select(nil, nil, nil, 0.25)
|
2010-04-05 20:34:41 +00:00
|
|
|
end
|
|
|
|
|
2009-12-28 00:21:21 +00:00
|
|
|
end
|
|
|
|
self.thread = nil
|
|
|
|
end
|
|
|
|
|
|
|
|
#
|
|
|
|
# Process a specific task from the queue
|
|
|
|
#
|
|
|
|
def process_task(task)
|
|
|
|
begin
|
|
|
|
if(task.timeout)
|
|
|
|
::Timeout.timeout(task.timeout) do
|
2009-12-28 19:37:14 +00:00
|
|
|
task.run(self, task)
|
2009-12-28 00:21:21 +00:00
|
|
|
end
|
|
|
|
else
|
2009-12-28 19:37:14 +00:00
|
|
|
task.run(self, task)
|
2009-12-28 00:21:21 +00:00
|
|
|
end
|
2009-12-28 19:44:14 +00:00
|
|
|
rescue ::ThreadError
|
|
|
|
# Ignore these (caused by a return inside of the proc)
|
2009-12-28 00:21:21 +00:00
|
|
|
rescue ::Exception => e
|
|
|
|
|
|
|
|
if(e.class == ::Timeout::Error)
|
|
|
|
elog("taskmanager: task #{task.inspect} timed out after #{task.timeout} seconds")
|
|
|
|
task.status = :timeout
|
|
|
|
task.completed = Time.now
|
|
|
|
return :drop
|
|
|
|
end
|
|
|
|
|
2010-04-24 18:34:21 +00:00
|
|
|
elog("taskmanager: task triggered an exception: #{e.class} #{e}")
|
|
|
|
elog("taskmanager: task proc: #{task.proc.inspect} ")
|
2011-04-07 21:59:32 +00:00
|
|
|
elog("taskmanager: task Call stack: \n#{task.source.join("\n")} ")
|
2009-12-28 00:21:21 +00:00
|
|
|
dlog("Call stack:\n#{$@.join("\n")}")
|
|
|
|
|
|
|
|
task.status = :dropped
|
|
|
|
task.exception = e
|
|
|
|
return :drop
|
|
|
|
|
|
|
|
end
|
|
|
|
task.status = :done
|
|
|
|
task.completed = Time.now
|
|
|
|
return :done
|
|
|
|
end
|
|
|
|
|
|
|
|
def log_error(msg)
|
|
|
|
elog(msg, 'core')
|
|
|
|
end
|
|
|
|
|
|
|
|
def log_debug(msg)
|
|
|
|
dlog(msg, 'core')
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|